Question

In: Computer Science

you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...

you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table!

struct mult_div_values {
     int mult;
     float div;
};

The program needs to read the number of rows and columns from the user as command line arguments. You should check that the user actually supplied a number before converting the input string to an integer. Continue to prompt the user for correct values if the number isn't a valid non-zero integer. At the end of the program, prompt the user if they want to see this information for a different size matrix.

// One approach to handling the command line arguments...
rows=atoi(argv[1]);
cols=atoi(argv[2]);
// Now check that rows and cols are valid non-zero integers
// ...

For example, if you run your program with these command line arguments

./prog 5 5 

Your program should create a 5 by 5 matrix of structs and assign the multiplication table to the mult variable in the struct and the division of the indices to the div variable in the struct. The mult variable is an integer, and the div variable needs to be a float (or double).

Your program needs to be well modularized with functions, including main, with 10 or less lines of code. This means you will have a function that checks if the rows are valid, non-zero integers:

bool is_valid_dimensions(char *m, char *n)

and another function that creates the matrix of structs given the m times n dimensions:

mult_div_values** create_table(int m, int n)

In addition, you need to have functions that set the multiplication and division values, as well as delete your matrix from the heap:

void set_mult_values(mult_div_values **table, int m, int n)
void set_div_values(mult_div_values **table, int m, int n)
void delete_table(mult_div_values **table, int m)

Then create functions to print the tables. After your code is functioning, test it using the following approach.

Example Run:

./prog 5 t
You did not input a valid column.
Please enter an integer greater than 0 for a column: 5
Multiplication Table:
1  2  3  4  5
2  4  6  8  10
3  6  9  12 15
4  8  12 16 20
5  10 15 20 25
Division Table:
1.00 0.50 0.33 0.25 0.20
2.00 1.00 0.67 0.50 0.40
3.00 1.50 1.00 0.75 0.60
4.00 2.00 1.33 1.00 0.80
5.00 2.50 1.67 1.25 1.00
Would you like to see a different size matrix (0-no, 1-yes)?
0

(5 pts) Separation of Files & Makefiles

Since we now have function prototypes and a struct that is a global user-defined type, then we might want to begin making an interface file that holds all of this information for us.

Create a mult_div.h interface file that will contain all the function and struct declaration information we need:

struct mult_div_values {
     int mult;
     float div;
};

bool is_valid_dimensions(char *, char *);
mult_div_values** create_table(int, int);
void set_mult_values(mult_div_values **, int, int);
void set_div_values(mult_div_values **, int, int);
void delete_table(mult_div_values **, int);

After creating this mult_div.h file you need to #include it in your implementation file (the one with a .cpp extension). You also need to remove the prototypes and struct definition from your implementation file (e.g. delete the original prototype/struct definition lines from the .cpp file).

#include “./mult_div.h” 

Now compile your program normally:

g++ mult_div.cpp –o mult_div

Let's take this a step further, and keep only your function definitions in the implementation file (mult_div.cpp), and put your main function in a separate file called "prog.cpp" Your prog.cpp file will have to #include the mult_div.h file as well. To put these files together, they need to be compiled together:

 g++ mult_div.cpp prog.cpp –o mult_div

What if we had 1,000 implementation (.cpp) files? Manually compiling all of them together would take forever and have a high chance for error. Luckily, UNIX/Linux has a built in script that makes compiling multiple files together easy called a Makefile. Just type

vim Makefile 

on the command line to create it. Now modify the file following the pattern shown below:

<target>:
     <compiler> <file1.cpp> <file2.cpp> -o <target>

Note that the leading whitespace MUST be a tab (you can't just use spaces). An example of what this could look like for this lab would be:

mult_div:
     g++ mult_div.cpp prog.cpp -o mult_div

Save and exit the file. You can type "make" in the terminal to run it.

One of the other benefits of makefiles is that you can add variables to it and make compiling happen in different stages by stopping g++ after compiling and before running it through the linker. This creates object files (.o files), which you can link together

CC = g++
exe_file = mult_div
$(exe_file): mult_div.o prog.o
     $(CC) mult_div.o prog.o -o $(exe_file)
mult_div.o: mult_div.cpp
     $(CC) -c mult_div.cpp
prog.o: prog.cpp
     $(CC) -c prog.cpp

Try to make your program again. Notice all the stages. In addition, we usually add a target for cleaning up our directory:

clean:
     rm –f *.out *.o $(exe_file)

Now we can run the specific target by typing "make <target>.

make clean

Makefiles are a useful way to automate and control the program building process as your projects grow in size.

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Here I am attaching code for these files:

  • mult_div.cpp
  • mult_div.h
  • prog.cpp

mult_div.cpp:

#include <iostream>
#include <string>
#include "mult_div.h"

using std::cout;
using std::cin;
using std::endl;


void were_dimensions_specified(int num_args){
if (num_args != 3){
cout << "Could not find row and column numbers" << endl;
cout << "Expected usage: ./lab1 <num rows> <num columns>"<< endl;
exit(1);
}
}


struct mult_div_values{
int mult;
float div;
};


bool is_valid_dimensions(char* m, char* n){
int rows = atoi(m);
int cols = atoi(n);
if (rows>=1 && cols>=1){
return true;
}
else if (rows<1){
cout << "You did not input a valid row." << endl;
cout << "Please enter an integer greater than 0 for a row: ";
cin >> *m;
return is_valid_dimensions(m , n);
}
else if (cols<1){
cout << "You did not input a valid column." << endl;
cout << "Please enter an integer greater than 0 for a column: ";
cin >> *n;
return is_valid_dimensions(m, n);
}
else{
return false;
}
}


mult_div_values** create_table(int m, int n){
mult_div_values** table = new mult_div_values*[m];
for(int i=0; i<m; i++){
table[i] = new mult_div_values[n];
}
return table;
}

void set_mult_values(mult_div_values** table, int m, int n){
for (int i = 0; i<m; i++){
for (int j=0; j<n; j++){
table[i][j].mult = (i+1)*(j+1);
}
}
}


void set_div_values(mult_div_values** table, int m, int n){
for (int i = 0; i<m; i++){
for (int j=0; j<n; j++){
table[i][j].div = float(i+1)/float(j+1);
}
}
}

void delete_table(mult_div_values** table, int m){
for (int i=0; i<m; i++){
delete [] table[i];
}
delete [] table;
}

void print_table(mult_div_values** table, int m, int n){
cout << "Multiplication Table: " << endl;
for (int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf(" %i ", table[i][j].mult);
}
cout << endl;
}
cout << "Division Table: " << endl;
for (int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf(" %.3f ", table[i][j].div);
}
cout << endl;
}
}


mult_div.h:

#ifndef MULT_DIV_H
#define MULT_DIV_H

// Prototypes
void were_dimensions_specified(int);

struct mult_div_values;

bool is_valid_dimensions(char*, char*);

mult_div_values** create_table(int m, int n);
void set_mult_values(mult_div_values** table, int m, int n);
void set_div_values(mult_div_values** table, int m, int n);
void delete_table(mult_div_values** table, int m);
void print_table(mult_div_values** table, int m, int n);

#endif

prog.cpp:

#include <iostream>
#include <string>
#include <stdio.h>
#include "mult_div.h"


using std::cout;
using std::cin;
using std::endl;
using std::string;

int main(int argc, char** argv){
bool see_table = true;

were_dimensions_specified(argc);
char* rows_char = argv[1];
char* cols_char = argv[2];

while(see_table){
bool valid_dimensions = is_valid_dimensions(rows_char, cols_char);
int rows = atoi(rows_char);
int cols = atoi(cols_char);

mult_div_values** table = create_table(rows, cols);
set_mult_values(table, rows, cols);
set_div_values(table, rows, cols);
print_table(table, rows, cols);
delete_table(table, rows);

cout << "Would you like to see a different size matrix (0-no, 1-yes)?"<<endl;
bool yesNo;
cin >> yesNo;
if(yesNo == false){
see_table = false;
}
else{
cout << "Please enter the number of rows: ";
cin >> rows_char;
cout << "Please enter the number of columnds: ";
cin >> cols_char;
}

}
return 0;
}

Sample Output Screenshots:


Related Solutions

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Create a game of Connect Four using a two dimensional array (that has 6 rows and...
Create a game of Connect Four using a two dimensional array (that has 6 rows and 7 columns) as a class variable. It should be user friendly and be a two person game. It should have clear directions and be visually appealing. The code MUST have a minimum of 3 methods (other than the main method). The player should be able to choose to play again. **Code must be written in Java**
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
You will put a possible magic square into a two dimensional array and determine if it...
You will put a possible magic square into a two dimensional array and determine if it is a magic square or not. Some of the code is completed. You can assume that it is a square array and that all of the integers are unique (no repeats) CODE: package TwoDimensionalArrays; public class MagicSquare {    public static boolean checkMagicSquare(int[][]array)    {   //Pre: Assume that the array is a square two dimensional array        //Pre: Assume that there are no...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT