The program uses both files and two dimensional arrays. The Problem Statement Bu
ID: 3673177 • Letter: T
Question
The program uses both files and two dimensional arrays.
The Problem Statement
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.
MARKETING-SCAFFOLD.C
#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, best;
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;
/*** FILL IN CODE HERE, TO UPDATE BEST, AS NEEDED. ***/
// Output result.
printf("%d ", best);
}
fclose(ifp);
return 0;
}
Explanation / Answer
Here is the code after filling up the gap.
Also add code for declaring variables.
#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, best;
int colmax=0;
int rowmax =0;
int total=0;
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;
/*** FILL IN CODE HERE, TO UPDATE BEST, AS NEEDED. ***/
for (i=0; i<ROWS; i++)
{
for (j=0; j<COLS; j++)
{
total = total+ grid[i][j];
}
if(total < colmax)
colmax = total;
total =0;
}
total =0;
for (i=0; i<COLS; i++)
{
for (j=0; j<ROWS; j++)
{
total = total+ grid[i][j];
}
if(total < rowmax)
rowmax = total;
total =0;
}
if(rowmax > colmax)
best = rowmax;
else
best = colmax;
// Output result.
printf("%d ", best);
}
fclose(ifp);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.