to start off thanks for the help in advance, im trying to make a program that calls a function that i input integers into, then calling another function to calculate the total of the numbers entered.

here is my source code:

``````#include <cstdlib>
#include <iostream>

using namespace std;
const int LOT = 5;
const int MON = 12;

int sales_input(int [], int);
void sales_total(int [], int);

int main()
{

int month_sales[MON];

sales_input(month_sales, MON);
month_sales = sales_input(month_sales, 12);
sales_total(month_sales, MON);

system("PAUSE");
return EXIT_SUCCESS;
}

int sales_input(int month_sales[], int MON)
{
int num1, total;
for(num1 = 0; num1 < 5; ++num1)
{
cout << "enter total sales for lot " << (num1 + 1);
for(int num = 0; num < MON; ++num)
{
cout << "\ntotal sales ";
cin >> month_sales[num];
}

}

}
void sales_total(int month_sales[], int)
{
int total = 0;
for(int num2 = 1; num2 < 60; num2++)
total += month_sales[num2];
cout << "total= " << total;
}``````

>>to start off thanks for the help in advance,

my question is, why am i not able to pass the array month_sales to sales total, it wont pass the numbers. when i finish inputting my numbers it ends up totaling 15615616514 which is not what i entered, im trying to store the 60 numbers i input into the array then in my function sales_total i want to total all 60 up and output the result, can anyone help me so that the numbers i have entered in the function sales_input will be computed and output in the function sales_total?

your function for geting the sales for each month is messing you up a bit. you are not returning anything so im not sure why your compiler isnt flagging an error. anyway your input function should be returning the whole array so that when you write `month_sales = sales_input(month_sales, 12);` in your main function month_sales should recive the entier array. then you should be able to pass it to your totaling function.

your function for geting the sales for each month is messing you up a bit. you are not returning anything so im not sure why your compiler isnt flagging an error. anyway your input function should be returning the whole array so that when you write `month_sales = sales_input(month_sales, 12);` in your main function month_sales should recive the entier array. then you should be able to pass it to your totaling function.

how do i make it return the entire array is what i have been asking, when i try `return(month_sales);` it doesnt work. do you or does anyone here know how i can return the whole array?

line 30: why are you attempting to stuff 60 numbers into an array that only contains 12 elements? That loop is asking for the 12 monthly sales 5 times !

Line 45 has the same problem, but a lot more obvious. The array only has 12 elements but it is attempting to sum up 60 numbers.

If you want 60 monthly_sales then declare the array like this (line 15): `int month_sales[MON * LOT];` Those two functions don't need to return anything, just make both of them void functions.

Line 42, defination of function is not proper. The second formal parameter is missing.

Returning of array doesn't makes sense. As arrays passed are mutable in nature. Any changes applied to arrays inside a function is reflected back to the caller.
So sales_total doesn't return anything.
sales_input should also doesn't return anything, so make it void returning.

just to be clear if you pass an array into a function by value the array in the calling function will be changed after the called function exits?

>>if you pass an array into a function by value the array
That's not possible -- arrays can only be passed by reference. Passing by value implies creating another copy of the array and passing the copy. The language does not support passing arrays by value.

>>That's not possible -- arrays can only be passed by reference. Passing by value
>>implies creating another copy of the array and passing the copy. The language
>>does not support passing arrays by value.

Just being too strict in saying, the passing of arrays is converted into passing to the equivalent pointer by the compiler.

``````void f1(char a[])
{}``````

is automagically converted to

``````void f1(char* a)
{}``````

And the above pointer is passed by value. In effect it appears that the array has been passed 'by reference' but in reality, arrays are actually never passed to a functions.

And the above pointer is passed by value. In effect it appears that the array has been passed 'by reference' but in reality, arrays are actually never passed to a functions.

True -- the pointer is passed by value, but the array (the memory location of the first element of the array) is by reference since it is not copied. When you declare an array in main() and pass it to foo(), any changes made to the values of the array in foo() are visible in main(). That is what I mean by pass by reference.

>>, but the array (the memory location of the first element of the array) is by
>> reference since it is not copied.
No. The memory location of the first element is passed by value. This is what the name of the array points to. Look at the following snippet

``````void foo(int arr[])
{
arr=0;//nulling the pointer passed to the first element
}
int main()
{

int p[]={3,5,4};
std::cout<<"The arr points to --->  "<<p<<std::endl;
foo(p);//actually the array is not passed. But the pointer
// to the first element is passed by value
std::cout<<"Now, the arr points to --->  "<<p;//the output is same as above
//That proves that the memory address of the first element is passed by value. If it was not, zeroing it in foo would have been reflected in main
}``````

But I'm not talking about the pointer! You are just trying to be difficult. Change the function to this:

``````void foo(int arr[])
{
arr[0]=0;//setting the first element of the array to 0
}``````

Yeah, Yeah thats true.
because arr[0] is read as *(arr+0) which will change the value of the memory pointed by arr+0 directly.

Good.

commented: you have cleared up arrays for me thanks +1

thank you for that siddhant3s and Ancient Dragon i did not know that.

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.