I have most of the program written, but I'm stuck on the 2 dimensional array. Here is the code I have so far:

#include <iostream>

using namespace std;

//function prototype
int calcTotal();

int main()
{
	//declare variable and array
	int total = 0;
	int sales[6][2] = {{12000, 10000}, {45000, 56000}, {32000, 42000}, 
					   {67000, 23000}, {24000, 12000}, {55000, 34000}};

	//calculate and display total sales
	total = calcTotal();
	cout << "Total sales: $" << total << endl;

	return 0;
}  //end of main function

//*****function definitions*****
int calcTotal()
{
    //declare variables
	int company = 0;
	double salesAmts = 0.0;

	//sales accumulator
		for (int row = 0; row < 6; row = row + 1)
		for (int col = 0; col < 2; col = col + 1)
			//accumulate sales
			
			company = company + salesAmts[row][col];
		//end for col
	//end for row
	return company;
}  //end of calcTotal function

Dani AI

Generated

Good call by — the function must be given the sales data (or otherwise be able to see it) and its parameter types must match what you index. The original compile/runtime failures came from trying to index something the function did not actually have. Fixing that is the immediate step; the rest is making the code safer and clearer.

Recommended improvements and why they matter:

  • Pass the data explicitly and use const-reference to avoid copying large containers. For fixed, compile-time sizes prefer std::array; for flexible sizes use std::vector.
  • Store money in integer cents and sum with a 64-bit integer (std::int64_t) to avoid floating-point rounding and overflow; see integer types documentation at cppreference.
  • Use range-based loops or algorithms like std::accumulate to make the summation concise and less error-prone. Range-for is documented here: range-based for.

Practical example (modern C++ style — different from the posted snippets):

#include <vector>
#include <numeric>
#include <cstdint>

std::int64_t totalSales(const std::vector<std::vector<std::int64_t>>& sales) {
    std::int64_t sum = 0;
    for (const auto& row : sales)
        sum += std::accumulate(row.begin(), row.end(), std::int64_t(0));
    return sum;
}

Troubleshooting tips: if you change the function signature, update the prototype before main and update all calls. If you keep C-style arrays, either pass sizes alongside the pointer or use a template to capture dimensions at compile time. For printing currency, convert cents to dollars only for display and format with iostream manipulators (see iomanip). These small changes make the code safer and easier to maintain for future changes.

Recommended Answers

All 2 Replies

Hi,

I think you mean

int calcTotal(int sales[6][2])
{
    //declare variables
	int company = 0;
	//sales accumulator
		for (int row = 0; row < 6; row = row + 1)
		for (int col = 0; col < 2; col = col + 1)
			//accumulate sales
			company = company + sales[row][col];
		//end for col
	//end for row
	return company;
}  //end of calcTotal function

that should work if you array is always going to be fixed to [6][2] but will not if the size varies.

Hope this helps.

Thank you so much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.