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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:46:19+00:00 2026-06-14T14:46:19+00:00

My task is to type and read() several double variables and store them in

  • 0

My task is to type and read() several double variables and store them in vector, then the function compute() should calculate the sum of all variables stored in the vector and their average. I have fixed these functions and they work well. The problem is with the final function print() .. the function should print out the result – ‘sum’ and ‘average’ variables. But my code for print() is not working properly and prints out wrong numbers. Here is my code:

#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <vector> 
using namespace std;

vector<double> read(){//this function works fine
    cout << "Write some numbers with space between them?" << endl;
    double numz;
    vector<double> myvector;
    do {
    cin >> numz;
    myvector.push_back (numz);
    } while (numz);
    return myvector;
}  

void compute(double average, double sum, vector<double> &myvector){//this function works fine
//if i put cout in this function it calculates correctly
    vector<double>::iterator it;
    for ( it=myvector.begin() ; it < myvector.end()-1; it++ ){
    sum += *it;
    }
    average = sum/myvector.size();

}

void print(double average, double sum){//this is printing out wrong numbers
    cout.precision(2); 
    cout << sum << endl;
    cout << fixed  << average << endl;
}


int main (){
    vector<double> myvector = read();
    double average;
    double sum;
    compute(average, sum, myvector);
    print(average, sum);//this is printing out wrong numbers

    system("pause");
  return 0;
}

Thanks in advance for any help, i understand that my mystake is in variable passing between functions but i have spent the whole day debugging and reading tutorials without any luck.

  • 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-14T14:46:20+00:00Added an answer on June 14, 2026 at 2:46 pm

    There are several flaws in your program:

    1. The declaration of compute maybe better to changed to:

      void compute(const vector<double> &myvector, double &average, double &sum);
      

      Passing a const vector<double>& makes it not modifiable in the function, and takes the references of average/sum makes changing the passing in arguments possible, see more of “formal argument” and “actual argument” here.

    2. You’d better declares function read as:

      void read(vector<double>& myVector);
      

      as this will avoid copying of the vector when function read returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is Thor task: desc 'readkeys', 'Read keys' method_option :password, :type => :string, :desc
I have a content type Task, where we create task for ourself or user
The task: I am trying to create a custom data type and have it
I'm writing a custom Ant task that needs to accept a custom nested type.
The task in my software involves several steps where each step may or may
I've read several questions regarding UAC and privilege elevation but I've not found a
I read the following statement: The x86 architecture includes a specific segment type called
How can I read any type of file from a location and access only
I am using a Task to read some data from the Database etc. Let's
I've got a task of creating a read-only user for our ASP.Net MVC3 application.

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.