So my professor for a beginners c++ class docked me 2 points for a quiz to write a program based on a given 2D array.
He gave us that our array had to be initialized with the following numbers:
{ {75000, 30200, 67800, 45000, 6000, 67500}, {4000, 75000, 64000, 32600, 47800, 39000} }
and said our output needed to look like this:
Total Domestic Sales: $291500
Total International Sales: $262400
Total Company Sales: $553900
January Sales: $79000
February Sales: $105200
March Sales: $131800
April Sales: $77600
May Sales: $53800
June Sales: $106500
He told us specifically we could NOT use nested for loops but we were
allowed to use multiple loops.
He graded me and took off two points for the below saying that it was "bad coding":
for (j = 0; j < ROW; j++)
{
janSales += sales[j][0];
febSales += sales[j][1];
marSales += sales[j][2];
aprSales += sales[j][3];
maySales += sales[j][4];
juneSales += sales[j][5];
}
could anyone explain to me why this might be? because he won't give me any
clearer specifics of why it is bad code. My program ran exactly as he asked,
he was very vague about how we were supposed to go about coding the program, so this was my solution that didn't have me adding up each individual index location.
Thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
using namespace std;
const int COL = 6;
const int ROW = 2;
int sales[ROW][COL] = { {75000, 30200, 67800, 45000, 6000, 67500}, {4000, 75000, 64000, 32600, 47800, 39000} };
void displaySales(int array[][COL], int ROW, int COL);
int main()
{
displaySales(sales, ROW, COL);
return 0;
}
void displaySales(int array[][COL], int ROW, int COL)
{
int i = 0;
int j = 0;
int domesticSum = 0;
int internationalSum = 0;
int janSales = 0;
int febSales = 0;
int marSales = 0;
int aprSales = 0;
int maySales = 0;
int juneSales = 0;
for (i = 0; i < COL; i++)
{
domesticSum += sales[0][i];
internationalSum += sales[1][i];
}
for (j = 0; j < ROW; j++)
{
janSales += sales[j][0];
febSales += sales[j][1];
marSales += sales[j][2];
aprSales += sales[j][3];
maySales += sales[j][4];
juneSales += sales[j][5];
}
cout << "Total Domestic Sales: " << "\t\t" << "$" << domesticSum << endl;
cout << "Total International Sales: " << "\t" << "$" << internationalSum << endl;
cout << "Total Company Sales: " << "\t\t" << "$" << domesticSum + internationalSum << endl;
cout << "January Sales: " << "\t\t\t" << "$" << janSales << endl;
cout << "February Sales: " << "\t\t" << "$" << febSales << endl;
cout << "March Sales: " << "\t\t\t" << "$" << marSales << endl;
cout << "April Sales: " << "\t\t\t" << "$" << aprSales << endl;
cout << "May Sales: " << "\t\t\t" << "$" << maySales << endl;
cout << "June Sales: " << "\t\t\t" << "$" << juneSales << endl;
}
|