Hi, I’m trying to build the table below.
—————————————————————————
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
——————————————————————————–
Can you fill in the code to build the table? Here is the file below.
——————————————————————————-
// app.cpp
#include <iostream>
#include <iomanip> // Use setw() for proper column output. I used 3.
using namespace std;
const int ROWS = 10;
const int COLS = 15;
int main()
{
int ** table; // This is a pointer to a pointer. Use it to create
// a two-dimensional array.
// Write code here to allocate memory for the table
// Step 1: Allocate an array of pointers that has ROWS elements.
// Step 2: Allocate each row to have COLS elements.
// Write code here to populate it with required integers
// as per the lab assignment document.
// Write code here to print the table.
// Write code here to deallocate memory for the table.
// You allocated each row, so now you have to deallocate each row.
// Then deallocate the table pointer itself.
// Run this program in valgrind to make sure there are no leaks.
// Valgrind command: valgrind –leak-check=full ./lab5app
return 0;
}
Expert Answer
#include<iostream>
#include<iomanip>
using namespace std;
const int ROWS=10;
const int COLS=15;
int main()
{
//**table is pointer to a pointer, and creadted
//a 2-dim array
//Allocated memory for ROWS
int **table=new int*[ROWS];
//Allocating memory for COLS
for(int i=0;i<ROWS;i++)
table[i]=new int[COLS];
//populating given pattern
for(int i=0;i<ROWS;i++)
{
int temp=i;
for(int j=0;j<COLS;j++)
{
table[i][j]=temp;
temp++;
}
}
//printing populated pattern
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<COLS;j++)
{
cout<<setw(4)<<table[i][j];
}
cout<<“n”;
}
//deallocating memory for the table
for(int i=0;i<ROWS;i++)
{
delete[] table[i];
}
delete[] table;
return 0;
}