In: Computer Science
USING C PROGRAMMING
***CODE MUST BE MODULARIZED**
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:
C code:
#include <stdio.h>
#define SIZE 24
// Function to return index of maximum value in the array
int findMaxIndex(int arr[], int n) {
    int idx = 0;
    for (int i = 0; i < n; i++) {
        if (arr[idx] < arr[i]) {
            idx = i;
        }
    }
    return idx;
}
// Function to return index of given classroom
int findClassroomIndex(int arr[], int n, int classroom) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == classroom) {
            return i;
        }
    }
    return -1;  // Classroom not found
}
int main() {
    // Hard coded parallel arrays
    int classrooms[SIZE] = {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 cookiesSold[SIZE] = {0};
    int classroom, classroomIdx, cookies;
    // Get the classroom number
    printf("Enter a classroom number or -1 to exit: ");
    scanf("%d", &classroom);
    // Keep asking for classroom number till user enters -1
    while (classroom != -1) {
        // Find the index of classroom number
        classroomIdx = findClassroomIndex(classrooms, SIZE, classroom);
        // Get the number of cookies sold
        printf("Enter the number of cookies sold: ");
        scanf("%d", &cookies);
        // Add cookies sold to the corresponding classroom
        cookiesSold[classroomIdx] += cookies;
        // Get the classroom number
        printf("Enter a classroom number or -1 to exit: ");
        scanf("%d", &classroom);
    }
    // Find the index of classroom that sold the maximum cookies
    int maxIdx = findMaxIndex(cookiesSold, SIZE);
    int winningClassroom = classrooms[maxIdx];
    int totalCookies = cookiesSold[maxIdx];
    // Display the winning classroom
    printf("Class number %d won and sold a total of %d cookies\n", winningClassroom, totalCookies);
    return 0;
}
Sample output:

Kindly rate the answer and for any help just drop a comment