Requesting help for programming/ CS majors?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Superfreak04
    D7 Elite Keymasher
    • Jan 2007
    • 2407

    #1

    Requesting help for programming/ CS majors?

    This is probably easy for most, but I can't seem to figure it out. This is in C++ btw.



    #include <iostream>
    using namespace std;

    int main()
    {
    int name[12];
    int i;
    for(int i=0; i<12; i++) name[i] = i;
    for(i=0; i<12; i++)
    cout << "This is letter ["<< i <<"]: " << name[i] << "\n";

    }

    int dummy;
    cin >> dummy;

    return 0;
    }

    I have to use dummy's because the PC won't display anything without it.

    This is what I have so far, I'm not really sure about what to do from here. I'm not even sure if this is correct, lol.

    Help>
    Last edited by Superfreak04; 10-21-2013, 07:37 PM.
  • dAnceguy117
    new hand moves = dab
    FFR Simfile Author
    • Dec 2002
    • 10097

    #2
    Re: Requesting help for programming/ CS majors?

    I have nooo experience with C++ sorry, I'm sure someone else will come to the rescue soon. in the meantime

    int i;
    for(int i=0; i<12; i++) name[i] = i;
    you can get rid of the second "int" there, you already declared this variable.

    for each element in the array, you're storing its number. is that intentional?



    sorting the array in alphabetical order manually would be the trickiest part of this assignment by far. do you know if you're allowed to use C++ library functions to do the work for you?

    for the frequency of letters, I would make another array with 26 elements. each element in the array should have an int initialized to 0. the 1st element represents 'a', the 2nd represents 'b', etc. as you read in each character from the input, you'll want to compute the 0-25 value based on the character's ASCII value, and then increment (+1) the int stored in that letter's element.

    ^ ew that's a yucky low-level way of doing it. again, you should find out if you're allowed to use all of the functions available to you.

    good luck!

    Comment

    • rushyrulz
      Digital Dancing!
      FFR Simfile Author
      FFR Music Producer
      • Feb 2006
      • 12985

      #3
      Re: Requesting help for programming/ CS majors?

      you're using the same control variable for nested for loops. EDIT: nvm confusing without curly braces, loops aren't nested.

      Will edit with other thoughts gimme a min

      Code:
      #include <iostream>
      using namespace std;
      
      int main()
      {
      int name[12];
      int i;
      for(int i=0; i<12; i++) name[i] = i;
      for(i=0; i<12; i++)
      cout << "This is letter ["<< i <<"]: " << name[i] << "\n";
      
      }
      
      int dummy;
      cin >> dummy;
      
      return 0;
      }
      I'm not so much as experienced in C++ as I am in C, but I assume the concepts are the same.

      to read in the inputs, you should use a scanf and assign those to name[i] instead of just assigning i to name[i].

      Then you can loop through the array and perform the sorting algorithm.

      let me code up an example real quick..
      Last edited by rushyrulz; 10-15-2013, 01:56 PM.


      Comment

      • dAnceguy117
        new hand moves = dab
        FFR Simfile Author
        • Dec 2002
        • 10097

        #4
        Re: Requesting help for programming/ CS majors?

        it doesn't look like those for loops are nested. the code just doesn't have them spaced out.

        Comment

        • Superfreak04
          D7 Elite Keymasher
          • Jan 2007
          • 2407

          #5
          Re: Requesting help for programming/ CS majors?

          Thanks for the input!

          Anyways, I believe we are allowed to use commands that do it for us. And I meant to put in characters, not integers. Afterall, we are input letters, not numbers haha. THis is what I have. It's still wrong, but yeah.

          #include <iostream>
          using namespace std;

          int main()
          {
          char name[12];
          char i;

          for(i=0; i<12; i++) name[i] = i;

          cout << "Please enter your name one letter at a time: ";
          cin >> name;
          for(i=0; i<12; i++)
          cout << "This is letter ["<< i <<"]: " << name[i] << "\n";



          int dummy;
          cin >> dummy;

          return 0;
          }

          Basically what I'm trying to get it to do. IS make it ask me to input one letter. So I would put in letter e. Then it would ask me to input another letter, in which I put "r". So on and so fourth. then once I'm done inputting all the letters, it will take every letter, put it into an array, and make them in alphabetical order.
          Last edited by Superfreak04; 10-15-2013, 01:52 PM.

          Comment

          • dAnceguy117
            new hand moves = dab
            FFR Simfile Author
            • Dec 2002
            • 10097

            #6
            Re: Requesting help for programming/ CS majors?

            char i;

            for(i=0; i<12; i++) name[i] = i;
            that's no good haha. you've declared your variable as a char type, but you're assigning int values to it.

            you're looking for something like:

            Code:
            char name[12];
            char c;
            
            for(int i=0; i<12; i++) name[i] = c;
            that's still not everything you need, though, because no value is ever assigned to c.

            Comment

            • Superfreak04
              D7 Elite Keymasher
              • Jan 2007
              • 2407

              #7
              Re: Requesting help for programming/ CS majors?

              I thought characters were referred to as letters? 0_0 So I figured entering in char instead since I'm inputting letters. I guess that doesn't work haha.

              Comment

              • dAnceguy117
                new hand moves = dab
                FFR Simfile Author
                • Dec 2002
                • 10097

                #8
                Re: Requesting help for programming/ CS majors?

                on a simple level, characters are basically your letters, yes. the problem is there in your actual definition of the for loop, not in the code inside the loop. once you've already hit

                Code:
                char i;
                it's invalid to then have

                Code:
                i=0;
                because 0 is an int value, not a char value.

                Comment

                • Superfreak04
                  D7 Elite Keymasher
                  • Jan 2007
                  • 2407

                  #9
                  Re: Requesting help for programming/ CS majors?

                  Originally posted by dAnceguy117
                  on a simple level, characters are basically your letters, yes. the problem is there in your actual definition of the for loop, not in the code inside the loop. once you've already hit

                  Code:
                  char i;
                  it's invalid to then have

                  Code:
                  i=0;
                  because 0 is an int value, not a char value.
                  Ah, that makes a lot more sense now!

                  Comment

                  • megamon88
                    FFR Simfile Author
                    FFR Simfile Author
                    FFR Music Producer
                    • Feb 2006
                    • 2567

                    #10
                    Re: Requesting help for programming/ CS majors?

                    It seems like you're confusing the "for" loop as a sort of "for each" loop like the one in C#. In C++ you have to use a standard "for" loop, iterating over the loop i times.

                    Comment

                    • rushyrulz
                      Digital Dancing!
                      FFR Simfile Author
                      FFR Music Producer
                      • Feb 2006
                      • 12985

                      #11
                      Re: Requesting help for programming/ CS majors?

                      I wrote this in java, shouldn't be too hard to translate into C++. But this takes the name characters into an array until the special character * is input.
                      Code:
                      import java.util.Scanner; //same as iostream, just java version
                      public class namesort
                      {
                          public static void main(String args[])
                          {
                              char[] name = new char[50]; //creating character array
                              int count = 0; //loop control variable
                              System.out.println("Enter the letters of your name one by one and type * when done.");
                      
                              Scanner input = new Scanner(System.in); //invoking iostream
                              while(count < 50) //failsafe so it's not infinite.
                              {
                                  name[count] = input.next().charAt(0); //assign first character of user input to name[count]
                                  if(name[count] == '*') //until * is entered
                                          break; //terminate loop
                                  count++; //iterate loop
                              }
                      
                      }
                      To sort into alphabetical order, you'll need to write an algorithm that interprets the ascii values of each letter and you could probably just bubble sort it.

                      Then counting the letters should be as simple as looping an equality statement through your array and keeping counters.
                      Last edited by rushyrulz; 10-15-2013, 02:16 PM.


                      Comment

                      • Kibblre
                        Caelondia Represent
                        • Jul 2004
                        • 1984

                        #12
                        Re: Requesting help for programming/ CS majors?

                        If you need more direct help, I could help you through Skype possibly. It'll be easier since I can guide you through one step at a time instead of just giving you code.
                        Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                        Comment

                        • Superfreak04
                          D7 Elite Keymasher
                          • Jan 2007
                          • 2407

                          #13
                          Re: Requesting help for programming/ CS majors?

                          Originally posted by Kibblre
                          If you need more direct help, I could help you through Skype possibly. It'll be easier since I can guide you through one step at a time instead of just giving you code.
                          Unfortunately I'm in class atm lol. I won't be able to go onto skype until after 10 PM central time. :/

                          If you post the code now though, it would help. Then I can skype with you once classes are over?

                          Comment

                          • Kibblre
                            Caelondia Represent
                            • Jul 2004
                            • 1984

                            #14
                            Re: Requesting help for programming/ CS majors?

                            I guess. I'll make a section of code that'll work with the first part of your things you're supposed to do.
                            Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                            Comment

                            • Superfreak04
                              D7 Elite Keymasher
                              • Jan 2007
                              • 2407

                              #15
                              Re: Requesting help for programming/ CS majors?

                              Alright, sounds good. Add me on Skype, and I'll accept it once I get home.

                              Comment

                              Working...