displaying all the elements in the array.

missing the top cell for each column and the last column in the output

Page 1 of 1

10 Replies - 1625 Views - Last Post: 25 April 2010 - 12:52 PM Rate Topic: -----

#1 Marty09x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-April 10

displaying all the elements in the array.

Posted 24 April 2010 - 06:42 AM

#include <iostream>
using namespace std;

int main()
{
double arr_2d[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}};
 
cout << endl;

int row, col;
for(row=1; row <6; ++row)
{
	cout <<endl;
	for(col=0; col < 5; ++col)
		cout  << arr_2d[row][col];

}
cout << endl;

return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: displaying all the elements in the array.

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: displaying all the elements in the array.

Posted 24 April 2010 - 06:53 AM

Rows start at 0.
Was This Post Helpful? 1
  • +
  • -

#3 Marty09x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-April 10

Re: displaying all the elements in the array.

Posted 24 April 2010 - 10:08 AM

View PostMarty09x, on 24 April 2010 - 05:42 AM, said:

#include <iostream>
using namespace std;

int main()
{
double arr_2d[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}};
 
cout << endl;

int row, col;
for(row=1; row <6; ++row)
{
	cout <<endl;
	for(col=0; col < 5; ++col)
		cout  << arr_2d[row][col];

}
cout << endl;

return 0;
}

 Thank you for your feedback it worked out just fine. I realize I read it wrong when I tried that with  (setw) to make space between the elements output but I got this syntax error.


#include <iostream>
using namespace std;

int main()
{
double arr_2d[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}}; 
cout << endl;
 int row, col;
for(row=0; row <6; ++row)
    cout <<endl;
{
	for(col=0; col < 5; ++col)
		cout << setw(5)  << arr_2d[row][col];

}
cout << endl;

return 0;
}


------ Rebuild All started: Project: SimpleProJ, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'SimpleProJ', configuration 'Debug|Win32'
Compiling...
SimpleProj.cpp
.\SimpleProj.cpp(18) : error C3861: 'setw': identifier not found
Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\SimpleProJ\SimpleProJ\Debug\BuildLog.htm"
SimpleProJ - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: displaying all the elements in the array.

Posted 24 April 2010 - 10:10 AM

setw is defined in the header file <iomanip>.
Was This Post Helpful? 0
  • +
  • -

#5 Marty09x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-April 10

Re: displaying all the elements in the array.

Posted 24 April 2010 - 03:58 PM

i rewrote the program because i couldn't get the elements to print in their proper columns.
this programs output should print the elements in columns and divide them by the their maximum value cell.
there is one syntax error.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
int i, j;
double max_val, max_col;

double  c[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}};
ifstream infile ("C:\\L7_2.DAT");
 for(i=0; i < 6; i++)
{
	 for(j=0; j < 5; j++)
	 {
     for(i =0; i < 6; ++i)
	 {
		max_col = 0;
		for(j = 0; j< 5; ++j)
			max_col += c[i][j];
		max_val=(double) max_col / 6;
	    cout << endl << endl;
   
          {
	  cout << setw(15) <<c[i][j]<< " ";
	  }
	 
	 
}
cout << endl;
return 0;
}

Compiling...
SimpleProj.cpp
.\SimpleProj.cpp(39) : fatal error C1075: end of file found before the left brace '{' at '.\SimpleProj.cpp(19)' was matched
Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\SimpleProJ\SimpleProJ\Debug\BuildLog.htm"
SimpleProJ - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

This post has been edited by JackOfAllTrades: 24 April 2010 - 04:04 PM
Reason for edit:: Removed code tags.

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: displaying all the elements in the array.

Posted 24 April 2010 - 04:05 PM

Dude...put your CODE in code tags, NOT your text!

From your error, you're missing a closing brace } on (at least) one of your loops.
Was This Post Helpful? 0
  • +
  • -

#7 Marty09x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-April 10

Re: displaying all the elements in the array.

Posted 25 April 2010 - 09:41 AM

View PostMarty09x, on 24 April 2010 - 02:58 PM, said:

i rewrote the program because i couldn't get the elements to print in their proper columns.
this programs output should print the elements in columns and divide them by the their maximum value cell.
there is one syntax error.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
int i, j;
double max_val, max_col;

double  c[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}};
ifstream infile ("C:\\L7_2.DAT");
 for(i=0; i < 6; i++)
{
	 for(j=0; j < 5; j++)
	 {
     for(i =0; i < 6; ++i)
	 {
		max_col = 0;
		for(j = 0; j< 5; ++j)
			max_col += c[i][j];
		max_val=(double) max_col / 6;
	    cout << endl << endl;
   
          {
	  cout << setw(15) <<c[i][j]<< " ";
	  }
	 
	 
}
cout << endl;
return 0;
}

Compiling...
SimpleProj.cpp
.\SimpleProj.cpp(39) : fatal error C1075: end of file found before the left brace '{' at '.\SimpleProj.cpp(19)' was matched
Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\SimpleProJ\SimpleProJ\Debug\BuildLog.htm"
SimpleProJ - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Was This Post Helpful? 0
  • +
  • -

#8 Marty09x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-April 10

Re: displaying all the elements in the array.

Posted 25 April 2010 - 09:52 AM

i'm trying to print all the elements in the array in the second for(loop) in their proper columns.
#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;
}


------ Rebuild All started: Project: Week3Program, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'Week3Program', configuration 'Debug|Win32'
Compiling...
Week3Program.cpp
.\Week3Program.cpp(37) : warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data
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), 1 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Was This Post Helpful? 0
  • +
  • -

#9 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: displaying all the elements in the array.

Posted 25 April 2010 - 09:59 AM

You haven't initialized "matrix_mem" with any value, so that's why you are getting garbage output. Note that C/C++ does not default initialize a variable, so when you use something like this:
int a;


a has basically no value. To get over this, just initialize your variable with '0':
int norm_values,
matrix_mem = 0,



The warning occurs because of this line:
matrix_mem += matrix_members[norm_values][mem_cel];


matrix_mem is an integer, but matrix_members is a 2D array containing doubles. So, matrix_members[norm_values][mem_cel] is a double. Addition between an integer and a double, may occur in data loss (truncation of data after the decimal point).
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: displaying all the elements in the array.

Posted 25 April 2010 - 11:12 AM

Why did you open a new topic on the same issue?

Merging with original topic.
Was This Post Helpful? 0
  • +
  • -

#11 Marty09x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-April 10

Re: displaying all the elements in the array.

Posted 25 April 2010 - 12:52 PM

sarmanu thank for your feedback i actually posted the wrong topic that program was still been worked on the third for(loop)is the loop in question. The program prints all the array elements in the proper columns but when i made that correction the third for(loop) no longers prints the maximum value cell divde by the other cells in that column in it own column i tried to fix it no luck.
#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;
}

------ Rebuild All started: Project: Week3Program, Configuration: Debug Win32 ------
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 ==========

This post has been edited by Marty09x: 25 April 2010 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1