WRITE IT IN C!! Business is going well for your roommate, who is selling discoun
ID: 3676880 • Letter: W
Question
WRITE IT IN C!!
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 Times 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. 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.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main() {
// declare indexing variables
int i, j;
// open file to read input matrix
freopen("marketing.txt", "r", stdin);
// declare matrix of size 20x5
int matrix[20][5];
// read input matrix
for (i = 0; i < 20; ++i) {
for (j = 0; j < 5; ++j) {
matrix[i][j] = 0;
scanf("%d", &matrix[i][j]);
}
}
// initialize final ans to -1
int maxPeople = -1;
// iterate on matrix's rows
for (i = 0; i < 20; ++i) {
int sum = 0;
for (j = 0; j < 5; ++j) {
sum += matrix[i][j];
}
if (sum > maxPeople) maxPeople = sum;
}
// iterate on matrix's columns
for (i = 0; i < 5; ++i) {
int sum = 0;
for (j = 0; j < 20; ++j) {
sum += matrix[j][i];
}
if (sum > maxPeople) maxPeople = sum;
}
printf("Maximum number of people you can target is %d. ", maxPeople);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.