Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have tried and tried to figure out what is wrong in this code and nothing is working, can someone please help? the code is down below.

C++
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare array
	double scores[5][3] = {{75.5, 80.5, 0.0}, 
						   {88.5, 89.5, 0.0},
	 					   {63.0, 54.0, 0.0}, 
						   {100.0, 99.0, 0.0}, 
						   {88.5, 88.5, 0.0}};
  
	//calculate and assign average
	for (int row = 0; row < 5; row += 1)
		scores[row][0] = scores[row][0] + scores[row][1] / 2;
	//end for

	//display averages stored in the array
	cout << fixed << setprecision(1);
	for (int row = 0; row < 5; row += 1)
		cout << "Student " << row + 1 << " average: " 
			<< scores[row][2] << endl;
	//end for

	//system("pause");
    return 0;
}   //end of main function


What I have tried:

Ive tried several corrections to debug this code but nothing is working.
Posted
Updated 9-Apr-16 4:59am
v2
Comments
Sergey Alexandrovich Kryukov 8-Apr-16 16:59pm    
Did you read you own code after posting? Did you pay attention that first two lines are
#include
#include..?

I understand, this is merely because you screwed up HTML formatting, but who do you think needs to care about presenting exactly the same code as the code you have problem with?

It makes no sense to read the rest of your post before you use "Improve question" and format everything humanely.
Thank you for understanding.

—SA
Patrice T 8-Apr-16 17:02pm    
The problem is that the OP did not formatted its code.
Sergey Alexandrovich Kryukov 8-Apr-16 17:35pm    
Sure; and thank you for helping this member, but my point is: the inquirers should take reasonable care.
—SA

Obviously, your own code is like a blackbox to you. It don't do what you expect, and you don't understand why.
What follow is not directly a solution to your problem, but a key that will help you to understand by yourself what is wrong.
The debugger is your friend. It will show you what your code is really doing.
Follow the execution, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Debugger - Wikipedia, the free encyclopedia[^]

Pay attention to :
- you calculate the "average", where do you save it ?
- you display an "average", where does it come from ?
- does it match ?

-Check how you calculate the average.
Quote:
I've tried several corrections to debug this code but nothing is working.

Never try random corrections to see if it work, it is a waste of time, it is better to use a debugger and compare what you see with what you expect.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 8-Apr-16 17:36pm    
Nice advice looking convincing, a 5.
—SA
Patrice T 8-Apr-16 18:40pm    
thank you
nv3 9-Apr-16 11:30am    
My 5! And I think we should put up a big sign: TOUGH SHOULD USE YOUR DEBUGGER! That advice fits 80% of all the questions here :-)
Patrice T 9-Apr-16 11:40am    
Thank you! Agreed :)
C++
//calculate and assign average
	for (int row = 0; row < 5; row += 1)
		scores[row][0] = scores[row][0] + scores[row][1] / 2;
	//end for

You are storing the result in cell[0] instead of cell[2], which is the one you use to display the answers.
 
Share this answer
 
Comments
nv3 9-Apr-16 11:31am    
Yes, and he forgot the parentheses around the two values.
Richard MacCutchan 9-Apr-16 12:12pm    
Ah, I certainly missed that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900