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.










share|improve this question





















  • 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 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














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.










share|improve this question





















  • 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 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












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.










share|improve this question













#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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















  • 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 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















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党