Modify program such that the values for the 2 dimensional array is NOT initializ
ID: 3543286 • Letter: M
Question
Modify program such that the values for the 2 dimensional array is NOT initialized but rather read from keyboard. This must be done in a function. Test it out with the showArray() function and ensure that it does print the values inputted corrently.heres the program;
#include<iostream>
#include<iomanip>
using namespace std;
// global constants
const int COLS = 4;
const int TBL1_ROWS = 3;
const int TBL2_ROWS = 4;
void showArray(const int[] [COLS], int);
int main()
{
int table1[TBL1_ROWS][COLS] = {{1,2,3,4},{5,6 ,7,8}, {9,10,11,12}};
int table2[TBL2_ROWS][COLS] = {{10,20,30,40}, {50,60,70,80}, {90,100,110,120},{130,140,150,160}};
cout << "The contents of table1 are : ";
showArray(table1, TBL1_ROWS);
cout << "The contents of table2 are : ";
showArray(table2, TBL2_ROWS);
cout << " ";
system("PAUSE");
return 0;
}
void showArray(const int array [] [COLS], int rows)
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y<COLS; y++)
{
cout << setw(4) << array[x][y] << " " ;
}
cout << endl;
}
}
Modify program such that the values for the 2 dimensional array is NOT initialized but rather read from keyboard. This must be done in a function. Test it out with the showArray() function and ensure that it does print the values inputted corrently.
heres the program;
#include<iostream>
#include<iomanip>
using namespace std;
// global constants
const int COLS = 4;
const int TBL1_ROWS = 3;
const int TBL2_ROWS = 4;
void showArray(const int[] [COLS], int);
int main()
{
int table1[TBL1_ROWS][COLS] = {{1,2,3,4},{5,6 ,7,8}, {9,10,11,12}};
int table2[TBL2_ROWS][COLS] = {{10,20,30,40}, {50,60,70,80}, {90,100,110,120},{130,140,150,160}};
cout << "The contents of table1 are : ";
showArray(table1, TBL1_ROWS);
cout << "The contents of table2 are : ";
showArray(table2, TBL2_ROWS);
cout << " ";
system("PAUSE");
return 0;
}
void showArray(const int array [] [COLS], int rows)
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y<COLS; y++)
{
cout << setw(4) << array[x][y] << " " ;
}
cout << endl;
}
}
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
// global constants
const int COLS = 4;
const int TBL1_ROWS = 3;
const int TBL2_ROWS = 4;
void showArray(const int[] [COLS], int);
void readArray( int[] [COLS], int);
int main()
{
int table1[TBL1_ROWS][COLS];
int table2[TBL2_ROWS][COLS];
readArray(table1, TBL1_ROWS);
readArray(table2, TBL2_ROWS);
cout << "The contents of table1 are : ";
showArray(table1, TBL1_ROWS);
cout << "The contents of table2 are : ";
showArray(table2, TBL2_ROWS);
cout << " ";
//system("PAUSE");
return 0;
}
void showArray(const int array [] [COLS], int rows)
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y<COLS; y++)
{
cout << setw(4) << array[x][y] << " " ;
}
cout << endl;
}
}
void readArray( int array [] [COLS], int rows)
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y<COLS; y++)
{
int value;
cout<<"Enter a value: "; //delete this line if you dont want to show message while reading values
cin>>value;
array[x][y] = value;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.