我需要创建一个使用数组和显示的菜单系统的函数
输入的所有数字的总和、输入的数字的平均值以及输入的所有数值。它将允许用户输入最多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("ntWelcome!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 numbersn", 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("ntWelcome!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 numbersn", 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();
}