printing array elements in the programs output.

I'm trying to print all the array elements in their proper columns in the second for(loop).
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
#include <iostream>
#include <iomanip>
using namespace std;
 
 int main()
{
	const int NUM_ROW =6;
	const int NUM_COL =5;
	
double matrix_members [6][5] ={ {1.267, 0.167, 0.250, 2.670, 1.000},
	                        {3.240, 0.376, 0.375, 3.400, 1.128},
	                        {7.564, 0.668, 0.500, 4.303, 1.270},
	                        {5.041, 1.043, 0.625, 5.313, 1.410},
	                        {4.660, 1.502, 0.750, 7.650, 1.693},
	                        {5.727, 2.044, 0.875, 3.600, 2.257}};
	   int norm_values,
		matrix_mem,
		matrix_val,
		   mem_cel;


	    double mem_sum,
		   max_val;

	    cout << setprecision(1)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);
// Obtains and prints the  matrix members and divides the maximum value cell with each cell in that column.

	cout << "Print the normalized matrix value and divides"<< endl;
	cout<< "the maximum value cell with the rest of that column"<< endl;
	
	for(norm_values=0; norm_values<NUM_COL; ++norm_values)
	{
		for(mem_cel=0; mem_cel<NUM_ROW; ++mem_cel)
		matrix_mem += matrix_members[norm_values][mem_cel];
		matrix_val= matrix_mem + NUM_COL;
		cout<< endl <<endl;
		
		mem_sum = 0;
		for(mem_cel = 0; mem_cel < NUM_ROW; ++mem_cel)
	        mem_sum += matrix_members[norm_values][mem_cel];
		max_val=(double) mem_sum / NUM_ROW;
	        cout << endl << endl;
		cout << matrix_mem << setw(5) <<""
			<< " Value is: " << setw(5) << max_val;
	}
 
	cout << endl;
	return 0;
}
As general,matrix is printed out as ;
1
2
3
4
5
6
for( i = 0 ; i < NUM_ROW ; i++)
{
       for( j = 0 ; j < NUM_COL ; j++) 
              cout << matrix[i][j] << "  " ;
      cout << endl;
}

Last edited on
Dufresne, I appreciate you feedback that worked out just fine. The third for(loop) would give an output of the maximum value cell for each column and divided the cell by the other cells before i applied the changes to the second for(loop) I tried to changing it and had no luck.
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
#include <iostream>
#include <iomanip>
using namespace std;
 
 int main()
{
	const int NUM_ROW =6;
	const int NUM_COL =5;
	
	double matrix_members [6][5] =             { {1.267, 0.167, 0.250, 2.670, 1.000},
	                                                                    {3.240, 0.376, 0.375, 3.400, 1.128},
	                                                                    {7.564, 0.668, 0.500, 4.303, 1.270},
	                                                                    {5.041, 1.043, 0.625, 5.313, 1.410},
	                                                                    {4.660, 1.502, 0.750, 7.650, 1.693},
	                                                                    {5.727, 2.044, 0.875, 3.600, 2.257}};
	
	       int norm_values,
		   mem_cel;


	    double mem_sum,
		   max_val;

	         cout << setprecision(1)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);
	// Obtains and prints the  matrix members and divides the maximum value cell for each column.

	cout << "Print the normalized matrix value and divides"<< endl; 
        cout<< "the maximum value cell with the rest of that column"<< endl; 

	
	for(norm_values=0; norm_values<NUM_ROW; norm_values++)
	{
		for(mem_cel=0; mem_cel<NUM_COL; mem_cel++)
		    cout<<matrix_members[norm_values][mem_cel]<<"  ";
		cout<<endl;
	}
	{

		mem_sum = 0;
		for(mem_cel = 0; mem_cel < NUM_COL; ++mem_cel)
		mem_sum += matrix_members[norm_values][mem_cel];
		max_val=(double) mem_sum / matrix_members[norm_values][mem_cel];
	                cout << endl << endl;
		cout  << setw(10) <<max_val;
	}
 
	cout << endl;
	return 0;
}

1
2
3
4
5
6
7
8
Deleting intermediate and output files for project 'Week3Program', configuration 'Debug|Win32'
Compiling...
Week3Program.cpp
Linking...
Embedding manifest...
Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\Week3Program\Week3Program\Debug\BuildLog.htm"
Week3Program - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========




Topic archived. No new replies allowed.