Search code examples
cuser-input

How to display all user input numbers in C?


I am required to create a function that uses arrays and a menu system that displays:

the sum of all numbers entered, the average of numbers entered, and all numbers entered. It will allow the user to enter up to 1000 numbers.

I have the majority of the code working, I just need to figure out how to display all of the numbers a user has entered so far. Would anyone be able to help me with this? Thanks!

I've tried displaying the number entered, but this does not fit the assignment requirements.

Here is the code I have so far:

/*

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;
                }
            }
        }
    }
}

Solution

  • Your approach was right, just look how i used userInput. Below code works fine:

    #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();
    }