Having an Issue with 2D arrays. I need to output the array along with input by user being totaled
up vote
-2
down vote
favorite
#include <iostream>
#include <iomanip>
using namespace std;
double getInput(double sales[3][4], int div, int qtr, const int NUM_DIVS, const int NUM_QTRS);
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_DIVS, const int NUM_QTRS);
void displayResults(double totalSales, double** sales, int div, int qtr);
int main()
const int NUM_DIVS = 3; // Number of divisions
const int NUM_QTRS = 4; // Number of quarters
double sales[NUM_DIVS][NUM_QTRS]; // Array with 3 rows and 4 columns.
double totalSales = 0; // To hold the total sales
int div = 0, qtr = 0; // Loop counters
Im not sure if maybe my function calls are messed up?
getInput(sales, div, qtr, NUM_DIVS, NUM_QTRS);
calcTotal(sales, div, qtr, totalSales, NUM_QTRS, NUM_DIVS);
displayResults(sales, div, qtr, totalSales);
system("pause");
return 0;
double getInput(double sales[3][4], int div, int qtr, const int NUM_QTRS, const int NUM_DIVS)
cout << "This program will calculate the total sales ofn";
cout << "all the company's divisions.n";
cout << "Enter the following sales information:nn";
double** sales = new double*[NUM_DIVS];
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
cout << "Division " << (div + 1);
cout << ", Quarter" << (qtr + 1) << ": $";
cin >> sales[div][qtr];
cout << endl; // Print blank line.
return sales[div][qtr];
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_QTRS, const int NUM_DIVS)
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
totalSales += sales[div][qtr];
This is the portion where i need to calculate the total and assign it to totalSales but when i previously ran the code it would be ouputed as 0
$0.00
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
}
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
In this function im trying to output the total calculation of each element inputed by user and the output the entire 2D array also. I cant even get the total to calculate correctly.
c++ visual-c++ visual-studio-2017
|
show 1 more comment
up vote
-2
down vote
favorite
#include <iostream>
#include <iomanip>
using namespace std;
double getInput(double sales[3][4], int div, int qtr, const int NUM_DIVS, const int NUM_QTRS);
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_DIVS, const int NUM_QTRS);
void displayResults(double totalSales, double** sales, int div, int qtr);
int main()
const int NUM_DIVS = 3; // Number of divisions
const int NUM_QTRS = 4; // Number of quarters
double sales[NUM_DIVS][NUM_QTRS]; // Array with 3 rows and 4 columns.
double totalSales = 0; // To hold the total sales
int div = 0, qtr = 0; // Loop counters
Im not sure if maybe my function calls are messed up?
getInput(sales, div, qtr, NUM_DIVS, NUM_QTRS);
calcTotal(sales, div, qtr, totalSales, NUM_QTRS, NUM_DIVS);
displayResults(sales, div, qtr, totalSales);
system("pause");
return 0;
double getInput(double sales[3][4], int div, int qtr, const int NUM_QTRS, const int NUM_DIVS)
cout << "This program will calculate the total sales ofn";
cout << "all the company's divisions.n";
cout << "Enter the following sales information:nn";
double** sales = new double*[NUM_DIVS];
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
cout << "Division " << (div + 1);
cout << ", Quarter" << (qtr + 1) << ": $";
cin >> sales[div][qtr];
cout << endl; // Print blank line.
return sales[div][qtr];
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_QTRS, const int NUM_DIVS)
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
totalSales += sales[div][qtr];
This is the portion where i need to calculate the total and assign it to totalSales but when i previously ran the code it would be ouputed as 0
$0.00
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
}
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
In this function im trying to output the total calculation of each element inputed by user and the output the entire 2D array also. I cant even get the total to calculate correctly.
c++ visual-c++ visual-studio-2017
I am rather new to coding, any guidance would be greatly appreciated! thank you in advance !
– CodeNaivety
Nov 10 at 19:40
what is the question?
– OznOg
Nov 10 at 20:19
I dont know if I am recording the cin to the correct place or if im losing data when passing my array into the function, but when i cout the totalSales which should equal all of the elements in my array, i get 0
– CodeNaivety
Nov 10 at 20:33
When a function makes changes to its parameter, those changes are not generally visible to the caller. Consider:void f(int x) x = 42; y = 0; f(y); // y is still 0, not 42
. This is what happens withtotalSales
incalcTotal
. Read about passing by reference in your favorite C++ textbook.
– Igor Tandetnik
Nov 11 at 1:40
You used sales[3][4] then **sales ... Two different types. You should better use vector<vector<double>> or define your own matrix type or use one of a library.
– Damien
Nov 11 at 8:54
|
show 1 more comment
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
#include <iostream>
#include <iomanip>
using namespace std;
double getInput(double sales[3][4], int div, int qtr, const int NUM_DIVS, const int NUM_QTRS);
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_DIVS, const int NUM_QTRS);
void displayResults(double totalSales, double** sales, int div, int qtr);
int main()
const int NUM_DIVS = 3; // Number of divisions
const int NUM_QTRS = 4; // Number of quarters
double sales[NUM_DIVS][NUM_QTRS]; // Array with 3 rows and 4 columns.
double totalSales = 0; // To hold the total sales
int div = 0, qtr = 0; // Loop counters
Im not sure if maybe my function calls are messed up?
getInput(sales, div, qtr, NUM_DIVS, NUM_QTRS);
calcTotal(sales, div, qtr, totalSales, NUM_QTRS, NUM_DIVS);
displayResults(sales, div, qtr, totalSales);
system("pause");
return 0;
double getInput(double sales[3][4], int div, int qtr, const int NUM_QTRS, const int NUM_DIVS)
cout << "This program will calculate the total sales ofn";
cout << "all the company's divisions.n";
cout << "Enter the following sales information:nn";
double** sales = new double*[NUM_DIVS];
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
cout << "Division " << (div + 1);
cout << ", Quarter" << (qtr + 1) << ": $";
cin >> sales[div][qtr];
cout << endl; // Print blank line.
return sales[div][qtr];
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_QTRS, const int NUM_DIVS)
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
totalSales += sales[div][qtr];
This is the portion where i need to calculate the total and assign it to totalSales but when i previously ran the code it would be ouputed as 0
$0.00
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
}
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
In this function im trying to output the total calculation of each element inputed by user and the output the entire 2D array also. I cant even get the total to calculate correctly.
c++ visual-c++ visual-studio-2017
#include <iostream>
#include <iomanip>
using namespace std;
double getInput(double sales[3][4], int div, int qtr, const int NUM_DIVS, const int NUM_QTRS);
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_DIVS, const int NUM_QTRS);
void displayResults(double totalSales, double** sales, int div, int qtr);
int main()
const int NUM_DIVS = 3; // Number of divisions
const int NUM_QTRS = 4; // Number of quarters
double sales[NUM_DIVS][NUM_QTRS]; // Array with 3 rows and 4 columns.
double totalSales = 0; // To hold the total sales
int div = 0, qtr = 0; // Loop counters
Im not sure if maybe my function calls are messed up?
getInput(sales, div, qtr, NUM_DIVS, NUM_QTRS);
calcTotal(sales, div, qtr, totalSales, NUM_QTRS, NUM_DIVS);
displayResults(sales, div, qtr, totalSales);
system("pause");
return 0;
double getInput(double sales[3][4], int div, int qtr, const int NUM_QTRS, const int NUM_DIVS)
cout << "This program will calculate the total sales ofn";
cout << "all the company's divisions.n";
cout << "Enter the following sales information:nn";
double** sales = new double*[NUM_DIVS];
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
cout << "Division " << (div + 1);
cout << ", Quarter" << (qtr + 1) << ": $";
cin >> sales[div][qtr];
cout << endl; // Print blank line.
return sales[div][qtr];
void calcTotal(double sales[3][4], int div, int qtr, double totalSales, const int NUM_QTRS, const int NUM_DIVS)
for (div = 0; div < NUM_DIVS; div++)
for (qtr = 0; qtr < NUM_QTRS; qtr++)
totalSales += sales[div][qtr];
This is the portion where i need to calculate the total and assign it to totalSales but when i previously ran the code it would be ouputed as 0
$0.00
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
}
void displayResults(double totalSales, double** sales, int div, int qtr)
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
cout << sales[div][qtr] << endl;
In this function im trying to output the total calculation of each element inputed by user and the output the entire 2D array also. I cant even get the total to calculate correctly.
c++ visual-c++ visual-studio-2017
c++ visual-c++ visual-studio-2017
asked Nov 10 at 19:25
CodeNaivety
11
11
I am rather new to coding, any guidance would be greatly appreciated! thank you in advance !
– CodeNaivety
Nov 10 at 19:40
what is the question?
– OznOg
Nov 10 at 20:19
I dont know if I am recording the cin to the correct place or if im losing data when passing my array into the function, but when i cout the totalSales which should equal all of the elements in my array, i get 0
– CodeNaivety
Nov 10 at 20:33
When a function makes changes to its parameter, those changes are not generally visible to the caller. Consider:void f(int x) x = 42; y = 0; f(y); // y is still 0, not 42
. This is what happens withtotalSales
incalcTotal
. Read about passing by reference in your favorite C++ textbook.
– Igor Tandetnik
Nov 11 at 1:40
You used sales[3][4] then **sales ... Two different types. You should better use vector<vector<double>> or define your own matrix type or use one of a library.
– Damien
Nov 11 at 8:54
|
show 1 more comment
I am rather new to coding, any guidance would be greatly appreciated! thank you in advance !
– CodeNaivety
Nov 10 at 19:40
what is the question?
– OznOg
Nov 10 at 20:19
I dont know if I am recording the cin to the correct place or if im losing data when passing my array into the function, but when i cout the totalSales which should equal all of the elements in my array, i get 0
– CodeNaivety
Nov 10 at 20:33
When a function makes changes to its parameter, those changes are not generally visible to the caller. Consider:void f(int x) x = 42; y = 0; f(y); // y is still 0, not 42
. This is what happens withtotalSales
incalcTotal
. Read about passing by reference in your favorite C++ textbook.
– Igor Tandetnik
Nov 11 at 1:40
You used sales[3][4] then **sales ... Two different types. You should better use vector<vector<double>> or define your own matrix type or use one of a library.
– Damien
Nov 11 at 8:54
I am rather new to coding, any guidance would be greatly appreciated! thank you in advance !
– CodeNaivety
Nov 10 at 19:40
I am rather new to coding, any guidance would be greatly appreciated! thank you in advance !
– CodeNaivety
Nov 10 at 19:40
what is the question?
– OznOg
Nov 10 at 20:19
what is the question?
– OznOg
Nov 10 at 20:19
I dont know if I am recording the cin to the correct place or if im losing data when passing my array into the function, but when i cout the totalSales which should equal all of the elements in my array, i get 0
– CodeNaivety
Nov 10 at 20:33
I dont know if I am recording the cin to the correct place or if im losing data when passing my array into the function, but when i cout the totalSales which should equal all of the elements in my array, i get 0
– CodeNaivety
Nov 10 at 20:33
When a function makes changes to its parameter, those changes are not generally visible to the caller. Consider:
void f(int x) x = 42; y = 0; f(y); // y is still 0, not 42
. This is what happens with totalSales
in calcTotal
. Read about passing by reference in your favorite C++ textbook.– Igor Tandetnik
Nov 11 at 1:40
When a function makes changes to its parameter, those changes are not generally visible to the caller. Consider:
void f(int x) x = 42; y = 0; f(y); // y is still 0, not 42
. This is what happens with totalSales
in calcTotal
. Read about passing by reference in your favorite C++ textbook.– Igor Tandetnik
Nov 11 at 1:40
You used sales[3][4] then **sales ... Two different types. You should better use vector<vector<double>> or define your own matrix type or use one of a library.
– Damien
Nov 11 at 8:54
You used sales[3][4] then **sales ... Two different types. You should better use vector<vector<double>> or define your own matrix type or use one of a library.
– Damien
Nov 11 at 8:54
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242620%2fhaving-an-issue-with-2d-arrays-i-need-to-output-the-array-along-with-input-by-u%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am rather new to coding, any guidance would be greatly appreciated! thank you in advance !
– CodeNaivety
Nov 10 at 19:40
what is the question?
– OznOg
Nov 10 at 20:19
I dont know if I am recording the cin to the correct place or if im losing data when passing my array into the function, but when i cout the totalSales which should equal all of the elements in my array, i get 0
– CodeNaivety
Nov 10 at 20:33
When a function makes changes to its parameter, those changes are not generally visible to the caller. Consider:
void f(int x) x = 42; y = 0; f(y); // y is still 0, not 42
. This is what happens withtotalSales
incalcTotal
. Read about passing by reference in your favorite C++ textbook.– Igor Tandetnik
Nov 11 at 1:40
You used sales[3][4] then **sales ... Two different types. You should better use vector<vector<double>> or define your own matrix type or use one of a library.
– Damien
Nov 11 at 8:54