Search code examples
carraysfor-loopswitch-statementstdio

how to record multiple max values from a 2D array in C using stdio.h


I have this homework from my class where I am asked to hard-code 30 values into a 2D array (4 weeks, 7 days where temperature was recorded daily). Then make my program so that it finds and prints the max value, and the day&week it was recorded. However there are 3 max values on 3 separate days. I assume that all 3 cases must be printed.

ScreenShot of the question is here. I am still a beginner and I haven't encountered a problem like this before. I wrote a code to print one max value of the 3. It is mentioned below:

#include <stdio.h>

#define y 4
#define x 7

int main()
{
    int max = 0, week, i, j, day;
    int table[y][x] = {
        {32,31,30,31,32,33,32},
        {33,32,34,35,34,36,36},
        {34,34,36,36,37,38,38},
        {38,37,36,35,34,33,32}};

    for (i = 0; i < y; i++)
    {
        for (j = 0; j < x; j++)
        {
            if (max <= table[i][j])
            {           
                max = table[i][j];
                day = j + 1;
                week = i + 1;           
            }
        }
    }

    switch (day)
    {
        { case 1: printf("The highest temperature %d was recorded on Monday of week %d\n", max, week); break; }
        { case 2: printf("The highest temperature %d was recorded on Tuesday of week %d\n", max, week); break; }
        { case 3: printf("The highest temperature %d was recorded on Wednesday of week %d\n", max, week); break; }
        { case 4: printf("The highest temperature %d was recorded on Thursday of week %d\n", max, week); break; }
        { case 5: printf("The highest temperature %d was recorded on Friday of week %d\n", max, week); break; }
        { case 6: printf("The highest temperature %d was recorded on Saturday of week %d\n", max, week); break; }
        { case 7: printf("The highest temperature %d was recorded on Sunday of week %d\n", max, week); break; }
    }
    return 0;
}

Can you guys help me to write this code in a way that if there are x number of max values, all cases of x values be printed with their days and week. Also since we haven't covered all libraries in my class. They expect me to only use stdio.h to write programs.

Thank you for taking time to read. also thank you in advance for your replies.

PS: I would really appreciate if you guys can suggest solutions with using, <stdio.h>, arrays, pointers and loops, because I haven't still covered other aspects of coding yet.


Solution

  • Your code is functional and it does produce the last day with highest temperature recorded. Yet here are some areas of improvement:

    • Avoid defining simple lowercase identifiers such as x and y with #define.
    • You should use an array for the names of the days of the week to avoid a lengthy switch statement with lots of redundant code.
    • You should initialize max to the value of the first temperature recorded: as coded, you would fail to find the largest if all temperatures were negative.

    You noticed the maximum temperature was recorded on multiple days, this is a very good point and the specification is somewhat ambiguous regarding what to output. Producing multiple results may not be what is expected, but certainly a bonus if the assignment is verified by hand.

    To print all matching days, first determine the maximum temperature, then use a second loop to enumerate the days it was recorded.

    Here is a modified version:

    #include <stdio.h>
    
    #define NWEEKS 4
    #define WEEKDAYS 7
    
    int main(void) {
        int max, week, day;
        int table[NWEEKS][WEEKDAYS] = {
            { 32, 31, 30, 31, 32, 33, 32 },
            { 33, 32, 34, 35, 34, 36, 36 },
            { 34, 34, 36, 36, 37, 38, 38 },
            { 38, 37, 36, 35, 34, 33, 32 }};
        const char *dayname[WEEKDAYS] = {
            "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", "Sunday" };
    
        max = table[0][0];
        for (week = 0; week < NWEEKS; week++) {
            for (day = 0; day < WEEKDAYS; day++) {
                if (max < table[week][day]) {           
                    max = table[week][day];
                }
            }
        }
        printf("The highest temperature is %d\n", max);
        for (week = 0; week < NWEEKS; week++) {
            for (day = 0; day < WEEKDAYS; day++) {
                if (table[week][day] == max) {
                    printf("It was recorded on %s of week %d\n",
                           dayname[day], week + 1);
                }
            }
        }
        return 0;
    }