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 9160651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:47:19+00:00 2026-06-17T13:47:19+00:00

I have a text file containing 200 numbers. I want to calculate the average

  • 0

I have a text file containing 200 numbers. I want to calculate the average value of those numbers (because it takes way too long to do it by hand). I have no idea how to accomplish this, because I have never done such a thing. Thanks in advance.

Edit: I tried searching google but couldnt find something usefull.

  • 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-06-17T13:47:20+00:00Added an answer on June 17, 2026 at 1:47 pm

    A simple C++ program would implement the following steps:

    1. Open the file as a text file (best use a command line argument as its file name, don’t hardcode it)
      • Use a stream rather than a plain old C file handle; there are a lot of different possibilities to read files
    2. Read it line by line and parse the line as a double value
    3. Either store the values and calculate the average after reading all numbers or calculate the average on the fly while reading
      • Storing the values should happen in a dynamic datastructure like std::vector, read about it
      • Calculating on the fly would mean to sum up all values and increment a counter; divide the sum by the counter at the end
      • Take it as an exercise to implement both possibilities; to calculate the average of a std::vector there are again a lot of possibilities; try to find 3 different solutions to learn good C++ style. (google for STL algorithms)
    4. Then print the result to the standard output with cout

    Have a look at these steps and ask concrete questions if you encounter problems.


    However, you don’t need a C++ program to do this. A spreadsheet program is the perfect tool for this.

    If you don’t like GUI programs for such tasks, SQLite is a perfect tool, too. Open the SQLite shell and type (assuming data.txt is your file with a decimal number on each line):

    create table tmp (value double);
    .import data.txt tmp
    select avg(value) from tmp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a text file I will have a line containing a series of numbers,
I have this file containing following text. 200.200.200.253 80/tcp:filtered:http 443/tcp:filtered:https 200.200.200.254 80/tcp:filtered:http 443/tcp:filtered:https web.example.com
I have a text file containing 10 columns of numbers. What I would like
Hi I have a text file containing two arrays and one value(all integers) like
I have a text file containing a giant list of line numbers which I
I have a text file containing three columns of numbers; one column each for
I have a text file containing the row numbers of the rows that should
I have a text file containing the first 1000 prime numbers and I have
I have a text file containing decimal numbers throughout it and was wanting to
I have text file containing a list of 16 bit hex numbers (e.g. '61C7393AA9B3474DB081C7B7CCE1C545')

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.