how to change vector elements by passing them to a function

I've got a c++ exam coming up soon, so I decided to study by writing a random program that would implement most of the material that's been covered in the class so far. What I came up with is a program that reads numerical data from an input file, copies it to a vector, performs a few calculations via a few custom functions, sorts the vector from lowest value to highest, and displays the data in standard output.

Everything works fine, except for the sorting function. I can't seem to figure out how to change the values of a vector. When running the program the vector elements stay in their original order. I'm pretty sure I need to reference the vector in order for the function to be able to change the actual data, but I can't figure out how to do so properly.

Here's the code (it requires a file named "input.txt" to work) :

https://pastebin.com/5Q8GP3mY

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
void icountSumAvg(vector<double>, int, int &icount, double &sum, double &avg);
void display(int, double, double);
void display(int, double, double, vector<double>, int);
void swap(vector<double>, int);

int main()
{
    ifstream ifile;
    const string FILENAME = "input.txt";
    ifile.open(FILENAME);
    int count = 0;
    double sum = 0, avg = 0;
    if (!(ifile.fail()))
    {
        bool testSwap = true; // displays each element if true
        vector<double> vect;
        double value = 0.0;
        while (!ifile.eof())
        {
            ifile >> value;
            vect.push_back(value);
        }
        int vectSize = vect.size();

        icountSumAvg(vect, vectSize, count, sum, avg);

        if (testSwap == true)
        {
            swap(vect, vectSize);
            display(count, sum, avg, vect, vectSize);
        }
        else
            display(count, sum, avg);

        ifile.close();
    }
    else
        cout << "There was an error opening the file: " << FILENAME << endl;
    system("pause");
}

void icountSumAvg(vector<double> values, int size, int &icount, double &sum, double &avg)
{
    for (int i = 0; i < size; i++)
    {
        icount++;
        sum += values[i];
    }
    avg = sum / icount;
}

void swap(vector<double> values, int size)
{
    double temp;
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (values[j] < values[i])
            {
                temp = values[i];
                values[i] = values[j];
                values[j] = temp;
            }
        }
    }
}

void display(int count, double sum, double avg, vector<double> values, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << "Element " << i + 1 << ": " << values[i] << endl;
    }
    cout <<"Total count:        " << count << endl
        << "Total sum:          " << sum << endl
        << "Average of numbers: " << avg << endl;
}

void display(int count, double sum, double avg)
{
    cout <<"Total count:        " << count << endl
        << "Total sum:          " << sum << endl
        << "Average of numbers: " << avg << endl;
}

2 answers

  • answered 2019-11-14 05:27 codtiger

    In the swap function, the values function doesn't receive a pointer/reference to the vector object, therefore accepting a copy of the object, and as the method is void you are just changing the value of the copy not the object itself.

  • answered 2019-11-14 05:35 seccpur

    You're swap() function is only modifying the vector copied to the function. Send the vector by reference to the swap() function to get the desire effect. This will avoid extra copying also.

    Since icountSumAvg() and display() is not supposed to modify the vector parameter, declare them as const reference too like so:

    void swap(vector<double>&, int);
    void icountSumAvg(const vector<double>&, int, int &icount, double &sum, double &avg);
    void display(int, double, double, const vector<double>&, int);