如何使用 stdio.h 在 C 中记录二维数组中的多个最大值
how to record multiple max values from a 2D array in C using stdio.h
我的作业来自 class,我被要求将 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 值的情况及其日期和星期。此外,由于我们没有涵盖我的 class 中的所有库。他们希望我只用stdio.h写程序
感谢您花时间阅读。也提前感谢您的回复。
PS:如果你们能提出使用 <stdio.h>
、数组、指针和循环的解决方案,我将不胜感激,因为我还没有涵盖编码的其他方面。
试试这个。如果有多个,则将天数的详细信息存储在链表中。
#include<stdio.h>
#define y 4
#define x 7
typedef struct record{
int week;
int day;
record* next;
}dayRecord;
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}};
dayRecord* list = NULL;
for(i=0; i<y ; i++ )
{
for(j=0; j<x ; j++)
{
if(max<table[i][j])
{
max=table[i][j];
dayRecord* d = malloc(sizeof(dayRecord));
d->day=j+1;
d->week=i+1;
d->next = NULL;
list = d;
}else if(max == table[i][j]){
dayRecord* d = malloc(sizeof(dayRecord));
d->day=j+1;
d->week=i+1;
d->next = list;
list = d;
}
}
}
while(list->next != NULL){
int day = list->day;
int week = list->week;
list = list->next;
switch(day)
{
{case 1: printf("The highesest tempreture %d was recorded on Monday of week %d\n",max,week);break;}
{case 2: printf("The highesest tempreture %d was recorded on Tuesday of week %d\n",max,week);break;}
{case 3: printf("The highesest tempreture %d was recorded on Wednesday of week %d\n",max,week);break;}
{case 4: printf("The highesest tempreture %d was recorded on Thursday of week %d\n",max,week);break;}
{case 5: printf("The highesest tempreture %d was recorded on Friday of week %d\n",max,week);break;}
{case 6: printf("The highesest tempreture %d was recorded on Saturday of week %d\n",max,week);break;}
{case 7: printf("The highesest tempreture %d was recorded on Sunday of week %d\n",max,week);break;}
}
}
return 0;
}
如果你是初学者,我会说最好的办法是拥有 2 套迭代器。
迭代器基本上是
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
// Do something here.
}
}
所以你要做的是,在第一个迭代器中找到最大温度,并在第二个迭代器中使用 if 条件将最大值与当前值相匹配。如果值匹配,运行 您已经编写的 switch case。
这样您就不必编写复杂的 link 列表。
不过,如果你想在编程中有一个载体,强烈建议使用 link 列表。
您的代码可以正常运行,它确实产生了记录最高温度的最后一天。但这里有一些需要改进的地方:
- 避免使用
#define
. 定义简单的小写标识符,例如 x
和 y
- 您应该使用一个数组来表示星期几的名称,以避免包含大量冗余代码的冗长
switch
语句。
- 您应该将
max
初始化为记录的第一个温度的值:如编码所示,如果所有温度均为负值,您将无法找到最大值。
您注意到最高温度是连续几天记录的,这是一个很好的观点,规范在输出什么方面有些含糊。产生多个结果可能不是预期的,但如果作业是手工验证的,肯定是一个奖励。
要打印所有匹配的日期,首先确定最高温度,然后使用第二个循环枚举记录的日期。
这是修改后的版本:
#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;
}
我的作业来自 class,我被要求将 30 个值硬编码到二维数组中(4 周 7 天,每天记录温度)。然后制作我的程序,以便它找到并打印最大值,以及记录的日期和星期。但是,在 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 值的情况及其日期和星期。此外,由于我们没有涵盖我的 class 中的所有库。他们希望我只用stdio.h写程序
感谢您花时间阅读。也提前感谢您的回复。
PS:如果你们能提出使用 <stdio.h>
、数组、指针和循环的解决方案,我将不胜感激,因为我还没有涵盖编码的其他方面。
试试这个。如果有多个,则将天数的详细信息存储在链表中。
#include<stdio.h>
#define y 4
#define x 7
typedef struct record{
int week;
int day;
record* next;
}dayRecord;
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}};
dayRecord* list = NULL;
for(i=0; i<y ; i++ )
{
for(j=0; j<x ; j++)
{
if(max<table[i][j])
{
max=table[i][j];
dayRecord* d = malloc(sizeof(dayRecord));
d->day=j+1;
d->week=i+1;
d->next = NULL;
list = d;
}else if(max == table[i][j]){
dayRecord* d = malloc(sizeof(dayRecord));
d->day=j+1;
d->week=i+1;
d->next = list;
list = d;
}
}
}
while(list->next != NULL){
int day = list->day;
int week = list->week;
list = list->next;
switch(day)
{
{case 1: printf("The highesest tempreture %d was recorded on Monday of week %d\n",max,week);break;}
{case 2: printf("The highesest tempreture %d was recorded on Tuesday of week %d\n",max,week);break;}
{case 3: printf("The highesest tempreture %d was recorded on Wednesday of week %d\n",max,week);break;}
{case 4: printf("The highesest tempreture %d was recorded on Thursday of week %d\n",max,week);break;}
{case 5: printf("The highesest tempreture %d was recorded on Friday of week %d\n",max,week);break;}
{case 6: printf("The highesest tempreture %d was recorded on Saturday of week %d\n",max,week);break;}
{case 7: printf("The highesest tempreture %d was recorded on Sunday of week %d\n",max,week);break;}
}
}
return 0;
}
如果你是初学者,我会说最好的办法是拥有 2 套迭代器。
迭代器基本上是
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
// Do something here.
}
}
所以你要做的是,在第一个迭代器中找到最大温度,并在第二个迭代器中使用 if 条件将最大值与当前值相匹配。如果值匹配,运行 您已经编写的 switch case。
这样您就不必编写复杂的 link 列表。
不过,如果你想在编程中有一个载体,强烈建议使用 link 列表。
您的代码可以正常运行,它确实产生了记录最高温度的最后一天。但这里有一些需要改进的地方:
- 避免使用
#define
. 定义简单的小写标识符,例如 - 您应该使用一个数组来表示星期几的名称,以避免包含大量冗余代码的冗长
switch
语句。 - 您应该将
max
初始化为记录的第一个温度的值:如编码所示,如果所有温度均为负值,您将无法找到最大值。
x
和 y
您注意到最高温度是连续几天记录的,这是一个很好的观点,规范在输出什么方面有些含糊。产生多个结果可能不是预期的,但如果作业是手工验证的,肯定是一个奖励。
要打印所有匹配的日期,首先确定最高温度,然后使用第二个循环枚举记录的日期。
这是修改后的版本:
#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;
}