Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Business is going well for your friend, who is selling discounts to area clubs a

ID: 3802386 • Letter: B

Question

Business is going well for your friend, who is selling discounts to area clubs and restaurants, which means that business is going well for you!  

Both of you have decided that you'll advertise on memory mall, which is roughly arranged as a rectangle. There's not enough time before a football game to hand flyers to everyone arranged on the mall. Therefore, you will only be able to walk through one "row" or "column" of the huge grid for advertising purposes.

Naturally, you'd like to give flyers to as many people as possible as you make your single walk through.

Program Setup

A scaffold of the solution has been created for you:

Place your solution in the designated area of the scaffold.

Memory mall is arranged like a 20 x 5 grid of tailgating locations. A representation of memory mall has been created using a two dimensional array. You will need to read from a file the number of people in each section or "cell" of memory mall. Then, you should determine which row or column of the grid has the most number of people in it.

Since these numbers represent the number of people in an area, it is guaranteed that all numbers for all input cases are non-negative.

Example

This example is on a smaller grid to illustrate the idea. Suppose the following is our input grid.

Each number represents the number of people tailgating in that cell. If we select the third column shaded blue, we would advertise to 1500 people. This is more than if we chose any of the other columns or rows.

Input File Format

The first line of the input file contains a single positive integer, n (n 1000), representing the number of grids to process.

This is followed by n 5x20 grids of integers.

Sample Run

After you add your code to designated location in the scaffold, run your program using the input file marketing.txt. Your output should match the output in marketing.out.

.out and .txt are unformatted files that can be opened with most text editors. Code::Blocks can also create and read files of these types.

Specification

Homework assignments are either correct or incorrect. Do not modify any of the printf statements in the scaffold. Do not add any printf statements to the program.

Submission

Test your program for correctness. Make sure you code is easy to read and that you include a header comment and comments throughout your main function.

200 20 500 19 25 44 5 16 50 30 400 15 80 93 2 14 19 40 600 0 30 18 92 19

Explanation / Answer

#include <stdio.h>
#define ROWS 20
#define COLS 5

int main() { // Open the input file and read in the number of cases to process.
FILE* ifp = fopen("marketing.txt", "r");
freopen("marketing.out", "w", stdout);
int loop, numCases, best, cur;
int grid[ROWS][COLS], i, j;
fscanf(ifp, "%d", &numCases);

// Go through each input case.
for (loop=0; loop<numCases; loop++) {
// Get this input grid.
for (i=0; i<ROWS; i++)
for (j=0; j<COLS; j++)
fscanf(ifp, "%d", &grid[i][j]);

// Will store best value for row or column.
best = 0;
for(i = 0; i < ROWS; ++i) {
int tmp = 0;
for(j = 0; j < COLS; ++j) {
tmp += grid[i][j];
}
if(tmp > best)
best = tmp;
}
for(i = 0; i < COLS; ++i) {
int tmp = 0;
for(j = 0; j < ROWS; ++j) {
tmp += grid[j][i];
}
if(tmp > best)
best = tmp;
}

/*** FILL IN CODE HERE, TO UPDATE BEST, AS NEEDED. ***/
// Output result.
printf("%d ", best);
}
fclose(ifp); return 0;

}