-2

in a function in my code, I used two separate for-loops to calculate the total sales for each brand but the output of the second for-loop is wrong.

this is my output :

Number of Sales by Car Brand (2001-2010)
----------------------------------------
Year     Proton     Perodua     Total
----------------------------------------
2001      76568       99342    175910
2002      92539      181903    274442
2003      93567      177422    270989
2004      87993       99856    187849
2005     101234      188230    289464
2006     103975      168911    272886
2007     100672      200152    300824
2008      99456      180453    279909
2009     100821      199654    300475
2010     109716      200894    310610
----------------------------------------
Average  96654.1     200743    266337
----------------------------------------
LOWEST : Proton (76568) in year 2001
HIGHEST : Perodua (200894) in year 2010

this is the correct output :

Number of Sales by Car Brand (2001-2010)
----------------------------------------
Year Proton Perodua Total
----------------------------------------
2001 76568 99342 175910
2002 92539 181903 274442
2003 93567 177422 270989
2004 87993 99856 187849
2005 101234 188230 289464
2006 103975 168911 272886
2007 100672 200152 300824
2008 99456 180453 279909
2009 100821 199654 300475
2010 109716 200894 310610
----------------------------------------
Average 96654.1 169682 266336
----------------------------------------
LOWEST: Proton (76568) in year 2001
HIGHEST: Perodua (200894) in year 2010

the actual average of the total sales for Perodua :

169682

the value im getting :

200743

this is the part having issues :

Average calcAvrg(int sales_proton[10], int sales_perodua[10])
{
    int sum_proton, sum_perodua;
    double avg_proton, avg_perodua;
    
    for(int i = 0; i < 10; i++)
    {
        sum_proton = sum_proton + sales_proton[i];
    }
    
    avg_proton = sum_proton / 10.00;
    
    for(int count = 0; count < 10; count++)
    {
        sum_perodua = sum_perodua + sales_perodua[count];
    }
    
    avg_perodua = sum_perodua / 10.00;
    
    Average avrg;
    
    avrg.avrg_proton = avg_proton;
    avrg.avrg_perodua = avg_perodua;
    
    return avrg;
}

here's my full code :

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

struct Average
{
    double avrg_proton;
    double avrg_perodua;
};

void getInput(int [], int [], int []);
Average calcAvrg(int [], int []);
void dispAnalysis(int [], int [], int []);
void dispOutput(int [], int [], int []);

int main()
{
    int year[10];
    int num_sales_proton[10];
    int num_sales_perodua[10];
    
    getInput(year, num_sales_proton, num_sales_perodua);
    
    dispOutput(year, num_sales_proton, num_sales_perodua);
    
    return 0;
}

void getInput(int yr[10], int sales_proton[10], int sales_perodua[10])
{
    ifstream textFile;
    
    textFile.open("input.txt");
    
    if (!textFile)
    {
        cout << "ERROR! The " << textFile << " file cannot be found or read!" << endl;
        exit(EXIT_FAILURE);
    }
    
    int i = 0;
    
    while(i < 10 && textFile >> yr[i] >> sales_proton[i] >> sales_perodua[i])
    {
        i += 1;
    }
    
    textFile.close();
}

Average calcAvrg(int sales_proton[10], int sales_perodua[10])
{
    int sum_proton, sum_perodua;
    double avg_proton, avg_perodua;
    
    for(int i = 0; i < 10; i++)
    {
        sum_proton = sum_proton + sales_proton[i];
    }
    
    avg_proton = sum_proton / 10.00;
    
    for(int count = 0; count < 10; count++)
    {
        sum_perodua = sum_perodua + sales_perodua[count];
    }
    
    avg_perodua = sum_perodua / 10.00;
    
    Average avrg;
    
    avrg.avrg_proton = avg_proton;
    avrg.avrg_perodua = avg_perodua;
    
    return avrg;
}

void dispAnalysis(int yr[10], int sales_proton[10], int sales_perodua[10])
{
    int lowest, highest, lowest_proton = sales_proton[0], highest_proton = sales_proton[0], lowest_perodua = sales_perodua[0], highest_perodua = sales_perodua[0];
    int year_lowest, year_highest, year_lowest_proton = yr[0], year_highest_proton = yr[0], year_lowest_perodua = yr[0], year_highest_perodua = yr[0];
    string brand_lowest, brand_highest;
    
    for(int i = 0; i < 10; i++)
    {
        if(sales_proton[i] < lowest_proton)
        {
            lowest_proton = sales_proton[i];
            year_lowest_proton = yr[i];
        }
        
        if(sales_proton[i] > highest_proton)
        {
            highest_proton = sales_proton[i];
            year_highest_proton = yr[i];
        }   
    }
    
    for(int i = 0; i < 10; i++)
    {
        if(sales_perodua[i] < lowest_perodua)
        {
            lowest_perodua = sales_perodua[i];
            year_lowest_perodua = yr[i];
        }
        
        if(sales_perodua[i] > highest_perodua)
        {
            highest_perodua = sales_perodua[i];
            year_highest_perodua = yr[i];
        }
    }
    
    if(lowest_proton < lowest_perodua)
    {
        lowest = lowest_proton;
        brand_lowest = "Proton";
        year_lowest = year_lowest_proton;
    }
    
    else
    {
        lowest = lowest_perodua;
        brand_lowest = "Perodua";
        year_lowest = year_lowest_perodua;
    }
    
    if(highest_proton > highest_perodua)
    {
        highest = highest_proton;
        brand_highest = "Proton";
        year_highest = year_highest_proton;
    }
    
    else
    {
        highest = highest_perodua;
        brand_highest = "Perodua";
        year_highest = year_highest_perodua;
    }
    
    cout << "LOWEST : " << brand_lowest << " (" << lowest << ") in year " << year_lowest << endl;
    cout << "HIGHEST : " << brand_highest << " (" << highest << ") in year " << year_highest << endl;
}

void dispOutput(int yr[10], int sales_proton[10], int sales_perodua[10])
{
    int total_per_year[10], overall_total;
    double avrg_total;
    
    cout << "Number of Sales by Car Brand (2001-2010)" << endl;
    cout << "----------------------------------------" << endl;
    
    cout << setw(4) << "Year";
    cout << setw(6) << "     Proton";
    cout << setw(6) << "     Perodua";
    cout << setw(6) << "     Total" << endl;
    cout << "----------------------------------------" << endl;
    
    for(int i = 0; i < 10; i++)
    {
        total_per_year[i] = sales_proton[i] + sales_perodua[i];
    }
    
    for(int count = 0; count < 10; count++)
    {
        cout << setw(4) << right << yr[count] << "     ";
        cout << setw(6) << right << sales_proton[count] << "      ";
        cout << setw(6) << right << sales_perodua[count] << "    ";
        cout << setw(6) << right << total_per_year[count]<< endl;
    }
    
    cout << "----------------------------------------" << endl;
    
    Average a = calcAvrg(sales_proton, sales_perodua);
    
    for(int i = 0; i < 10; i++)
    {
        overall_total = overall_total + total_per_year[i];
    }
    
    avrg_total = overall_total / 10.00;
    
    cout << "Average  " << a.avrg_proton << "     " << a.avrg_perodua << "    " << avrg_total << endl;
    cout << "----------------------------------------" << endl;
    
    dispAnalysis(yr, sales_proton, sales_perodua);
}

I tried removing the first for-loop and the output came out correct so I think the problem is what values the loop is adding for the total sales of Perodua. I am not sure how to go about this as I'm pretty new to C++ so it would be great if someone could help

Thanks

Shalu
  • 1
  • 2
  • You are using uninitialized variable `sum_proton` and so have undefined behavior in `sum_proton = sum_proton + sales_proton[i];` – Jason Jul 28 '22 at 13:04
  • 2
    Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jul 28 '22 at 13:11
  • ahh okay thank you for the advice, i'll try this if I encounter a problem in the future again @AndreasWenzel – Shalu Jul 28 '22 at 13:17

2 Answers2

1

You forgot to initialize the variables sum_proton and sum_perodua, so you are adding your values to their indeterminate values.

Initialize the variables before adding like this:

int sum_proton = 0, sum_perodua = 0;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

The problem is that you're using uninitialized variable in sum_proton in sum_proton = sum_proton + sales_proton[i]; leading to undefined behavior.

To solve this, initialize built in types:

int sum_proton =0, sum_perodua =0;
double avg_proton =0, avg_perodua =0;
Jason
  • 36,170
  • 5
  • 26
  • 60
  • It is not necessary to initialize `avg_proton` and `avg_perodua`. It is only necessary to initialize `sum_proton` and `sum_perodua`. Unnecessarily initializing variables may have a negative performance impact. – Andreas Wenzel Jul 28 '22 at 15:01
  • @AndreasWenzel Yes, i know. But as a rule of thumb i always initialize built in types. – Jason Jul 28 '22 at 15:01