Allocate storage to store a 3 row by 3 column two dimensional array of integers.
ID: 3840883 • Letter: A
Question
Allocate storage to store a 3 row by 3 column
two dimensional
array of integers. Call the array A.
Initial
ize the array with each element’s
value as follows:
A[0][0] = 1
A[0][1] = 2
A[0][2] = 3
A[1][0] = 4
A[1][1] = 5
A[1][2] = 6
A[2][0] = 7
A[2][1] = 8
A[2][2] = 9
Then, have the program use a loop to continually ask the user to
input
a
pair of integers. The first
integer is the row number
(row)
and the second integer is the column number
(column)
of an
element in the array A.
One of these three action
s will be performed depending on the value of
the row number and column number inputted:
1.
If the row number is
-
1 and
the
column number is
-
1 then terminate the loop and the
program.
2.
Or, c
heck to
m
ake sure the row number and column number are valid
indices
for the array
A
. A valid index for array A is
0
, 1 or
2
. If either
the
row
number
or
the
column
number
(
or both) are invalid then
do nothing and return to the beginning of the loop to
ask for a
new pair of numbers.
3.
If
the row number and the column number
are valid
indices
then load the value of element
A[row][column] from memory and print out the value.
Then, return to the beginning of
the loop to
ask for a new pair of numbers.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int A[3][3] = {1,2,3,4,5,6,7,8,9};
int row,col;
do
{
cout<<" Enter row number and column number :";
cin>>row>>col;
if(row >=0 && row <3 && col >=0 && col <3)//validate row and column
cout<<" A["<<row<<"]["<<col<<"] = "<<A[row][col];//display A if valid row and column
}
while(row != -1 && col != -1);
return 0;
}
Output:
Enter row number and column number :2 0
A[2][0] = 7
Enter row number and column number : 3 6
Enter row number and column number : 1 1
A[1][1] = 5
Enter row number and column number : 4 5
Enter row number and column number : 2 2
A[2][2] = 9
Enter row number and column number :-1 -1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.