Как отобразить все пользовательские номера ввода в C? - PullRequest
0 голосов
/ 08 ноября 2019

Мне необходимо создать функцию, которая использует массивы и систему меню, которая отображает:

сумму всех введенных чисел, среднее число введенных чисел и все введенные числа. Это позволит пользователю ввести до 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;
                }
            }
        }
    }
}

1 Ответ

0 голосов
/ 09 ноября 2019

Ваш подход был верным, просто посмотрите, как я использовал 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();
}
...