Help pls on pancake glutton

Hi, im trying to work on cplusplus beginner exercises but got stuck on this, how can i sort the data along with their respective person .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>

sort();
int a=0,pan[3],temp,p[3]={1,2,3},i=0;
int main()
{
    clrscr();

    int most,least;
      cout<<"Pancake Glutton\n\n";

      for (i=0; i<3; )
       {cout<<"Enter the number eaten by person "<<p[i]<<": ";
	cin>>pan[a];

	i++;
	a++; }

    if (pan[0]>pan[1] && pan[0]>pan[2])
       most=p[0];
    else if (pan[1]>pan[0] && pan[1]>pan[2])
      most=p[1];
    else if (pan[2]>pan[0] && pan[2]>pan[1])
      most=p[2];

    cout<<"\nWho ate the most pancake? Person "<<most; getch();

    if (pan[0]<pan[1] && pan[0]<pan[2])
       least=p[0];
    else if (pan[1]<pan[0] && pan[1]<pan[2])
       least=p[1];
    else if (pan[2]<pan[0] && pan[2]<pan[1])
       least=p[2];

    cout<<"\n\nWho ate the least pancake? Person "<<least;

    getch();
    sort();



getch();
}
    sort(){

	for (int x=0; x<=5; x++)
	{ for (a=0; a<=3; a++)
	 { if (pan[a]<pan[a+1])
	   { temp=pan[a]; pan[a]=pan[a+1]; pan[a+1]=temp; } } }


	cout<<"\n\nTest Sort\n";
	 for(a=0; a<3; a++)
	  cout<<"Person "<<p[i]<<": "<<pan[a]<<endl;
	} 


Output:
(assuming I entered 5,9,8 as inputs)

Person 3: 9
Person 3: 8
Person 3: 5
Hello Thesimch

This program has been done many times, so do a search here and you can find similar code that has been worked out to look at.

Some suggestions:

Put line 7 inside of main and pass what variables that are needed to the functions. Better to stay away from global variables if at all possible.

"main" is missing the "return 0;" before the closing brace.

"clrscr()" may work for you right now, but it does not work for everyone.

In the sort function since "pan" and "p" would e considered parallel arrays you need to sort both at the same time.

You should create function for lest and most pancakes eaten. These functions could either display the results or return an index number for later use.

The programs I have worked on in the past kept track of names and amounts.

For testing purposes you can comment likes 15 - 20 and initialize you variables and arrays with the information you need so that you do not have to type in information every time the program runs and the arrays can be larger this way.

Once I load up your program I will know more.

Hope that helps,

Andy
"main" is missing the "return 0;" before the closing brace.
This is not necessary.
The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.
http://en.cppreference.com/w/cpp/language/main_function
Hello Thesimch,

Initial problems I found before I could even run the program.

There is no file "iostream.h" or "iomanip.h" just "iostream" and "iomanip".

Changed "sort();" to "void sort(int pan[], int p[]);". it was missing the return type an after moving line 7 to inside main the arrays need to be passed to the function. Also had to change the function definition.

Things like "cout",, "cin" and "endl" needed to be qualified with "std::" to work.

Needed to define "temp" inside the "sort" function.

Temp defined in main is not used.

In your for loops you should declare the type of the variable when defined in the first part. If you do this outside the for loop it works, but easy to loose track of what you did ending up with a variable that is used, but not defined. In the last for loop of "sort" "a" did not have a type,"int", and the array "p" used a subscript of "i" which was never defined anywhere i the function.

Hope that helps,

Andy
To find the least/most, it's usually better to keep track of the index of the smallest/largest value. That way if you need the index, you have it:
1
2
3
4
5
6
7
8
9
    most = 0;
    if (pan[1] > pan[most]) {
        most = 1;
    }
    if (pan[2] > pan[most]) {
        most = 2;
    }

    cout << "\nWho ate the most pancake? Person " << most+1 << " ate " << pan[most] << '\n';


Those if statements will get too cumbersome if the array is larger, and it will be downright impossible if you don't know exactly how many entries there are, so it's more common to do it inside a loop:
1
2
3
4
5
6
    most = 0;
    for (i=1; i<3; ++i) {
        if (pan[i] > pan[most]) {
            most = i;
        }
    }

If the number of items changes, you just change the upper bould (3 in this case).

It's more advanced, but there are actually functions in the standard library to find the smallest and largest items in a collection (max_element() and min_element())
Hello Thesimch

The more I look at your program the more I feel that you are not following the instructions correctly.

The if/else if statements may work for now, but what would you have to do if the array size is 50? Both of these would work better in their own functions using a for loop to step through the array to find the min and max and return the index of min or max.

I noticed in the sort function the outer for loop has the condition x <= 5, but the arrays only hold three elements. This will be a problem if it works. If you set up the sort routine differently you could make use of both "x" and "a".

Hope that helps,

Andy

P.S. The use of the global variables may work, but it is not the best way to program. They should be defined in main and passed to functions that need them.
Last edited on
Topic archived. No new replies allowed.