【发布时间】:2017-05-28 17:26:34
【问题描述】:
我在课堂上有一项作业,要求我将 30 个值硬编码到一个二维数组中(4 周 7 天,每天记录温度)。然后制作我的程序,以便它找到并打印最大值,以及记录它的日期和星期。但是,在 3 个不同的日子里有 3 个最大值。我假设必须打印所有 3 个案例。
我还是个初学者,以前没有遇到过这样的问题。我写了一个代码来打印 3 的一个最大值。下面提到:
#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;
}
你们能否帮我编写这段代码,如果有 x 个最大值,则所有 x 值的情况都会打印出它们的日期和星期。另外,因为我们还没有涵盖我班上的所有图书馆。他们希望我只使用 stdio.h 来编写程序。
感谢您花时间阅读。也提前感谢您的回复。
PS:如果你们能提出使用<stdio.h>、数组、指针和循环的解决方案,我将不胜感激,因为我还没有涉及到编码的其他方面。
【问题讨论】:
-
你所要做的就是在获得最大值后,再次遍历二维数组。如果值等于最大值,则在每个元素处打印它们的日、周和最大值(在循环内)!
标签: c arrays for-loop switch-statement stdio