c++ (5 pts) Review: Arrays, Structs, Pointers, etc. In this lab, you will create
ID: 3812947 • Letter: C
Question
c++
(5 pts) Review: Arrays, Structs, Pointers, etc. In this lab, you will create a dynamic two dimensional array of structs that contain the multiplication and division table of the rows and columns, starting at 1 instead of zero. This prevents us from causing a divide by zero error in the division table! The program needs to read the number of rows and columns from the user as command line arguments. You do need to check if the user supplied a number before you convert the string to a number. Continue to prompt for correct values, if the number isn't a valid, non-zero integer. At the end of the program, prompt the user if he/she wants to see this information for a different size matrix. Make sure you do not have a memory leak llChecked that rows and cols are valid, non-zero integers rows atoi(argv[11); cols atoi (argv[2]); For example, if you run your program with these command line arguments: ./prog 5 5Explanation / Answer
I have implemented the program in a single file . I assume you can make different versions of it as descibed above.. Please comment if you need further help.
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
using namespace std;
/*************************************************************************
* Following declaration can be later put into a header file "multi_div.h"
* and included into this file.
* **********************************************************************/
struct multi_div_values{
int multi;
float div;
};
bool is_valid_diminsions(char*, char*);
multi_div_values** create_table(int , int);
void set_mult_values(struct multi_div_values ** , int , int);
void set_div_values(struct multi_div_values **, int , int);
void delete_table(struct multi_div_values ** , int);
/************************************************************************/
void print_mult_values(struct multi_div_values **, int , int);
void print_div_values(struct multi_div_values **, int , int);
void ask_input(char*, char*,int* r , int*c );
/* Assuming user has passed command line arguments */
int main(int argc, char* argv[])
{
int ret = 0, choice = 1, r = 0, c = 0 ;
char row[10] = {''}, column[10] = {''};
struct multi_div_values ** table = NULL;
strncpy(row,argv[1], strlen(argv[1]));
strncpy(column,argv[2], strlen(argv[2]));
ret = is_valid_diminsions(row, column);
if(!ret){
ask_input(argv[1],argv[2],&r, &c);
}
else{
r = atoi(row);
c = atoi(column);
}
while(choice == 1)
{
table = create_table(r, c);
set_mult_values(table , r , c);
set_div_values(table, r , c);
print_mult_values(table,r,c);
print_div_values(table , r, c);
delete_table(table ,r);
cout<< "Would you like to see different size matrix (0-no 1-yes)? ";
cin >>choice;
if(choice){
ask_input(NULL, NULL,&r , &c);
}
}
return 0;
}
void ask_input(char* input_row, char* input_column,int* r , int* c)
{
int row =0 , column = 0;
if((input_row == NULL) && (input_column == NULL)){
cout << "Please enter an integer greater than 0 for row : ";
cin >> row;
cout << "Please enter an integer greater than 0 for column : ";
cin >> column;
}
else{
if( atoi(input_row) <= 0){
cout << "Please enter an integer greater than 0 for row : ";
cin >> row;
*r = row;
}
else
*r = atoi(input_row);
if( atoi(input_column) <= 0){
cout << "Please enter an integer greater than 0 for column : ";
cin >> column;
*c = column;
}
else
*c = atoi(input_column);
return;
}
*r = row;
*c = column;
}
bool is_valid_diminsions(char* row, char* column)
{
int r = atoi(row);
int c = atoi(column);
if((r<=0) || (c<=0)){
return false;
}
}
multi_div_values** create_table(int row, int column)
{
struct multi_div_values** table = new struct multi_div_values*[row];
int i =0;
for(i=0; i< row ;i++){
table[i] = new struct multi_div_values[column];
}
return table;
}
void set_mult_values(struct multi_div_values ** table, int row, int column)
{
int i = 0, j=0;
if(table == NULL)
return;
for(i=1; i<= row; i++)
{
for(j=1; j<=column ;j++){
(table[i-1][j-1]).multi = i*j;
}
}
}
void set_div_values(struct multi_div_values ** table, int row , int column)
{
int i = 0, j=0;
if(table == NULL)
return;
for(i=1; i<= row; i++)
{
for(j=1; j<=column ;j++){
(table[i-1][j-1]).div = i/(float)j;
}
}
}
void delete_table(struct multi_div_values ** table , int row)
{
int i =0;
for(i=0; i< row;i++){
delete table[i];
}
delete table;
}
void print_mult_values(struct multi_div_values ** table, int row, int column)
{
int i =0, j=0;
cout<<"Multiplication Table : " <<endl;
for(i=0 ;i<row;i++)
{
for(j=0; j<column;j++)
{
cout<< (table[i][j]).multi << " ";
}
cout<<endl;
}
}
void print_div_values(struct multi_div_values ** table, int row , int column)
{
int i =0, j=0;
cout<<"Division Table : " <<endl;
for(i=0 ;i<row;i++)
{
for(j=0; j<column;j++)
{
cout<< setw(4) <<setprecision(2)<< fixed << (table[i][j]).div << " ";
}
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.