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