2D array display not correct

I'm in an intro to C++ which is why my code may seem in-efficient but this is how my instructor has taught us thus far. Anyway I will give you the jist of the program requirements, no input from user, it has two procedures, a display() and an compute() ( used to find average of the 3 test scores ). I must use a 2D array for the scores and i put the scores right in the initialization. When I run the program it only displays the last score in each group of numbers in my scores array. What i need you professionals to help me with is,

1) Am i referencing how i would like to display the rows and columns correctly ?

2) If not, why does C++ output 2d arrays like this? What I think ( Logically in my loop / nested loop, i specify the row with the loop variable and the loop1 variable references the column in that row. This is clearly not happening in my output console window and it is frustrating. )

Here is the output window:

http://img139.imageshack.us/img139/8499/cmdtestip6.png

Here is my code

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
59
#include <iomanip>
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::left;
using std::setiosflags;
using std::setw;
using std::string;
using std::ios;


//void display(string namesArray[], int gradesArray[][3]);
//void compute(int gradesArray[][3]);

const string upperDisplay = "Names          Score #1      Score #2      Score #3     Average";

void display(string namesArray[], int gradesArray[][3])
{
	int count = 0;
	cout << upperDisplay << endl;
    int loop = 0;
    int loop1 = 0;

	for (loop = 0; loop < 4; loop ++)
	{
	    loop1 = 0;
		cout << setiosflags(ios::left) << setw(16) << namesArray[loop] << setw(14);

		for (loop1 = 0; loop1 < 3; loop1 ++)
		{
			cout << setw(14) << gradesArray[loop][loop1] << setw(10);
		}
		cout << endl;
	}
}

void computeAverage()
{


}

int main()
{
	int stop = 0;

	string studentsArray[4] = { "I.P. Freely", "Al Bino", "Claire Anette", "Rob Banks" };
	int assignmentArray[4][3] = { (95,85,95), (100,85,95), (85,90,100), (95,100,85) };

	display(studentsArray, assignmentArray);

	cout << endl << "STOP";
	cin >> stop;

	return 0;
}
Watch your syntax on initialization:
 
int assignmentArray[4][3] = { {95,85,95}, {100,85,95}, {85,90,100}, {95,100,85} };

Each array must be braced with { and }.
The expression (95,85,95) evaluates to 95. So what you had before is the same as:
 
int assignmentArray[4][3] = { 95, 95, 100, 85 };

which is the same as:
 
int assignmentArray[4][3] = { {95,0,0}, {95,0,0}, {100,0,0}, {85,0,0} };


A couple of other thoughts for you:
It is OK to say using namespace std; in your main program.
Also, loop and loop1 are terrible names for counting variables. You would do just as well, and be much more readable and explicit, with int count0, count1; or int i,j.

Hope this helps.
Last edited on
Thank you so much, lol i usually use count, count1 as my loop counters but everyone in my class likes using loop so i tryed it just for this one (im glad im the right one :)) also, what does using namespace std; do ? And THANK U for my little initializing, omg was this annoying :P
A lot of times instructors (usually those brain-damaged by Java) will have their students explicitly list each and every item imported into the current namespace. That is what you have done at the top with all those using statements.

However, since you are using so many things, all from ::std:: and this is your main program (not a library header file), it is OK to just dump everything from the standard namespace into the current namespace.

This is sadly one of those little things people get up-tight about, but the correct rule is: as long as you don't contaminate someone else's namespace, whatever you do is OK. Just so long as it is convenient and workable for you.

In general though, you'll want to try to avoid unnecessarily contaminating your own namespace too, to avoid unintentional name-clashes.
Few notes not previously added, you'r upperdisplay is worthless the way it's done now. Also try setw(15) instead of 16. It arranges everythign nicely. Also use namespace std instead of declaring everything. Also you need to decalre ALL ARRAYS in braces. Plus there's mroe effective ways to pause a system to show output. or print ot file, open file, exit. That's true pro.
Topic archived. No new replies allowed.