# Thread: Product of two elemets of arrays returned to main function

1. ## Product of two elemets of arrays returned to main function

I am supposed to write three arrays product, quantity, and amount. Product and quantity are given and are supposed to go to function extend and multiply each element to find the equivalent element for amount, then return the answers back to main and display them to the user. I am having trouble with the extend function when I run the program I get 0 as the displayed answer.

Code:
```include <stdio.h>
#include <stdlib.h>
double extend (double [], double [], double []); /*function prototype*/
int main()
{
#define MAXELS 10
double price[MAXELS]= {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
double quantity[MAXELS]= {4, 8.5, 6, 8.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
double amount[MAXELS];

printf("The amounts of each element are %lf\n", &amount[MAXELS]);
system("pause");
return 0;
}
double extend (double price[MAXELS], double quantity[MAXELS], double amount[MAXELS])
{
int i;
amount[0]=price[0]*quantity[0];
for (i=0; i < MAXELS; i++);
return(amount[MAXELS]);
}```

2. Code:
``` amount[0]=price[0]*quantity[0];   /* here you're only calculating element 0 */
for (i=0; i < MAXELS; i++);       /* this is a do-nothing loop (a loop with an empty body) */
return(amount[MAXELS]);           /* this is an out-of-bounds array access */```

3. Okay so I'm guessing I need to change it to amount[maxels]=price[maxels]*quantity[maxels]
as for the other two mistakes can you explain to me in greater detail what I am doing incorrectly?

4. Originally Posted by LearnOnTheFly
Okay so I'm guessing I need to change it to amount[maxels]=price[maxels]*quantity[maxels]
You need to put it into the body of the loop and index it with i. You can't index an array of size MAXELS with MAXELS. Since array indices start at zero, MAXELS is one beyond the end.

Originally Posted by LearnOnTheFly
as for the other two mistakes can you explain to me in greater detail what I am doing incorrectly?
The semicolon at the end of the for statement line constitutes an empty statement and the entire body of the loop, making it a nop (no-operation), do-nothing loop. Get rid of that semicolon, toss in some braces and put the multiplication into that loop body.

Also, you don't need to return anything because you're modifying the array data in the caller directly, so get rid of the return statement. Make the function void.

5. Ok thank you, but when I remove semi-colon it says I need a final semi-colon before the last bracket in my compiler
Here is my function extend
Code:
```void extend (double price[MAXELS], double quantity[MAXELS], double amount[MAXELS])
{
int i;
for (i = 0, i < MAXELS; amount[i]=price[i]*quantity[i], i++)

}```

6. Also when I do add the semi-colon the program displays the answer 0

7. It should look something like this:
Code:
```for (i = 0, i < MAXELS; i++)
{
amount[i] = price[i] * quantity[i];
}```
This is what it means by "Get rid of that semicolon, toss in some braces and put the multiplication into that loop body."

8. Ok thanks for clearing that up. When I do that my compiler tells me I need a ; before the ) in my for statement. Sidenote, when I add the ; I get the result of zero displayed for my program.
Here is my update code:
Code:
```void extend (double [], double [], double []); /*function prototype*/
int main()
{
#define MAXELS 10
double price[MAXELS]= {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
double quantity[MAXELS]= {4, 8.5, 6, 8.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
double amount[MAXELS];

printf("The amounts of each element are %lf\n", &amount[MAXELS]);
system("pause");
return 0;
}
void extend (double price[MAXELS], double quantity[MAXELS], double amount[MAXELS])
{
int i;
for (i = 0, i < MAXELS; i++)
{
amount[i]=price[i]*quantity[i];
}

}```

9. Originally Posted by LearnOnTheFly
When I do that my compiler tells me I need a ; before the ) in my for statement.
Yes, i.e., there should be two semi-colons, not one.

Originally Posted by LearnOnTheFly
I get the result of zero displayed for my program.
You should print the amounts in a loop. At the moment, you are printing the address of an element that does not exist.

By the way, since macros do not obey the rules of scope, it is quite pointless to #define MAXELS 10 in main. Just do it at file scope.

10. A loop to print the amount would have to be in the function extend right? If so, I am supposed to send the amount back to the main function and then print the answers

11. Originally Posted by LearnOnTheFly
A loop to print the amount would have to be in the function extend right?
No, it should be in main, or yet another function.

Also, you should call the function. By the way, why it is named extend?

12. The assignment called for the function to be named extend. Do I need to make to make amount amountaddr and use a pointer to send it back to main?

13. Originally Posted by LearnOnTheFly
Do I need to make to make amount amountaddr and use a pointer to send it back to main?
You are already using a pointer named amount in the extend function.

14. I'm going to figure it out one of these days. I called the function as extend (price, quanity, amount). Do I need to write the equation for amount after that? If so, would I inlcude MAXELS in the []

15. Originally Posted by LearnOnTheFly
I called the function as extend (price, quanity, amount).
Sounds correct.

Originally Posted by LearnOnTheFly
Do I need to write the equation for amount after that?
You're supposed to have the equation in the loop. Refer to my example.

Originally Posted by LearnOnTheFly
If so, would I inlcude MAXELS in the []
What do you mean?