Requesting help for programming/ CS majors?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dAnceguy117
    new hand moves = dab
    FFR Simfile Author
    • Dec 2002
    • 10097

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

    see Rushy's post for a full explanation of how you can do these tasks manually. that's your fail-safe.

    here's some info for a possible C++ specific shortcut for the alphabetizing task. I have no idea how the sort function works haha, but if you get it down, it could save you some time.

    Is it possible to use std::sort defined inside <algorithm> for sorting char arrays according to their ASCII value? If yes, please provide an example.



    and Superfreak, be sure to give yourself time for these assignments! having a "get it done now, learn later" mentality will hurt you in the long run


    edit: actually after staring at that StackOverflow page for a bit I think I get what the parameters for the function are.
    Code:
    char array[] = "zabgqkzg";
    
    std::sort(array, array+sizeof(array));
    once you've got a char array, you can use that sort() function. your arguments inside those parentheses are actually pointers.
    array is a pointer to the start of the array, and
    array+sizeof(array) is a pointer to the first memory address after the array

    I think? lol
    Last edited by dAnceguy117; 10-15-2013, 02:48 PM.

    Comment

    • Superfreak04
      D7 Elite Keymasher
      • Jan 2007
      • 2407

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

      Thanks haha. And yeah, right now I'm trying to convert what rushy said, into C++. Not going very well. :/

      Comment

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

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

        I went ahead and did steps 1 and 2 in java. you should be able to figure out how to count the letter frequency and make the histogram. Here's my java code:
        Code:
        import java.util.Scanner;
        public class namesort
        {
            public static void main(String args[])
            {
                char[] name = new char[50];
                int[] asciiname = new int[50];
                int count = 0;
                System.out.println("Enter the letters of your name one by one and type * when done.");
        
                Scanner input = new Scanner(System.in);
                while(count < 50)
                {
                    char dumbchar = input.next().charAt(0);
                    if(dumbchar == '*')
                        break;
                    else
                    {
                        name[count] = dumbchar;
                        count++;
                    }
                }
                //cast char to int(ASCII)
                for(int i = 0; i < name.length; i++)
                {
                    asciiname[i] = (int)name[i];
                }
        
                //bubble sort
                for(int i = 0; i< asciiname.length-1; i++)
                {
                    for(int j = 0; j<asciiname.length-1; j++)
                    {
                        if(asciiname[j+1] < asciiname[j])
                        {
                            //swap elements
                            int dummy = asciiname[j];
                            asciiname[j] = asciiname[j+1];
                            asciiname[j+1] = dummy;
                        }
                    }
                }
                //cast int(ASCII) to char(ANSI).
                for(int i = 0; i < asciiname.length; i++)
                {
                    name[i] = (char)asciiname[i];
                }
        
                for(int i=0; i<name.length; i++)
                {
                    System.out.print(name[i]);
                }
            }
        }


        Comment

        • Kibblre
          Caelondia Represent
          • Jul 2004
          • 1984

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

          Yeah, I didn't notice rushy's post. His will work perfectly fine if you just change the Java functions to C++ functions like he said.
          Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

          Comment

          • Superfreak04
            D7 Elite Keymasher
            • Jan 2007
            • 2407

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

            Originally posted by Kibblre
            Yeah, I didn't notice rushy's post. His will work perfectly fine if you just change the Java functions to C++ functions like he said.
            Yeah I'm trying lol. Right now I'm mainly focused on step one.

            Comment

            • Kibblre
              Caelondia Represent
              • Jul 2004
              • 1984

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

              By the way, do you HAVE to use arrays or can you use vectors? I personally prefer vectors.

              EDIT: I totally missed this part: Ask the user to entire his/her name one letter at a time.
              Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

              Comment

              • Superfreak04
                D7 Elite Keymasher
                • Jan 2007
                • 2407

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

                We have to use arrays.

                Comment

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

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

                  you might need to use another include statement to make the .length operator (or C++ equivalent) work, I know C style languages are kinda bitches when it comes to that. Just google syntactical crap that is confusing you.


                  Comment

                  • Kibblre
                    Caelondia Represent
                    • Jul 2004
                    • 1984

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

                    In case you hadn't been able to translate rushy's code, here's what part one could look like:

                    Code:
                    #include <iostream>
                    using namespace std;
                    
                    int main()
                    {
                    	char name[50];
                    	int count = 0;
                    	bool valid = true;
                    	char inputChar; 
                    
                    	cout << "Enter a name one letter at a time and denote the end of the name with *: ";
                    
                    	while(valid)
                    	{
                    		cin >> inputChar;
                    		if(inputChar != '*')
                    		{
                    			name[count] = inputChar;
                    			count++;
                    		}
                    		else
                    			valid = false;
                    	}
                    
                    	system("pause");
                    	return 0;
                    }
                    Originally posted by rushyrulz
                    you might need to use another include statement to make the .length operator (or C++ equivalent) work, I know C style languages are kinda bitches when it comes to that. Just google syntactical crap that is confusing you.
                    Finding the size of a char array in C++ is sizeof(name).

                    Also, you might want to look into including the * as well for your later parts so you know to stop sorting there. Or make a new array using the count variable, but you'd have to make the new char array a pointer since you can't declare variable size char arrays unless you do.
                    Last edited by Kibblre; 10-15-2013, 03:00 PM.
                    Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                    Comment

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

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

                      damn, C++ is so much nicer at handling IO than other languages I've programmed in..
                      Do you know if there's any way to restrict input to just one character, Kibblre?


                      Comment

                      • shenjoku
                        Wubalubadubdub
                        • May 2005
                        • 1697

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

                        lol bubble sort.

                        *ahem*

                        If you want to let STL do the sorting for you it's very simple. Just call std::sort() like this:

                        Code:
                        std::sort(name, name + sizeof name);
                        The sizeof name part only works if you're using an array like you currently are. If you ever want to change to dynamically allocate the array so you aren't limited to some hard-coded number of characters then you would replace that part with whatever the calculated size of the array is (which I guess you'd do based on the user input). Or you could just replace it with 12 or whatever other hard-coded size you change to. Up to you

                        You might want to consider looking up how std::sort() works and try to implement it yourself. It's always good to know how the internal stuff works.

                        It's a shame you're limited to using an array. If you could use std::string you could just do a single
                        Code:
                        std::string name; std::cin >> name;
                        to get the entire name. Oh well. Gotta learn how to use arrays at some point I suppose
                        Last edited by shenjoku; 10-15-2013, 03:18 PM.
                        boop

                        Comment

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

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

                          would you prefer a merge sort instead? it's nlog(n) instead of nē


                          Comment

                          • Kibblre
                            Caelondia Represent
                            • Jul 2004
                            • 1984

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

                            Originally posted by rushyrulz
                            damn, C++ is so much nicer at handling IO than other languages I've programmed in..
                            Do you know if there's any way to restrict input to just one character, Kibblre?
                            I used Java more than C++, so I don't know if there's a direct IO function to handle it. You could use getline() to take in the whole line and then use substr() to cut it down to the first character only and finally cast it as a char.
                            Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                            Comment

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

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

                              yikes. I'd hate to have the program explode because someone was typing too fast and accidentally entered a two character input.
                              t
                              h
                              i
                              s
                              i
                              s
                              s
                              o
                              s
                              t
                              u
                              p
                              i
                              d
                              w
                              h
                              y
                              a
                              m
                              i
                              d
                              o
                              i
                              n
                              g
                              t
                              hi *EXPLOSION*
                              s


                              Comment

                              • Kibblre
                                Caelondia Represent
                                • Jul 2004
                                • 1984

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

                                It wouldn't explode. It would actually read it/print it later as hi.

                                Enter a name one letter at a time and denote the end of the name with *: he
                                l
                                l
                                o
                                *
                                (print loop went here, printing 1 char at a time)
                                he
                                l
                                l
                                o
                                Press any key to continue . . .

                                EDIT: Actually, it indexes it correctly. h is index 0 and e is index 1. Really odd why it prints like that then.
                                Last edited by Kibblre; 10-15-2013, 03:10 PM.
                                Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                                Comment

                                Working...