gpt4 book ai didi

c - 如何在C中显示所有用户输入的数字?

转载 作者:行者123 更新时间:2023-11-30 16:10:56 25 4
gpt4 key购买 nike

我需要创建一个使用数组的函数和一个显示以下内容的菜单系统:

输入的所有数字的总和、输入的数字的平均值以及输入的所有数字。它将允许用户输入最多 1000 个数字。

我已经完成了大部分代码的工作,我只需要弄清楚如何显示用户到目前为止输入的所有数字。有人能帮我解决这个问题吗?谢谢!

我尝试显示输入的数字,但这不符合作业要求。

这是我到目前为止的代码:

/*

Title: Array Intro
Author: James Henderson
Desc: a program designed to display the sume, average, and all previous numbers entered of user input numbers
Date: 11/06/19
*/

#include <stdio.h>
#include <math.h>

//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];

//Void Function

static void sumFunction(userInput)
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInput);

//switch statement
while (1)
{
switch (userInput)
{
case 1:

printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput);
//determine sum
number = userInput;
sum += number;
counter++;

average = sum / counter;

printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}

最佳答案

你的方法是对的,看看我如何使用userInput。下面的代码工作正常:

#include <stdio.h>
#include <math.h>

//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];

//Void Function

static void sumFunction()
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInt);

//switch statement
while (1)
{
switch (userInt)
{
case 1:

printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput[counter]);
//determine sum
number = userInput[counter];
sum += number;
counter++;

average = sum / counter;

printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);

// number entered so far
printf("\n The list of numbers entered so far : \n");
for(int i=0;i<counter;i++){
printf(" %d ",userInput[i]);
}
printf("\n");

if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}

int main(){
sumFunction();
}

关于c - 如何在C中显示所有用户输入的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58770736/

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