I\'m using the variables \"r\" and \"c\" because they iterate of the rows and co
ID: 3748496 • Letter: I
Question
I'm using the variables "r" and "c" because they iterate of the rows and columns of the array, respectively. The outer loop iterates over all the rows, and the inner loop iterates over all the columns in a single row. Notice how the row index (r) comes before the column index (c).
Also, notice how the loop bounds are handled in the interior loop. While the outer loop goes from 0 to ROW, the inner loop goes from 0 to COL.
Type in, and finish the following program, which computes and prints an addition table using nested loops and a 2D array:
//C++ Lab3B.cpp
// <Your Name>
// <Your Section>
// <Your student id>
#include <iostream>
using namespace std;
typedef int* IntArrayPtr;
int main( )
{
//Propt user for input
cout << "Please Enter your Size of An Array ";
//Read an integer from the keyboard & save it to the int variable.
//[1. Add Code Here ]
//[2. Create one-dimensional dynamic array]
//[3. Create two-dimensional dynamic array]
//Fill out the table using a netsted loop so that
//Table[r][c] = r + c;
//[4. Add Code Here]
//Itrate over the table, printing each value
//so that columns align(hint: use a nested loop )
//[5. Add Code Here]
//[6. Delete dynamic array]
system("pause");
return 0;
}//End of main
When finished, with an input value of 4, the program should print something like:
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Explanation / Answer
#include<iostream>
using namespace std;
typedef int* IntArrayPtr;
int main()
{
int n;
cout << "Please Enter your Size of An Array ";
cin>>n;
IntArrayPtr R = new int[n];
IntArrayPtr* a = new int*[n];
for(int i=0;i<n;i++)
{
a[i] = new int[n];
for(int j=0;j<n;j++)
{
a[i][j]=i+j;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
delete [] R;
delete [] *a;
system("pause");
return 0;
}