Instructions: Create a program that will collect multiple receipts from multiple classrooms to accumulate total cookie sales. Once all receipts have been processed, the program must show what classroom is won and the total amount of cookies they sold.
The classrooms that are participating are from the:
2nd Floor: 201, 202, 203, 204, 205, 206, 207, 208
3rd Floor: 301,302, 303, 304, 305, 306, 307, 308, 309
4th Floor: 401, 402, 403, 404, 408, 409, 410
In total 24 classrooms.
Instructions for writing the program:
MUST BE 2 arrays only
C program for the problem is as follows:
#include<stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main()
{
int hardArr[24]={201, 202, 203, 204, 205, 206, 207, 208, 301,302, 303, 304, 305, 306, 307, 308, 309, 401, 402, 403, 404, 408, 409, 410};
int candies[24]={0};
int classNo;
while(1)
{
printf("Enter classroom number. (OR Enter -1 to exit.)");
scanf("%d",&classNo);
if(classNo==-1)
break;
int ind=binarySearch(hardArr,0,23,classNo);
printf("Enter the number of cookies sold.");
int cookies;
scanf("%d",&cookies);
candies[ind]+=cookies;
}
int maxCandy=0;
int ind;
for(int i=0;i<23;i++)
{
if(candies[i]>maxCandy)
{
maxCandy=candies[i];
ind=i;
}
}
printf("Class Number %d has maximum number of Candies: %d",hardArr[ind],maxCandy);
return 0;
}
Screenshots of a test input are:
Get Answers For Free
Most questions answered within 1 hours.