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;
}
2in yoursetw()call.