Beginner Exercises: evaluation

I'm new to C++ and I've just solved a few beginner's exercises (found in http://www.cplusplus.com/forum/articles/12974/page9.html). If anyone has some time maybe they could review it? I didn't want to post all this on the actual thread as it would take up a lot of space in there. Also, I would like to point out that I know using system("PAUSE") isn't good, but I just think it's enough for me right now. So, here goes:

Grading program:
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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int grade;
    cout<<"Enter your grade\n";
    while(1)
    {
            cin>>grade;
            if (grade>-1&&grade<101)
            {
               if (grade==100) 
                  cout<<"You've got a perfect score!\n";
		  else if (grade>89)
		  cout<<"You've got an A\n";
		  else if (grade>79)
		  cout<<"You've got a B\n";
                  else if (grade>69)
		  cout<<"You've got a C\n";
                  else if (grade>59)
		  cout<<"You've got a D\n";
               else cout<<"You suck. F.\n";
               break;
            }                       
            else cout<<"You probably got an F to enter that. Enter again.\n";
    }        
    system("PAUSE");
    return 0;
}




Cola machine:

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int choice;
    
    cout<<"Cola - 1\n";
    cout<<"Sprite - 2\n";
    cout<<"Fanta - 3\n";
    cout<<"Water - 4\n";
    cout<<"Some mysterious drink - 5\n\n";
    
    cin>>choice;
    
    switch (choice)
    {
           case 1:
                cout<<"You choose cola.\n"; break;
           case 2:
                cout<<"You choose sprite.\n"; break;
           case 3:
                cout<<"You choose fanta.\n"; break;
           case 4:
                cout<<"You choose water.\n"; break;
           case 5:
                cout<<"You chose some mysterious drink. Good luck!\n"; break;
           default:
                cout<<"Error, you're too dumb to choose. Take your money and go away.\n"; break;
    }       
    system("PAUSE");
    return EXIT_SUCCESS;
}





While( user == gullible ):

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int z;
    for (int i=0;i<10;i++)
    {
        cout<<"Enter a number other than "<<i<<"\n";
        cin>>z;
        if (i==z)
        {
                 cout<<"I asked you enter not "<<i<<", you lose.\n";
                 system("PAUSE");
                 return 0;
/* Here I terminate the program if the user entered the number they shouldn't
have because I didn't think of other way to skip the output sentence in the
end*/
        }
    }
    cout<<"You're more patient than me, you win...\n";
    system("PAUSE");
    return 0;
}




Pancake Glutton:
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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{   
    int person[9], max=0, min=0;
    
    cout<<"Enter, how many pancakes 10 different people ate:\n";
    for (int i=0;i<10;i++)
        cin>>person[i];
        
    for (int i=1;i<10;i++)
    /*I start the cycle with 1 because I don't think I need to compare person[0] with person[0]*/ 
    {
        if (person[max]<person[i])
           max=i;
        if (person[min]>person[i])
           min=i;
    }
    cout<<"Person "<<max+1<<" ate the most pancakes ("<<person[max]<<").\n";
    cout<<"Person "<<min+1<<" ate the least pancakes ("<<person[min]<<").\n\n";
    /*I use max+1 and min+1 because I want people to be numbered 1 to 10, rather than 0 to 9*/
    
    /*Third modification: it sorts all the people from the one who ate the most, to the one who ate the least*/
    for (int i=0;i<10;i++)
    {
    /*Here the program looks for the biggest number left and memorizes it...*/
        max=i;
        for (int n=0;n<10;n++) 
            if (person[max]<person[n])
               max=n;
    /*Then it prints out the results...*/
        cout<<"Person "<<max+1<<" ate "<<person[max]<<" pancakes.\n";
    /*And eliminates that pancake, so it won't be used again*/
        person[max]=-1;                 
    }
    system("PAUSE");
    return 0;
}




All of these work the way they should, but I just wanted that some experienced programmer corrected mistakes which, for example, slow the program down or something like that. Because I want to program the good way from the beginning, so that I wouldn't have any bad habits later.

Thanks in advance.
Last edited on
The first 3 look good.

The fourth solution could be made simpler.

You mean the calculations could be made in some other way? Because that's pretty much the only one I thought of. Well, there are a lot of variations to do the same thing, but it's still the same thing, I guess.

Though now that I think, there might be a way to simple things up, but I don't have the knowledge to do that yet. For an example - the part, where I eliminate the person, whose "score" is already printed. I don't really like it that I have to do that in the way I did, but I just didn't think of another way how I could make a variable from an array no longer considerable when the cycle is repeated the second time and so on (I hope you understand what I mean, because it was hard to describe what I meant :D).

And, before I forgot, - thanks for reviewing them.


P.S. Don't be mad at me that it's hard to understand what I'm trying to say, English is not my native language.
For pancake glutton, work on sorting the list after user input. Once the list is sorted, your max, min, and sorted output are trivial.
I'm sorry, jRaskel, but I don't understand what you mean. Are you trying to say, that I should sort the list out just after user input? Why? I'm sorry for my bad English.
I would love to see a professionals answer to pancake glutton because i cant get my head round it. Spent a fair few hours but cant seem to make it work. Troubles with the following:

1) Making it display which persons pancake your entering the number of (example "Enter person (person number)'s number of pancakes eaten)

2) Storing that number

3) Getting it done in less than 40 lines because (according to jsmith) the solution Disblader came up with is not the best way. I cant see how to do it never mind make it even more efficient.

I really want to figure this out for myself but ive read 3 books so far on arrays, for loops, if statements etc etc and havent figured it out (just dont really have a problem solving brain it seems) so to put me out of my misery, someone please post one of the most efficient ways to do this. Maybe, just maybe i will learn something.
closed account (S6k9GNh0)
For reading purposes, in the first example, a more readable solution on the if statement would be if (grade >= 0 && grade <= 100) although there isn't a difference in what actually happens.
Why not use the std::sort function defined in #include <algorithm> .
http://www.cplusplus.com/reference/algorithm/sort/
Last edited on
Yeah i was thinking of using that with a vector (really dont know how to use it with an array), would this be the "more efficient" way? I stopped last night and walked away from it to clear my head, then went to bed reading stroustrops book and came across the section on vetors and sorting them etc.

Just out of interest, why use arrays at all. I know there are different storage types for different tasks but an array just seems like an ungrowable vector, so is there any reason to ever use an array? Or will vectors fit the job just as well if not better?

I feel im on the brink of cracking this which is good, and yet i would still like to see whats deemed as the best solution for it. That way i can take a quick peek once ive made mine (or not got it to work) and hopefully it will inspire me beyond this 'programmers block' phase. Maybe i need more coffee.
Last edited on
Somarl: You can sort anything that has a "pointer-like" interface (an iterator or a real pointer perhaps).

Here is a solution with arrays, in less than 40 lines. Even with (slack) error handling and so on.

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
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;

struct PersonInfo {
    int id;
    int pancakes;
    static bool lesser(const PersonInfo &p1, const PersonInfo &p2) {
        return (p1.pancakes < p2.pancakes);
    }
};        
    
int main()
{
    const int kCountPerson = 10;
    PersonInfo person[kCountPerson];
    
    for (int i = 0; i < kCountPerson; i++) {
        person[i].id = i+1;
        cout << "Pancakes ate by person #" << i+1 << "\n";
        while (!(cin >> person[i].pancakes)) {
            cout << "Try again!\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    
    sort(person, person + kCountPerson, PersonInfo::lesser);    
    cout << "Max: #" << person[kCountPerson-1].id << " with "
        << person[kCountPerson-1].pancakes << " pancakes.\n";
    cout << "Min: #" << person[0].id << " with "
        << person[0].pancakes << " pancakes.\n";
    for (int i = 0; i < kCountPerson; i++) {
        cout << "Person: #" << person[i].id << " with "
        << person[i].pancakes << " pancakes.\n";
    }
}
Topic archived. No new replies allowed.