logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jun16/multiTable.c

/*
This sets up a multiplication table using a 2 dimensional array
You can have as many dimensions in an array as you'd like, but in
this class, we will deal with at most 2 dimensions.
*/
#include <stdio.h>
int multiTable[15][15];
/*
first bracket refers to rows, second refers to columns.
This above is our two dimensional array that sets up the multiplication table.
We will have room for 15 rows and 15 columns, the indexes for each will go from
0 to 14.
In our makeTable function, we're putting the values we need into the array. Currently,
the loop goes from 0 to 12, as in main, we currently have the integer 12 hardcoded in
as the argument we're passing to makeTable.
Obviously, you can edit that by putting some other number in, but as it's written right now,
if you enter a number bigger than 14, the program will crash, as you'll end up accessing
memory that isn't allocated to us. Remember, the smallest index allocated to us is 0, and the
largest index that we can access is whatever the size we allocate is minus 1. In this case, the
largest index usable by us is 14. If you want to generate times tables bigger than 14, you'll
have to edit the numbers inside the brackets.
Also note that this array was declared outside of all the functions, this is a global variable,
which means that any function in this program can access it. (Read the TutorialsPoint page on Scope
for more information).
*/
void makeTable(int a){
//using a nested loop to iterate through the 2D array and perform an operation
//first row is row index 0, first column is column index 0
for(int i = 0; i <= a; i++){
for(int j = 0; j <= a; j++){
//printf("%4d", i*j);
/*
above code that's currently commented out will print the table,
the %4d (note, no decimal point) in the printf indicates that you want to use at least 4 spaces
to print out this intger. If the integer is 3 digits or less, then it gets padded with spaces
*/
multiTable[i][j] = i*j; //putting i*j into row i column j
}
//printf("\n"); //used to help print the table, currently commented out.
}
// also note that the datatype here is void, so we are not returning anything
}
int main(){
makeTable(12); //passing the integer 12 in to make a table that goes from 0 to 12
int a, b;
printf("Enter two numbers: ");//prompting for two numbers
scanf("%d %d", &a , &b);
printf("Calculated: %d * %d = %d\n", a, b, a * b);
printf("Lookup: %d * %d = %d\n", a, b, multiTable[a][b]);
/*
In the first printf statement above, we're using the multiplication operator to find the product of
a and b. We can do this in the printf statement as seen above.
In the second printf satement, we get the product by accessing the array we generated. We already did the
multiplication operations when we generated the table, now we're just using a and b to access the element
of the array. The value inside the element is the product of the index of the row, and the index of the column.
Suppose a = 5 and b = 7. The first printf statement merely multplies a * b and prints out 35.
The second printf statement takes the a and b, and prints out the value in multiTable[a][b], or in this case
multiTable[5][7], the value inside that is 35.
*/
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX