Thread: Storing and printing matrices

  1. #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.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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