Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:00:04+00:00 2026-05-13T14:00:04+00:00

Currently my program allows the user to enter 5 integers which are used to

  • 0

Currently my program allows the user to enter 5 integers which are used to create an average number. This is set to five as after the fifth number is entered the loop is broken.

I am trying to implement a method which will let the user continue to add as many numbers as they like to an array from which i can then use to create an average without a limit on the amount of numbers that can be entered.

I have come across a few problems, firstly i cannot create an array which is dyamic as i have no idea how many numbers the user may wish to enter which means i can’t give it a definitive size.

Secondly the way my program currently creates the average is by looping through the elements in the array and adding the consecutively to an integer, from which the the average is made. I cannot specify the limit for the loop to continue running if i cannot determine the array.

Hopefully my example explains this better.

#include <stdio.h>
#include <string.h>

void main()
{
    int i = 0;
    int arrayNum[5];
    int temp = 1;
    int anotherTemp = 0;
    int answer = 0;


    printf("Enter as many numbers as you like, when finished enter a negative number\n");

    for(i = 0; i < 5; i++)
    {
        scanf("%d", &temp);

        arrayNum[i] = temp;

        anotherTemp = anotherTemp + arrayNum[i];
    }

    answer = anotherTemp / 5;

    printf("Average of %d,%d,%d,%d,%d = %d",arrayNum[0],arrayNum[1],arrayNum[2],arrayNum[3],arrayNum[4],answer);
}

Although this may not be the best way to implement it, it does work when the amount of numbers are specified beforehand.

What would be the best way to get around this and allow the user to enter as many number as necessary?

Edit: Although i needed to use an array I have decided that it is not necessary as the solution is much simpler without being restricted to it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:00:05+00:00Added an answer on May 13, 2026 at 2:00 pm

    In terms of code simplicity, you might want to check out the realloc() function; you can allocate an initial array of some size, and if the user enters too many numbers call realloc() to get yourself a bigger array and continue from there.

    You don’t, however, actually need to keep the numbers as you go along at all, at least if you only care about the average:

    int input;
    int sum = 0;
    int count = 0;
    int average;
    
    while (1) {
        scanf("%d", &input);
        if (input < 0) {
            break;
        }
        sum += input;
        count++;
    }
    
    average = sum / count;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a small Java program which I would like to run both
I'm currently working on a program that has many of those the user SHOULD
I'm currently trying to pass a mono threaded program to multithread. This software do
How can I check the umask of a program which is currently running? [update:
I'm currently designing a program that will involve some physics (nothing too fancy, a
I am currently trying to program my first ajax interface using Rails. The application
I'm currently writing a program to generate really enormous (65536x65536 pixels and above) Mandelbrot
The program that I am currently assigned to has a requirement that I copy
So I am currently learning C++ and decided to make a program that tests
I'm currently trying to extend a friend's OCaml program. It's a huge collection of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.