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

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you can find an example on this page on hacks.mozilla.org May 13, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer UPDATE The deprecation was removed today, in response to this… May 13, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer You are never using fetch() to actually execute the GqlQuery… May 13, 2026 at 8:12 pm

Related Questions

My application will have a per machine (not per user) Startup shortcut. I can
I need a window for my application that allows the user to: see currently
I am writing an application in C# that needs to run as a service
I'm currently using such blocks of code in my project public ActionStatus GetStuff(out output)
So, I'm writting a CLI application in C++ which will accept a bunch of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.