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:
#include <cstdlib>
#include <iostream>
usingnamespace 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";
elseif (grade>89)
cout<<"You've got an A\n";
elseif (grade>79)
cout<<"You've got a B\n";
elseif (grade>69)
cout<<"You've got a C\n";
elseif (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;
}
#include <cstdlib>
#include <iostream>
usingnamespace 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;
}
#include <cstdlib>
#include <iostream>
usingnamespace 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.
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.
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.
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.
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.