Hi i need help on my assignment what i have to do is Enter data into a 5 X 7 array. The example i have is
Please enter the numbers in the first row: 1234567
Please enter the numbers in the second row: 1234567
Please enter the numbers in the third row: 1234567
Please enter the numbers in the fourth row: 1234567
Please enter the numbers in the fifth row: 1234567

The original array is:
1234567
1234567
1234567
1234567
1234567

Average of row one is: 4
Average of row two is: 4
Average of row three is: 4
Average of row four is: 4
Average of row five is: 4

Average of column one is: 1
Average of column two is: 2
Average of column three is: 3
Average of column four is: 4
Average of column five is: 5
Average of column six is: 6
Average of column seven is: 7

what i got is .. how do I get the original array and averages thank you

#include <iostream>
using namespace::std;

#include <iomanip>

int main()
{
	const int size = 5;
	const int column = 7;
	int a[size];
	int num,num1,num2,num3,num4,num5,num6;

		cout <<"Please enter seven numbers in the first row: ";
		cin >>num>>num1>>num2>>num3>>num4>>num5>>num6;
		

		cout <<"Please enter seven numbers in the second row: ";
		cin>>num>>num1>>num2>>num3>>num4>>num5>>num6;

		cout <<"Please enter seven numbers in the third row: ";
		cin>>num>>num1>>num2>>num3>>num4>>num5>>num6;
		

		cout <<"Please enter seven numbers in the fourth row: ";
		cin>>num>>num1>>num2>>num3>>num4>>num5>>num6;
	
		cout <<"Please enter seven numbers in the fifth row: ";
		cin>>num>>num1>>num2>>num3>>num4>>num5>>num6;
		

		cout <<"The original array is: \n";
		for ( int i = 0; i < size; i++ )
        cout <<"\n"<< setw( 3 )<<a[ i ];

		return 0;
}

Recommended Answers

All 7 Replies

You're not creating a 5x7 array. What you're doing is creating a 1x5 array. What you need is:

int myArray[5][7];

Then you'll need 2 nested for loops to assign the numbers to the array.

Here's an example. You'll still have to work your averages out.

#include <iostream>

using namespace std;

int main()
{
    const int height = 2;
    const int width = 4;

    int myArray[width][height];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            cout << "Enter digit: ";
            cin >> myArray[j][i];
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            cout << myArray[j][i] << "  ";
        }
        cout << endl;
    }

}

YEAH [5][7]

means 5 row and 7 colomns.

I still dont get it. This is what i have now im trying to input 1 2 3 4 5 6 7 and make that become row 1 instead of looping it countless times thanks;

#include <iostream>
using namespace std;


int main()
{
    const int row = 5;
   
    const int column = 7;
  
    int myArray[column][row];
  
    for (int i = 0; i < row; i++)
	  {

      for (int j = 0; j < column; j++)
 
      {

      cout << "Please enter the numbers for the first row: ";
      cin >> myArray[j][i];
	  
	  cout<<"Please enter the numbers for the second row: ";
	  cin >> myArray[j][i];
	
	  cout <<"Please enter the numbers for the third row: ";
	  cin >> myArray[j][i];
	  

      }

      }

      for (int i = 0; i < row; i++)

      {

      for (int j = 0; j < column; j++)

      {

      cout << myArray[j][i] << " ";

	  }
      cout << endl;

	  }
 }

Try something like this:

for (int i = 0; i < row; i++)
{
      cout << "\nPlease enter the numbers for the "<<(i+1)<<" row: ";
      for (int j = 0; j < column; j++)
       {
                cin >> myArray[i][j];
        }
}

Try something like this:

for (int i = 0; i < row; i++)
{
      cout << "\nPlease enter the numbers for the "<<(i+1)<<" row: ";
      for (int j = 0; j < column; j++)
       {
                cin >> myArray[i][j];
        }
}

I agree with jonsca

you can't enter values to an array directly you have to use a loop
and if it was a 2-d array you have to use two loops the first is controlled by the rows and the second is controlled by the columns

I still dont get it. This is what i have now im trying to input 1 2 3 4 5 6 7 and make that become row 1 instead of looping it countless times thanks;

To help you understand what you're doing let's go over your code:

Start a loop
      for (int j = 0; j < column; j++)
 
      {

      //Enter data into loop
      cout << "Please enter the numbers for the first row: ";
      cin >> myArray[j][i];
	  
      //You haven't changed the position of j yet so
      //overwrite what you just entered.... Ooops
	  cout<<"Please enter the numbers for the second row: ";
	  cin >> myArray[j][i];
	
      //Same as above, overwrite what you entered once again
	  cout <<"Please enter the numbers for the third row: ";
	  cin >> myArray[j][i];
	  

      }

You don't want to overwrite your data so you need to increment your counter every time you enter data.

//loop does nothing
     for (int i = 0; i < row; i++)

      {

     //loop does nothing
      for (int j = 0; j < column; j++)

      {

???

If you look at my post I clearly stated that you need TWO NESTED for loops like this:

//Increment the rows
    for (int i = 0; i < height; i++)
    {
        //Increment the columns
        //So for each ONE row this will execute for ALL your columns
        //So you're don't need any other loops except to output 
        //the data. This will fill your whole array... mind you that
        //your input statement isn't changing for each number...
        //but you could do some creative coding with if statements
        //that would make that possible too
        for (int j = 0; j < width; j++)
        {
            cout << "Enter digit: ";
            cin >> myArray[j][i];
        }
    }

If you haven't covered loops in your C++ class yet they you could always do this without loops, but it will be a LOT of code. Basically you'll have to hand code 35 cin statements and increment the row/columns 35 times, something like:

int main()
{
    const int row = 2;
    const int column = 2;

    int myArray[row][column];
   
    int r = 0, c = 0;
    cout << "Enter first digit: ";
    cin >> myArray[r][c];
    c++
    cout << "Enter second digit: ";
    cint >> myArray[r][c];
    c++;
    
    //etc etc etc
}

Hope this helps you understand the solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.