Problem: Maximal Advertising Business is going well for your roommate, who is se
ID: 3679447 • Letter: P
Question
Problem: Maximal Advertising
Business is going well for your roommate, who is selling discounts to area clubs and restaurants, which means that business is going well for you! Your program for keeping track of everyone's accounts is working perfectly.
Both of you have decided that you'll advertise on memory mall, which is roughly arranged as a rectangle. You've decided that there's not enough time before a football game to get to everyone arranged on the mall, but that you will only be able to walk through one "row" or "column" of the huge grid for advertising purposes.
Naturally, you'd like to hit as many people as possible as you make your single walk through. For this assignment you'll complete a program that reads in the number of people in each "cell" of memory mall, which is arranged like a 20 x 5 grid of cells, and determines the maximum number of people in any row or column of the grid.
For example, (this example is on a smaller grid to illustrate the idea), if the following was the input grid, where 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.
Note: Since these numbers represent number of people in an area, it is guaranteed that all numbers for all input cases are non-negative.
Add your code in the designated area to the file marketing-scaffold.c.
After you add the correct code, if you run your program on the input file marketing.txt, your output should match the output in marketing.out.
(Here is the additional information)
marketing-scaffold.c. file
marketing.txt
marketing.out.
200 20 500 19 25 44 5 16 50 30 400 15 80 93 2 14 19 40 600 0 30 18 92 19Explanation / 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");
int loop, numCases;
int newSum1=0;
int newSum2=0;
int sumRows=0;
int sumCol=0;
int best1;
int best2;
int best;
fscanf(ifp, "%d", &numCases);
// Go through each input case.
for (loop=0; loop<numCases; loop++) {
// Get this input grid.
int grid[ROWS][COLS], i, j;
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.
int best = 0;
for (i=0; i<ROWS; i++)
{
for (j=0; j<COLS; j++)
{
sumRow= sumRow+grid[i][j];
}
if(newSum1 > sumRow)
best1= newSum1;
newSum1= sumRow;
}
for (j=0; j<COLS; j++)
{
for (i=0; i<ROWS; i++)
{
sumCol= sumCol+ grid[i][j];
}
if(newSum2 > sumCol)
best2= newSum2;
newSum2= sumCol;
}
if(best1> best 2)
best=best1;
else
best=best2;
// Output result.
printf("%d ", best);
}
fclose(ifp);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.