Change value in 2D array
Apr 2, 2012 at 5:45am UTC
Hello,
I am having trouble doing two things to my array.
1) change the values of column 2. multiply by 2
2) change the value of row 7, col 6 to value 10.23.
basically having trouble with changing data in an array lol Any help would be appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_ROW = 10;
const int MAX_COL = 8;
int main()
{
// initialize variables and set array size;
int array [MAX_ROW] [MAX_COL];
int row, col, max_value_row=0, max_value_col = 0;
for (row =0; row < MAX_ROW; row++)
{
for (col = 0; col < MAX_COL; col ++)
array[row][col] = row * col;
}
// Change data in column 2
for (row=0; row < MAX_ROW; row++)
array[row][col] << row * 2;
// Change row 7, column 6 to 12.23
cout << fixed << showpoint << setprecision(2);
array[7][6] = static_cast <double > (10.23);
// Print 2D array
for (row =0; row < MAX_ROW; row++)
{
for (col = 0; col < MAX_COL; col++)
cout << setw(5) << array [row][col]<<" " ;
cout << endl;
}
// Find largest value
for (row =0; row < MAX_ROW; row++)
{
max_value_row = array [row][0];
for (col = 1; col < MAX_COL; col++)
{
if (max_value_row < array [row][col])
max_value_row = array[row][col];
}
}
for (col =0; col< MAX_COL; col++)
{
max_value_col = array[0][col];
for (row =1; row < MAX_ROW; row++)
if (max_value_col < array [row][col])
max_value_col = array [row][col];
}
if (max_value_row > max_value_col)
cout << max_value_row << " is the largest value." << endl;
else if (max_value_row < max_value_col)
cout << max_value_col << " is the largest value." << endl;
else
cout << max_value_row << " is the largest value. " ;
return 0;
}
Apr 2, 2012 at 6:40am UTC
1.
Change this:
1 2 3
// Change data in column 2
for (row=0; row < MAX_ROW; row++)
array[row][col] << row * 2;
To This:
1 2 3
// Change data in column 2
for (row=0, col = 2; row < MAX_ROW; row++) // Remember to specify the column
array[row][col] *= 2; // The << operator is not useful here.
2.
Change this:
int array [MAX_ROW] [MAX_COL];
and this:
1 2 3
// Change row 7, column 6 to 12.23
cout << fixed << showpoint << setprecision(2);
array[7][6] = static_cast <double > (10.23);
To this:
double array [MAX_ROW] [MAX_COL];
and this:
1 2
// Change row 7, column 6 to 12.23
array[7][6] = 10.23;
Last edited on Apr 2, 2012 at 6:40am UTC
Apr 2, 2012 at 6:54am UTC
Thank you very much for the assistance! I kept trying different ways to change data to a double for 10.23 but neglected that array was set as int values! :)
Topic archived. No new replies allowed.