### Write a C++ program for two students comparing their grades I need help writing a C++ program to compare between two students grades.
Two students A,B in CSc 102 compared their grades (0 <= <=5) in the first 3 laboratory quizzes:
Grades for A: a1,a2,a3
Grades for B: b1,b2,b3
If a1>b1, then A receives one point;
If a1<b1, then B receives one point;
If a1=b1, then neither student receives a point.
Given the grades of A and B, write a C++ program to print the point totals of the two students.

that's what i got
int main()
{
int x=0, y=0, a, b;
for (int i=0; i<3; i++){
cout <<"Enter Lab" << i+1 << "grade For A";
cin >> a[i];
cout <<"Enter lab" << i+1 << "grade For B";
cin >>b[i];
cout <<endl;
if(a[i]!= b[i]){
if ( a[i]>b[i])
x=x+1;
else
y=y+1;

}
}
cout <<"Grade points of A" << x << endl;
cout <<"Grade points of B" << y << endl;
cout <<"Total points =" << x+y << endl;
return 0;
}
Last edited on Hello basel07,

So what do you have so far? Post any code you have or any ideas you have and we can go from there. Based on your post few will respond because you have shown no attempt to do any work.

Show some effort even if it is wrong.

Andy is the code correct ? Hello basel07,

PLEASE do not edit a message unless it has been just a few minutes or less. On this case it is not that big of a deal, but if the code was posted and changes someone reading this after your edit would not understand what was change and would not be able to see the original code. Better to add a new message so people can see what is different.

I should have mentioned this earlier:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your program is much the same as what I did with two differences.

1) You loaded your arrays with input from the user whereas I initialized the array with test numbers. I was just thinking to far ahead.

2) The if statements are all wrong. Not accurate but all you need is:
 ``1234`` ``````if ( A < B) // point for B. else if (A > B) // point for A. ``````

Nothing is needed for A = B because nothing is awarded to either.

Tomorrow I will explain some things that will improve your programming that you can use in the future. Right now it is hard to focus on what I want to say.

Maybe you missed it the copy and paste, but "#include <iostream>" appears to be missing.

Hope that helps,

Andy Hello basel07,

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````//#define RELEASE // <--- Uncomment when compiled for release. #include #include #include #ifndef RELEASE // <--- Used for writing and testing program. #include // <--- Used for _getch(). You could do something different. Also used for writing and testing program. #endif // !RELEASE void GetNames(std::string& name1, std::string& name2); int main() { constexpr size_t MAXSIZE{ 3 }; // <--- Only need to change one number to affect three places. std::array student1{ 90, 85, 95 }; // <--- Leave empty for run std::array student2{ 89, 87, 94 }; // <--- Leave empty for run std::string name1{ "Andy Gelpi" }, name2{ "Bob Smithe" }; // <--- Leave empty for run. size_t studentPoints1{}, studentPoints2{}; //GetNames(name1, name2); // <--- Uncomment for run. for (size_t lc = 0; lc < MAXSIZE; lc++) { std::cout << "Enter Lab " << lc + 1 << " grade For " << name1 << ": "; // <--- Changed prompt. std::cin >> student1[lc]; std::cout << "Enter lab " << lc + 1 << " grade For " << name2 << ": "; // <--- Changed prompt. std::cin >> student2[lc]; std::cout << std::endl; if (student1[lc] < student2[lc]) studentPoints2++; else if (student1[lc] > student2[lc]) studentPoints1++; } std::cout << "\n " << name1 << " has " << studentPoints1 << " point(s)." << "\n " << name2 << " has " << studentPoints2 << " point(s)." << std::endl; #ifndef RELEASE // <--- Used for writing and testing program. std::cout << "\n\n\n\n Press anykey to continue"; _getch(); #endif // !RELEASE return 0; } //End main void GetNames(std::string& name1, std::string& name2) { std::cout << "\n Enter the name of student 1: "; std::getline(std::cin, name1); std::cout << "\n Enter the name of student 2: "; std::getline(std::cin, name2); }``````

Above main is a prototype of a function.This is not necessary, but the concept will come in handy in the future.If you are not to functions yet what is between the {} of the function can be placed inside main.

The beginning of main is different than yours. It is better to define each variable on one line. this makes things easier to find and to see what you are doing. Line 17 creates the variable "MAXSIZE" which is used in three places. this way you only have to change one number without changing that number everywhere in your program.

Lines 18 an 19 I used a std::array, but a C - style array will work just fine.For working with the program I initialized the array so I would not have to enter numbers each time the program runs.Use this while working with the program and leave the{} blank when you are finished.

The same concept applies to line 20.

The "size_t" is another name for an "unsigned int". I use this when the numbers that will be used are positive and small. Numbers that an "unsigned int" will hold.

The for loop is much the same as what you did although I changed the "cout" statements. Call it my quirk, but I like the way the output looks. The extra spaces read better and the name makes it more personal.

Thinking about the for loop, it works fine the way it is, but I would eigher break it up into two for loops, e.g., one to get the input and one to check the arrays. Or I would probably put each for loop in a function and use main to guide the program flow.

Lastly notice the variable names I used. You might use something similar for the variable names. The idea is that it makes the program easier to read now and especially in six month when come back to see what you did.

Hope that helps,

Andy
Topic archived. No new replies allowed.