gpt4 book ai didi

c - 使用数组的平均分数 - 由用户在一定时间内输入

转载 作者:行者123 更新时间:2023-11-30 18:10:09 26 4
gpt4 key购买 nike

想请您帮忙!!我在添加计时器和找到平均分方面遇到了困难。标记将由用户在特定时间输入。即每隔一分钟,用户将输入标记。第一分钟 - 输入第一个值

再等待 60 秒,然后

第二分钟 - 然后输入第二个值。第 3 分钟 - 然后输入第 3 个值。

因此用户输入的每第 n 个值相差 60 秒。

我已经添加了每一分钟倒计时,但在我输入 num=5 后输出给出了奇怪的值。

下面的代码添加了一分钟定时器:

printf("Enter the marks to find average: ");
scanf("%d",&num);//get input from user

// for num, i enter 5, so num=5 then 5 values should be entered
// time taken for entering 5 values will be 5 minutes.

//loop for get input from user
for(i=0; i<num; i++)
{
timer++;
//if timer is equal to 60 then, get 1st mark from user,
if (timer ==60)
{
printf("Enter marks %d: ",i+1);
scanf("%f", &arr[i]);
}
timer=0; //reset back to zero and loop again to get next marks!
}

包括计时器和查找平均值的完整代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main()
{
float arr[100];//array declaration-keep max to 100
int i,num;//variable declaration
double avg=0.0,sum=0.0;
static uint32_t timer = 0; //1min timer for getting marks

printf("Enter the marks to find average: ");
scanf("%d",&num);//get input from user

//if user enter "10", then 10 values should be entered 60secs apart.
// time taken for entering 10 values will be 10minutes.

//loop for get input from user
for(i=0; i<num; i++)
{
timer++;
//if timer is equal to 60 then, get 1st mark from user,
if (timer ==60)
{
printf("Enter marks %d: ",i+1);
scanf("%f", &arr[i]);
}
//timer=0; //reset back to zero
}
for(i=0; i<num; i++){ //loop calculating sum of array elements
sum=sum+arr[i];
avg=sum/num; //calculating average
}
printf("Average of entered numbers are: %.2f",avg);
getch();//display result on the screen
return 0;
}

输出如附件enter image description here

最佳答案

在循环中,计时器只会增加 5 次。找到下面正确的代码。

       for(i=0; i<num; i++)
{
printf("Enter marks %d: ",i+1);
scanf("%f", &arr[i]);
while(timer!=60)
timer++;
timer=0; //reset back to zero
}

您可以使用计时器功能来实现您的应用程序。 https://www.geeksforgeeks.org/time-delay-c/

关于c - 使用数组的平均分数 - 由用户在一定时间内输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com