My C++ test is COMING!!...

This is a discussion on My C++ test is COMING!!... within the C++ Programming forums, part of the General Programming Boards category; Hey guys. I will be having my C++ open book test on the 5th of December . Luckily its open ...

  1. #1
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37

    Exclamation My C++ test is COMING!!...

    Hey guys. I will be having my C++ open book test on the 5th of December. Luckily its open book. So I need advice from you guys. Any kind advices.

    Also I'm very sure that arrays will be included, because the last topic that we learnt was arrays and recently we had a mock C++ test which included arrays. And so far, when it comes to arrays, I'm totally clueless. The reason is because my lecturer is useless in teaching. He's is just as good as, "Here we will be learning arrays. The end. Learn everything yourself." But of course he does try to teach, but nobody in class knows whats going on.

    So I've have found a few good web sites on C++ and have a book on C++, but I'm only starting to learn arrays. I have the simple basic for C++, but still, I have no idea on how to use arrays. So while I'm trying to learn it, I taught that may be I could post here and ask for advices and a bit of lessons.

    Could anyone give me a simple example of a code using arrays? How about, arranging numbers from smallest to biggest?

    I will scan my mock test and post it here later, so that you guys have a better Idea of the test. And since its an open book test, we are allowed to have notes during the test, whether on paper or even on the internet.

    So would it be okay if I log on this site during the test? But no cheating of course... May be to help me out a bit?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    How about, arranging numbers from smallest to biggest?
    You could search the Web concerning sorting algorithms. Sorting Algorithms
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    I think that if he can't understand arrays at this point, then sorting algorithms may be a little beyond his current abilities.

    For arrays, it's really quite simple if you understand variables. All that an array is is a variable. so what makes an array different, you ask? Well, for one thing, arrays are more than one variable.

    Here's the example that I've seen used numerous times when describing arrays: Suppose that you have six numbers. Each one is the id of an object. So we'd have
    Code:
    int id1, id2, id3, id4, id5, id6;
    id1 = 0;
    id2 = 1;
    id3 = 5;
    id4 = 7;
    id5 = 2;
    id6 = 4;
    Now, as you can see, all of these numbers are related, yet they're all seperate variables. But if you use an array, you can do this:

    Code:
    int id[6];
    id[0] = 0;
    id[1] = 1;
    id[2] = 5;
    id[3] = 7;
    id[4] = 2;
    id[5] = 4;
    the [n] area of each array is called it's 'indice'. Note that arrays start at an indice zero; as such, when you declare an array of length 6 (by going int id[6]), you end up with an array with indices from zero to five.

    What's the point of this? At first, there doesn't seem to be much of any. However, there are several uses, one of the most common uses is that arrays and for loops are built for each other. Instead of having to do:

    Code:
    cout << id1 << "\n";
    cout << id2 << "\n";
    coud << id3 << "\n"
    ...
    you could do

    Code:
    for(int i = 0; i < 6; i++)
    cout << id[i] << "\n";

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There's a tutorial on arrays, I don't know if you've seen it: http://www.cprogramming.com/tutorial/lesson8.html

    Basically, an array is a convenient name for many different variables as the previous poster has indicated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Thanks alot guys,.... I've been reading on this a lot for the past don't know how many hours.
    I mannage to understand a bit.

    laserlight, I went through that site but It was kind of hard to understand completely. About the Bubble sort code,
    Code:
    void bubbleSort(int numbers[], int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    }
    How will the output turn out?


    therabidwombat: Thanks a lot! I mannaged to understand the basic already.

    But what I noticed in the lecture slides provided by the lecturer is that all the values in the arrays are all fixed. For example
    Code:
    const int responseSize = 99;
    	response[ responseSize ] = 
    		{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    		7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    		7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    		7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    		5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    		7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
    		7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    		4, 5, 6, 1, 6, 5, 7, 8, 7 };
    I understand that all these numbers becomes the values stored in the arrays, but how do we input our own values to be stored? I can't seem to get any info on that.



    There is more which I don't understand. Well actually the code starts with this:
    Code:
    #include <iostream.h>
    #include<iomanip.h>
    
    
    void mean(const int[], int);
    void median(int[], int)
    void mode(int[], int[], int)
    void bubbleSort(const int[], int);
    
    int main()
    {
    
    	const int responseSize = 99;
    		response[ responseSize ] = 	
    			{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    			7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    			7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    			7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    			5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    			7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
    			7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    			4, 5, 6, 1, 6, 5, 7, 8, 7 };
    
    	mean( response, responseSize );
    	median( response, responseSize );
    	mode( frequency, response, responseSize );
    
    	return 0;
    }
    
    ...
    ...
    then it goes on to defining mean then median...
    here's the code for defining median:
    Code:
    void median( int answer[], int size )
    {
    	cout << "\n********\n Median\n********\n"
    		<< "The unsorted array of responses is";
    	printArray( answer, size );
    	bubbleSort( answer, size );
    	cout << "\n\nThe sorted array is";
    	printArray( answer, size );
    	cout << "\n\nThe median is element " << size / 2
    		<< " of\nthe sorted " << size 
    		<< " element array.\nFor this run the median is "
    		<< answer[ size / 2 ] << "\n\n";
    and the output for the median is:
    Code:
    ********
     Median
    ********
    The unsorted array of responses is
     6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
     6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
     6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
     5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
     7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7
     
    The sorted array is
     1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5
     5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7
     7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8
     8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
     9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
     
    The median is element 49 of
    the sorted 99 element array.
    For this run the median is 7
    What I don't understand is how did they values get all sorted up. I noticed that the sorting was done by this part of the code:
    Code:
    printArray( answer, size );
    But how? How was answer and size defined?


    and, to dwks: Yeah I did check that out. The explanation seems to be too general and less technical. Thanks anyway.

  6. #6
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Wait, wait....

    May be I got it. Is this how you input the values?
    Code:
    int main();
    {
    	int grades[10]
    
    	for (int i = 0; i < 4; 4; i++)
    	{
    		cout << "Enter 10 grades: ";
    		cin >> grades[i];
    	}
    Did I get it right?

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Your for() loop is wrong, it should read for(int i = 0; i < 10; i++). There also lacks a closing brace and you have an extra semi-colon after int main(). Oh, and you lack a semi-colon after int grades[10];

  8. #8
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Oh yeah... mistakes. Thanks. So it should be:
    Code:
    int main();
    {
    	int grades[10];
    
    	for(int i = 0; i < 10; i++);
    	{
    		cout << "Enter 10 grades: ";
    		cin >> grades[i];
    	}
    ...
    ...
    ...
    
    	return 0;
    }
    So, about the median thingy... How did they get sorted? I don't get it...

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    >int main();

    Still got a semi-colon here
    I'm just trying to be a better person - My Name Is Earl

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by [Z-D]
    What I don't understand is how did they values get all sorted up. I noticed that the sorting was done by this part of the code:
    Code:
    printArray( answer, size );
    But how? How was answer and size defined?
    Wrong. It is done by the bubbleSort() function. The median() function simply shouldn't be there... really.. It hides the whole purpose of the bubble sort. It first displays the unsorted array, then calls bubbleSort() which sorts the array and then displays the now-sorted array. This is your typical before-after comparison.

    If you are wondering how the bubble sort works well you can take a sheet of paper and a pencil and work by hand what it's doing. You'll learn a lot more. I find it's a twisted version of the bubble sort, though. The way I know it is much simpler. Whatever.

    Code:
    void bubbleSort(int numbers[], int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    }
    This basically means this:
    Code:
    for ( all entries in the array, going backwards )
    {
      for ( all the entries from 1 to i )
      {
        if( the entry at the location j - 1 ( 0, 1 , 2 ... i ) is greater than the entry next to it)
        {
          switch the values
        }
      }
    }
    Basically, what it does is simple. You start by going through all the numbers from the beginning to end and switch the entries so that the biggest number becomes last in the array. Then, 'i' decreases so you don't have to check the last number because you know for sure that it is the biggest in the array. The reason why 'j' is set to 1 in the second for() loop is because when you have done n - 1 iterations, the last number not sorted is surely the smallest in the array, thus you don't need to proceed further. However, the code has something weird. The first for() loops until i is equal to 0 and then it finishes... and the second says "execute if j is <= i" ... however, j is set to 1 in the first place. Therefore, when i will equal 0, the first for() loop will be executed, but the second will not. This is no biggie. No bug, no leak, no blue-screen-of-death. Just useless. It could be fixed by simply changing "i >= 0" to "i >= 1".

    Edit: Just in case you're visual:

    This is the list to sort:

    4 7 1 5 3

    Here's what happens

    Code:
      j     i
    4 7 1 5 3
    
    You always have to j - 1 to j and switch if j - 1 is greater than j
    
        j   i
    4 7 1 5 3
    
          j i
    4 1 7 5 3
    
            ji (both on 3)
    4 1 5 7 3
    
      j   i
    4 1 5 3 7
    
        j i
    1 4 5 3 7
    
          ji (both on 3)
    1 4 5 3 7
    
      j i
    1 4 3 5 7
    
        ji (both on 3)
    1 4 3 5 7
    
    1 3 4 5 7
    Hope this helps you.
    Last edited by Desolation; 11-25-2006 at 09:05 PM.

  11. #11
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Quote Originally Posted by swgh
    >int main();
    Oh darn.... Yeah. Got that, thanks.
    Quote Originally Posted by Desolation
    Wrong. It is done by the bubbleSort() function. The median() function simply shouldn't be there... really.. It hides the whole purpose of the bubble sort. It first displays the unsorted array, then calls bubbleSort() which sorts the array and then displays the now-sorted array. This is your typical before-after comparison.
    Ummm... What's wrong? The code given is wrong?... Those are from my lecturer. I guess he's worst than I taught.

    Quote Originally Posted by Desolations
    However, the code has something weird. The first for() loops until i is equal to 0 and then it finishes... and the second says "execute if j is <= i" ... however, j is set to 1 in the first place. Therefore, when i will equal 0, the first for() loop will be executed, but the second will not. This is no biggie. No bug, no leak, no blue-screen-of-death. Just useless. It could be fixed by simply changing "i >= 0" to "i >= 1".
    Thanks... If it is fixed to...
    Code:
    i>=1
    ...does that mean that the process will stop at i = 0? What if I change "j = 1" to "j = 0" instead? Will this also work?

    So if I do this,
    Code:
    	for (i = (array_size -1); i >=0; i--)
    	{
    		for (j = 0; j <=i; i++)
    Instead of stopping at i = 1, the sorting will stop when i = 0... Am I right?

    Quote Originally Posted by Desolation
    Edit: Just in case you're visual:

    This is the list to sort:

    4 7 1 5 3

    Here's what happens

    Code:
      j     i
    4 7 1 5 3
    
    You always have to j - 1 to j and switch if j - 1 is greater than j
    
        j   i
    4 7 1 5 3
    
          j i
    4 1 7 5 3
    
            ji (both on 3)
    4 1 5 7 3
    
      j   i
    4 1 5 3 7
    
        j i
    1 4 5 3 7
    
          ji (both on 3)
    1 4 5 3 7
    
      j i
    1 4 3 5 7
    
        ji (both on 3)
    1 4 3 5 7
    
    1 3 4 5 7
    Hope this helps you.
    Ohhh hell,.... Yeah! That helped a lot! Thanks a lot.



    So will this work?
    Code:
    int main()
    {
    	int numbers[10];
    
    	for (int i = 0; i < 10; i++);
    	{
    		cout << "Enter 10 grades: ";
    		cin >> numbers[i];
    		cout << "Your sorted grades are: ";
    
    	bubbleSort(int numbers[], int array_size);
    	}
    
    	return 0;
    }
    
    void bubbleSort{int numbers[], int array_size)
    {
    	int i, j, grades;
    
    	for (i = (array_size -1); i >=0; i--)
    	{
    		for (j = 0; j <=i; i++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i]
    				numbers[j-1] = numbers [j];
    				number[j] = grades;
    			}
    		}
    	}
    
    }
    Sorry if this code bothers you with the amound of mistakes, if there is. I'm still kind of blur you know!...

    This is the output that I'm trying acheive:
    Code:
    Out put:
    Enter 10 grades: 15, 24, 12, 99, 58, 47, 26, 45, 87, 55 <---- then we enter grades here
    Your sotrt grades are: 12, 15, 24, 26, 45, 47, 55, 58, 87, 99   <----- this is the sorted one
    But there's something I need to know. I want these values to be equals to more than 0 and equals to less than 100.
    Last edited by [Z-D]; 11-26-2006 at 02:10 AM.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. You were wrong when you said the sorting was done by printArray().
    2. No, changing j = 1 to j = 0 will crash your program. Try to find why, as an exercise.
    3. Not changing i >= 0 will simply leave you with an useless for() loop executed. You have to change it to i >= 1 to get rid of it because then when i equals 1, the second for() loop will get executed as well (since it is executed for j <= i).

  13. #13
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Okay then. But how do we display the arrange set of numbers in the output?

    And supposing I want the programme to be able to input 10 numbers(or grades) then arrange and output the numbers. How do set it so that "i" must be between 0 and 100? Meaning equals or more than 0 and equals or less than 100.

    Was my code in my previous code correct? I couldn't seem to get it right.




    Okay so this is from the lecturers notes...
    Code:
    #include <iostream.h>
    #include<iomanip.h>
    
    
    void mean(const int[], int);
    void median(int[], int)
    void mode(int[], int[], int)
    void bubbleSort(const int[], int);
    
    int main()
    {
    
    	const int responseSize = 99;
    		response[ responseSize ] = 	
    			{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    			7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    			7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    			7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    			5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    			7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
    			7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    			4, 5, 6, 1, 6, 5, 7, 8, 7 };
    
    	mean( response, responseSize );
    	median( response, responseSize );
    	mode( frequency, response, responseSize );
    
    	return 0;
    }
    
    void mean( const int answer[], int arraySize )
    {
    	int total = 0;
    
    	cout << "********\n  Mean\n********\n";
    
    	for ( int j = 0; j < arraySize; j++ )
    		total += answer[ j ];
    
    	cout << "The mean is the average value of the data\n"
    		<< "items. The mean is equal to the total of\n"
    		<< "all the data items divided by the number\n"
    		<< "of data items (" << arraySize 
    		<< "). The mean value for\nthis run is: " 
    		<< total << " / " << arraySize << " = "
    		<< setiosflags( ios::fixed | ios::showpoint )
    		<< setprecision( 4 )
    		<< static_cast< double >( total ) / arraySize << "\n\n";
    }
    
    
    void median( int answer[], int size )
    {
    	cout << "\n********\n Median\n********\n"
    		<< "The unsorted array of responses is";
    	printArray( answer, size );
    	bubbleSort( answer, size );
    	cout << "\n\nThe sorted array is";
    	printArray( answer, size );
    	cout << "\n\nThe median is element " << size / 2
    		<< " of\nthe sorted " << size 
    		<< " element array.\nFor this run the median is "
    		<< answer[ size / 2 ] << "\n\n";
    }
    
    
    	void mode( int freq[], int answer[], int size )
    {
    	cout << "\n********\n  Mode\n********\n";
    
    	int rating, largest = 0, modeValue = 0;
    
    	for ( rating = 1; rating <= 9; rating++ )
    		freq[ rating ] = 0;
    
    	for ( int j = 0; j < size; j++ )
    		++freq[ answer[ j ] ];
    
    	cout << "Response"<< setw( 11 ) << "Frequency"
    		<< setw( 19 ) << "Histogram\n\n" << setw( 55 )
    		<< "1    1    2    2\n" << setw( 56 )
    		<< "5    0    5    0    5\n\n";
    
    	for ( rating = 1; rating <= 9; rating++ )
    
    	cout << setw( 8 ) << rating << setw( 11 )
    		<< freq[ rating ] << "          ";
    		if ( freq[ rating ] > largest )
    		{
    			largest = freq[ rating ];
    			modeValue = rating;
    		}
    
    		for ( int h = 1; h <= freq[ rating ]; h++ )
    			cout << '*';
    			cout << '\n';
    	}
    	cout << "The mode is the most frequent value.\n"
    		<< "For this run the mode is " << modeValue
    		<< " which occurred " << largest << " times." << endl;
    }
    
    
    void bubbleSort( int a[], int size )
    {
    	int hold;
    	for ( int pass = 1; pass < size; pass++ )
    
    		for ( int j = 0; j < size - 1; j++ )
    
    			if ( a[ j ] > a[ j + 1 ] )
    			{
    				hold = a[ j ];
    				a[ j ] = a[ j + 1 ];
    
    				a[ j + 1 ] = hold;
    			}
    }
    
    
    void printArray( const int a[], int size )
    {
    	for ( int j = 0; j < size; j++ )
    	{
    		if ( j % 20 == 0 )
    			cout << endl;
    			cout << setw( 2 ) << a[ j ];
    	}
    }
    And the output is suppose to be:
    Code:
    //Output:
    ********
      Mean
    ********
    The mean is the average value of the data
    items. The mean is equal to the total of
    all the data items divided by the number
    of data items (99). The mean value for
    this run is: 681 / 99 = 6.8788
     
     
    ********
     Median
    ********
    The unsorted array of responses is
     6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
     6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
     6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
     5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
     7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7
    
    The sorted array is
     1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5
     5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7
     7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8
     8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
     9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
     
    The median is element 49 of
    the sorted 99 element array.
    For this run the median is 7
    ********
      Mode
    ********
    Response  Frequency        Histogram
     
                                          1    1    2    2
                                     5    0    5    0    5
     
           1          1          *
           2          3          ***
           3          4          ****
           4          5          *****
           5          8          ********
           6          9          *********
           7         23          ***********************
           8         27          ***************************
           9         19          *******************
    The mode is the most frequent value.
    For this run the mode is 8 which occurred 27 times.
    ...and when I tested it, it didn't work.
    What are the mistakes in this code? Also, I'm tyring to understand it, but there is some part which I have difficulty understanding. What I'm interested in is how the the numbers got sorted. Also I don't understand almost the whole part of defining mean.

    I'm trying to do something like this, where the programme can sort the numbers, but the numbers are input-ed instead of given.



    This is what I came up with so far. I'm just trying to sort the inputed numbers here. Would this work.
    Code:
    #include <iostream.h>
    #include<iomanip.h>
    
    void bubbleSort(int numbers[], int array_size);			//initialise arrays
    
    int main(void)							//call fucntion
    {
    	int numbers[10];
    
    	for (int i = 1; i < 10; i++);
    	{
    		cout << "Enter 10 grades: ";
    		cin >> numbers[i];
    		cout << "Your sorted grades are: ";
    
    	bubbleSort(int numbers[], int array_size);
    	}
    
    	return 0;
    }
    
    void bubbleSort(int numbers[], int array_size)			//defining bubleSort
    {
    	int i, j, grades;
    
    	for (i = (array_size -1); i >=1; i--)
    	{
    		for (j = 1; j <=i; i++)	
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i]
    				numbers[j-1] = numbers [j];
    				number[j] = grades;
    			}
    		}
    	}
    
    }
    Last edited by [Z-D]; 11-27-2006 at 09:51 AM.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>What are the mistakes in this code?

    After a quick look through, there should be a { between the following two lines:
    Code:
    for ( rating = 1; rating <= 9; rating++ )
    
    	cout << setw( 8 ) << rating << setw( 11 )
    I'd prefer this:
    Code:
    for ( int h = 1; h <= freq[ rating ]; h++ )
    	cout << '*';
    	cout << '\n';
    look like this:
    Code:
    for ( int h = 1; h <= freq[ rating ]; h++ )
          cout << '*';
    cout << '\n';
    to be more clear in the intention, but it's not wrong as posted and the indentation may be a problem with the board and not the author.


    >>Would this work

    Run it and find out. The big problem I see is you are sorting with every input, which is time intensive. Moving these two lines out of the loop may be better.

    cout << "Your sorted grades are: ";
    bubbleSort(int numbers[], int array_size);
    You're only born perfect.

  15. #15
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Quote Originally Posted by elad
    >>Would this work

    Run it and find out.
    Well,.... he, he,..... the thing is that... ummmm.... I don't have a compiler at home. I need to test it in my Com Lab.

    I better buy one.
    Quote Originally Posted by elad
    The big problem I see is you are sorting with every input, which is time intensive.
    What do you mean by sorting ever input?... You mean this part?
    Code:
    void bubbleSort(int numbers[], int array_size)			//defining bubleSort
    {
    	int i, j, grades;
    
    	for (i = (array_size -1); i >=1; i--)
    	{
    		for (j = 1; j <=i; i++)	
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i]
    				numbers[j-1] = numbers [j];
    				number[j] = grades;
    			}
    		}
    	}
    
    }
    Quote Originally Posted by elad
    Moving these two lines out of the loop may be better.

    cout << "Your sorted grades are: ";
    bubbleSort(int numbers[], int array_size);
    You mean like this?
    Code:
    int main(void)							//call fucntion
    {
    	int numbers[10], i, j;
    
    
    
    	for (int i = 1; i < 10; i++);
    	{
    		cout << "Enter 10 grades: ";
    		cin >> numbers[i];
    	}
    		cout << "Your sorted grades are: ";
    
    	bubbleSort(int numbers[], int array_size);
    
    	return 0;
    }

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 02:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 06:28 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 03:57 PM
  5. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21