0

I have a 2D array of integers, as seen in the main function. The program needs to prompt the user for a single integer. I then must add the user's value to each of the values in the 2D array using the 'addValue' function, as seen in the main function.

Then, write the 'print' function to print out the values in the array with each row of the array on its own line, and each number in each row separated by a single whitespace.

NOTE: I should NOT print a whitespace after the last number on each line.

Example:

Input:
2

Output:

7 6

25 1

The problem I have is that I need exactly to have a space in the output, but I only get that for certain input values. For example when input is -1 I get output:

4 3

22-2      <- Notice that there is not an space.

Could someone help me fix it. Thanks

#include <iostream>
#include<iomanip>

using namespace std;

void addValue(int my_array[][2],int value)
{
   for(int i=0;i<2;i++)
   {
       for(int j=0;j<2;j++)
       {
           my_array[i][j]=my_array[i][j]+value;  
       }
   }
}
void print(int my_array[][2])
{
   for(int i=0;i<2;i++)
   {
       for(int j=0;j<2;j++)
       {
        cout<<setw(2)<<my_array[i][j];  
    }
       cout<<endl;
   }
}

int main()
{
     int my_array[2][2] = {{5,4},{23,-1}};

     int value;
     cin >> value;

     addValue(my_array, value);

     print(my_array);

     return 0;
}
1
  • Will your numbers always be 2 or less digits? If not, then your issue is not only with the space, but with the 2 in your setw() call. Commented Mar 11, 2017 at 6:32

1 Answer 1

1
cout<<setw(2)<<my_array[i][j]; 

outputs my_array[i][j] with a minimum spacing of 2 characters. If you have a value that takes 2 or more characters to display then... well here's an example:

std::cout << std::setw(1) << 42 << std::setw(2) << 42 << std::setw(3) << 42 << std::endl;

outputs

4242 42

So in the sample code 23 takes 2 characters and -1 takes 2 characters. They get crushed together and goes 23-1.

Solution:

Force the space

cout<<setw(2)<<my_array[i][j] << ' '; 

this can mess up the spacing of the columns and breaks the no space at the end of the line rule, so the other option is to ensure that the number passed into setw is large enough to maintain the spacing no matter what.

cout<<setw(100)<<my_array[i][j]; 

OK, that 100 is overkill, but you get the point. You could also put the space first.

cout<<setw(2)<<' ' << my_array[i][j]; 

No rule I see about a preceding space. Yet another option is to not bother with the inner for loop

cout << my_array[i][0] << ' ' << my_array[i][1]; 

if you have to preserve the for loop, print the first element without any adornment, then print the rest of the elements with a preceding space.

for(int i=0;i<2;i++)
{
    cout<< my_array[i][0];  
    for(int j=1;j<2;j++)
    {
        cout<< ' ' <<my_array[i][j];  
    }
    cout<<endl;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.