C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2006, 02:21 PM   #1
Registered User
 
Join Date: Mar 2006
Posts: 36
Storing and printing matrices

I am trying to make a program which will ask the user to input a series of numbers, then in end, display the numbers. I am using arrays and need to save the numbers in a 3x4 matrix and then display it. This is what I have:

Code:
#include <iostream>
#include <iomanip>
using namespace std;

const int numrow = 3;
const int numcol = 4;
	
int grades [numrow][numcol];

int main ()
{
	for (int i = 0; i <= 3; i++)
	{
	cout << endl;
	for (int j = 0; j <= 4; j++)
	{
	cout << "enter grades for row # " << (i + 1)<< ": ";
	cin >> grades [numrow][numcol];
	}
	}
            cout << setw (4) << grades [i][j];

	return 0;
}
any help would be greatly appreciated as I do not know what is missing/ needs to be put in/ needs to be changed/ etc.
stevedawg85 is offline   Reply With Quote
Old 03-20-2006, 08:57 AM   #2
Ethernal Noob
 
Join Date: Nov 2001
Posts: 1,891
1) you should have the for loop control variables set to numrow and num col (i < numrow) (j < numcol) - (not i <= numrow since the last indice will always be one less than the size) - so you don't have to go back and change them when you change the size of the array

2)
Code:
cin >> grades [numrow][numcol];
You're resetting the same variable which is grades[3][4]. Which is out of the array boundaries. Remember the array of size three has elements [0][1][2].

3) Your'e outputting the Grades[i][j] outisde of the scope of where i and j were declared. You'd have to do another loop to output it.

Here's what I got.

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    const int numrow = 3;
    const int numcol = 4;
	
    int grades [numrow][numcol];

	for (int i = 0; i < numrow; i++)
	{   
	   cout << endl;
	   for (int j = 0; j < numcol; j++)
	   {
	       cout << "enter grades for row # " << (i + 1) << ": ";
	       cin >> grades [i][j];
	   }
	}	
    
    for (int i = 0; i < numrow; i++)
	{      
	   cout << endl;
	   for (int j = 0; j < numcol; j++)
	   {
	       cout << setw(4) << grades[i][j];
	   }
	}
	cout << endl;

    system("PAUSE");
    return 0;
}

Last edited by indigo0086; 03-20-2006 at 09:05 AM.
indigo0086 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening a file and then printing the output? Sholcomb1 C Programming 5 11-02-2006 09:44 AM
Help with Printing BMPs simtron Windows Programming 5 09-22-2005 09:21 AM
Storing in structure bennyboy C Programming 1 02-26-2005 06:14 AM
Problem with storing and printing kasun C++ Programming 2 02-25-2004 08:36 AM
integers storing as symbols in arrays rjcarmo C++ Programming 4 05-19-2003 01:17 AM


All times are GMT -6. The time now is 09:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22