In: Computer Science
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.
Working code implemented in C++ and appropriate comments provided for better understanding.
Here I am attaching code for these files:
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: