you will create a dynamic two dimensional array ofmult_div_values structs (defined below). The two dimensional arraywill be used to store the rows and columns of a multiplication anddivision table. Note that the table will start at 1 instead ofzero, to prevent causing a divide-by-zero error in the divisiontable!
struct mult_div_values { int mult; float div;};
The program needs to read the number of rows and columns fromthe user as command line arguments. You should check that the useractually supplied a number before converting the input string to aninteger. Continue to prompt the user for correct values if thenumber isn't a valid non-zero integer. At the end of the program,prompt the user if they want to see this information for adifferent 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 linearguments
./prog 5 5
Your program should create a 5 by 5 matrix of structs and assignthe multiplication table to the mult variable in the struct and thedivision of the indices to the div variable in the struct. The multvariable 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 willhave a function that checks if the rows are valid, non-zerointegers:
bool is_valid_dimensions(char *m, char *n)
and another function that creates the matrix of structs giventhe m times n dimensions:
mult_div_values** create_table(int m, int n)
In addition, you need to have functions that set themultiplication and division values, as well as delete your matrixfrom 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 isfunctioning, test it using the following approach.
Example Run:
./prog 5 tYou did not input a valid column.Please enter an integer greater than 0 for a column: 5Multiplication Table:1 2 3 4 52 4 6 8 103 6 9 12 154 8 12 16 205 10 15 20 25Division Table:1.00 0.50 0.33 0.25 0.202.00 1.00 0.67 0.50 0.403.00 1.50 1.00 0.75 0.604.00 2.00 1.33 1.00 0.805.00 2.50 1.67 1.25 1.00Would 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 aglobal user-defined type, then we might want to begin making aninterface file that holds all of this information for us.
Create a mult_div.h interface file that will contain all thefunction 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 inyour implementation file (the one with a .cpp extension). You alsoneed to remove the prototypes and struct definition from yourimplementation file (e.g. delete the original prototype/structdefinition 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 functiondefinitions in the implementation file (mult_div.cpp), and put yourmain function in a separate file called \"prog.cpp\" Your prog.cppfile will have to #include the mult_div.h file as well. To putthese 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? Manuallycompiling all of them together would take forever and have a highchance for error. Luckily, UNIX/Linux has a built in script thatmakes compiling multiple files together easy called a Makefile.Just type
vim Makefile
on the command line to create it. Now modify the file followingthe pattern shown below:
: -o
Note that the leading whitespace MUST be a tab (you can't justuse spaces). An example of what this could look like for this labwould be:
mult_div: g++ mult_div.cpp prog.cpp -o mult_div
Save and exit the file. You can type \"make\" in the terminal torun it.
One of the other benefits of makefiles is that you can addvariables to it and make compiling happen in different stages bystopping g++ after compiling and before running it through thelinker. This creates object files (.o files), which you can linktogether
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.cppprog.o: prog.cpp $(CC) -c prog.cpp
Try to make your program again. Notice all the stages. Inaddition, we usually add a target for cleaning up ourdirectory:
clean: rm –f *.out *.o $(exe_file)
Now we can run the specific target by typing \"make.
make clean
Makefiles are a useful way to automate and control the programbuilding process as your projects grow in size.