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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:33:51+00:00 2026-06-18T01:33:51+00:00

My attempts to find a way to get the average of this array have

  • 0

My attempts to find a way to get the average of this array have so far been fruitless. Any help would be appreciated.

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main( )
{
    const int MAX = 100;
    double voltages[MAX];
    double average;
    ifstream thefile("c:\\voltages.txt");
        if(!thefile) 
    {
                cout<<"Error opening file"<<endl;
                system("pause");
                exit(1);
    }
        for(int count = 0; count < MAX; count++)
    {
                thefile >> voltages[count];
                cout << voltages[count++] << endl;
                average = voltages[count++]/count;
        if(count == 0 || thefile.eof())
    {
                break;
    }
    }
    cout << average;
    cout << "\n";
    system("PAUSE");
    return 0;
} 

voltages file is

100.8

120.4

121.4

111.9

123.4

but can have up to 100 doubles.

  • 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-18T01:33:53+00:00Added an answer on June 18, 2026 at 1:33 am

    To calculate the average of the numbers stored in any C++ container (even raw arrays), use the following approach. Take the sum and divide by the number of elements:

    std::accumulate(std::begin(v), std::end(v), 0.0) / (std::end(v) - std::begin(v));
    

    Example code in action:

    With std::vector /
    With raw array (Note that only the definition of the vector / array changes!)

    This code doesn’t check for zero length, which produces a division by zero. This will return a NaN value. This case can be detected in advance by using if (std::begin(v) == std::end(v)). You can handle such corner cases depending on your need if you don’t want to return NaN:

    • Return a well-defined value such as 0.0
    • Throw an exception
    • Print a warning and exit (essentially the same as exception but can’t be caught)
    • Custom error handling, for example set a boolean error variable to true
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my failed attempt at the problem any help would be appreciated. I
I would like to find an efficient way (not scanning the String 10,000 times,
I've just started programming and I've got this far but I've been struggling with
I have been banging my head on this one all day. The C++ project
I have searched & searched but I seem unable to find a way to
This code attempts to dynamically switch the class of the StateContainer div from StateOne
This code attempts to resize images in a directory called imgs. Unfortunately for some
Since PEAR attempts to get ConsoleTools from components.ez.no, which is not available anymore, how
I have seen various discussions and code attempts at solving the "String reduction" problem
I have been using eclipse-pdt in conjunction with xdebug and apache without problems, for

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.