Algorithmic Thinking
Here is how I would tackle this problem:
- Write the algorithm in my own words.
- Find out how to generate random numbers in the C language.
- Learn how to print information on the screen.
Algorithm
The algorithm is the set of steps you need to solve the problem. The task at hand already describes the problem, but it is often good practice to re-write it in your own words. (As a practical point, you can then take your words to your client -- in this case, your teacher -- and confirm that your understanding of the problem is correct.)
- Create a 2D array
- Populate the array with random numbers.
- Calculate the sum of each row of numbers (need a row sum counter).
- Calculate the sum of each column of numbers (need a column sum counter).
- Print the 2D array to the screen.
- Print the sum of each row at the end of each row.
- Print the sum of each column at the end of each column.
Assumption: Neither sum of sums are printed. (For example, the sum of the column sum.)
Generate Random Numbers
Google is helpful here. Try a Google search for:
generate random integers C
You will find lots of help, especially tips about the rand() function. Modify the Google search:
generate random integers C rand()
This search finds a great resource: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Print Information
Again, a Google search can help here:
print information on the screen in C
This yields: http://www.daniweb.com/software-development/c/threads/9688
The printf function seems handy. Find out more about it:
printf C
This yields a familiar site: http://www.cplusplus.com/reference/clibrary/cstdio/printf/
Development
If you really want to "wow" your professor:
- Identify the parts of your program that are most likely to change and make them constants.
- Separate your program into logical areas (called functions).
- Use meaningful variable names. Yes
r and c likely represent rows and columns, but for the amount of time it takes to spell out row and column, it will save anyone reading your code from having to either make a guess or read the code to discover its true use.
- Tell your professor that
qqqqq could use a better name. Suggest one, even.
For example:
#include <stdio.h>
/* Subject to change; only change it in one spot. */
#define MAX_ROWS 6
#define MAX_COLS 8
#define MIN_RANDOM_NUMBER 1
#define MAX_RANDOM_NUMBER 15
/** Returns a number between MIN_RANDOM_NUMBER and MAX_RANDOM_NUMBER. */
int randomNumber() {
return 0; /* FIX ME to use rand() and the modulus operator. */
}
int main( int argc, char *argv[] ) {
int qqqqq[MAX_ROWS][MAX_COLS];
/* FIX ME: Move these variables into the display function. */
int sumRows = 0;
int sumCols = 0;
/* Use different random numbers each time the program runs. */
seedRandomNumber();
/* Initialize the array with random numbers. */
for( int row = 0; row < MAX_ROWS; row++ ) {
for( int col = 0; col < MAX_COLS; col++ ) {
qqqqq[row][col] = randomNumber();
}
}
/* Display the array to the screen along with the sum totals. */
display( qqqqq );
}
Note that you have a choice to make.
You could pass the sumRows variable into the display function, or you could code the display function to call calculateSumRows itself. My preference is to always simplify the function prototypes. That is, reduce the number of parameters. It makes things easier to change in the future.
So write display as something like:
void display( int randomValues[MAX_ROWS][MAX_COLS] ) {
int sumCols = 0;
for( int row = 0; row < MAX_ROWS; row++ ) {
/* FIX ME: Write the calculateSumCols function. */
sumCols = calculateSumCols( randomValues, row );
for( int col = 0; col < MAX_COLS; col++ ) {
/* FIX ME: Use printf here to display the value at this row/column. */
}
}
/* FIX ME: Use printf here to display sumRows. */
for( int col = 0; col < MAX_COLS; col++ ) {
/* FIX ME: Use printf here to display the value of the rows. */
printf( "%d ", calculateSumRows( randomValues, col ) );
}
}
That should get you started.
Note that there are a number of simplifications and optimizations you could make here. Forget them. Get the code so that it actually works first. Once the code works, save it. Make a back up copy, even.
Then start to change it. See if you can remove variables. Perhaps you can even remove some of the for loops. If you make a mistake, you can always reference your back up copy. Eventually your "back up" copy will become a "source code repository".
%operator to get numbers between 1 to 15.randto a different range; it works well enough for class assignments, but depending on the RNG used you may get a decidedly non-random distribution of values. A better method is:val = (int)((rand() / (double) RAND_MAX) * (max - min + 1) + min);