Question
Write a program that creates a dynamic 2D array of structs to store and print the multiplication and division tables for values between 1 and
Write a program that creates a dynamic 2D array of structs to store and print the multiplication and division tables for values between 1 and a number n specified by the user. Specifically, follow these steps to create the program:
-
Create a new file in your repository directory named muldiv.cpp. This is the file in which you will write your program.
-
Set your program up to read the number n as a command line argument. For example, if the user runs your program like this, you will have n = 5: ./muldiv 5 Make sure to check whether the user inputs a valid integer value, and exit the program with an error if they do not. The function atoi() will be helpful for this step, e.g.: n = atoi(argv[1]);
-
Write a struct like this one to store a single entry in both the multiplication table and the division table: struct muldiv_entry { int mul; float div; };
-
Write a program that uses n and your struct to generate and then print the multiplication and division tables for all values between 1 and n, inclusive. For example, if the user runs your program as follows, then it should print the multiplication and division tables like below: ./muldiv 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 Your program must be well modularized. Specifically, you should write and use the following functions:
-
struct muldiv_entry** generate_tables(int n) This function should allocate space for an n x n array of your structs and fill it with the appropriate multiplication and division values. It should return the 2D array it creates.
-
void print_tables(struct muldiv_entry** tables, int n) This function should take a 2D array as created by generate_tables() and its size and print out the multiplication and division tables like above.
-
void free_tables(struct muldiv_entry** tables, int n) This function should release all of the memory allocated to a 2D array created by generate_tables(), given the array and its size as arguments. This function is important. Your program should not have a memory leak.
-
-
Now, compile your program: g++ muldiv.cpp -o muldiv
Step 8: Factor out your implementation and interface
Some of the functions we wrote, along with our struct, might be useful in other contexts, so lets factor them into a more reusable form by separating the struct and function prototypes into a separate header file and the function definitions into a separate implementation file. Doing this will have other benefits, such as helping us keep our code more organized and allowing us to speed up compilation (by compiling only files that have changed since the last compilation). Follow these steps:
-
Create a new header file named muldiv.hpp and add to it the definition of your struct and all of your function prototypes: struct muldiv_entry { int mul; float div; }; struct muldiv_entry** generate_tables(int n); void print_tables(struct muldiv_entry** tables, int n); void free_tables(struct muldiv_entry** tables, int n); Once youve created this header file, you can remove the definition of your struct from your .cpp file. If you have prototypes separate from your function definitions in the .cpp file, you can remove those too. Replace all those by including your new header file: #include "muldiv.hpp" You should still be able to compile your program here as you did before: g++ muldiv.cpp -o muldiv
-
Create a new implementation file named muldiv_print.cpp. Copy your main() function from your original muldiv.cpp file, add it to this new file, and then remove it from muldiv.cpp. Youll need to include your muldiv.hpp header file in the new file, too. Now, youll need to compile both files together to be able to run them: g++ muldiv.cpp muldiv_print.cpp -o muldiv_print After compiling, you should be able to run your new executable: ./muldiv_print 5
-
After youre happy with your new files, use git add to stage them for commit, commit them using git commit, and push them to your remote repo on GitHub using git push.
-
Lets write a makefile for our program to simplify its compilation. Follow these steps:
-
Create a new file named Makefile. Add the following lines to this new file (note that the indentation must be a tab character): muldiv_print: g++ muldiv.cpp muldiv_print.cpp -o muldiv_print Now, if you type the command make into your terminal, you should see your code be compiled.
-
We can go further, factoring our compilation into different stages and specifying dependencies for each compile step, which will allow the make utility to run only the compilation steps it needs to based on which files have changed. We can also use variables to make it easier to change some values in the makefile, such as the compiler we want to use or the name of the executable file generated. Modify your Makefile to look like this: CC=g++ EXE_FILE=muldiv_print all: $(EXE_FILE) $(EXE_FILE): muldiv.o muldiv.hpp muldiv_print.cpp $(CC) muldiv.o muldiv_print.cpp -o $(EXE_FILE) muldiv.o: muldiv.hpp muldiv.cpp $(CC) -c muldiv.cpp Now if you run make again, you should notice your compilation happening in stages. We usually also add a makefile target for cleaning up our directory: clean: rm -f *.o $(EXE_FILE) You can run that makefile target by specifying it on the command line when you run make, i.e. make clean.
-
Once your makefile is working well, add and commit it to your local git repo, and push it to your remote repo on GitHub.
-
Step 9: Use a Makefile to compile your program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started