# *Need help in the right direction*

• 12-20-2006
timtorba
*Need help in the right direction*
I just need help with what im doing, I get confused on how to pass arrays through functions therfore making my peice of code not working, also on how to use the arrays to get the product. any help would be greatly appreciated.

Assigenment;;;

Write a program that declares 3 single-dimensional arrays named Price,Quantity, and Amount. Each array should be declared in main() and should be capable of holding 10 double-precision numbers. The numbers to be stored in price are{10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32, 12.15,3.98}
and the numbers in quantity are{4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8}
Have the program pass these three arrays to a function called extend(), which calculates the elemets in the Amount array, as the product of the equivalent in the Price and Quantity arrays( for example, Amount[1]= Price[1]*Quantity[2].)

After extend() has put values into the Amount array, display the values in the array from within main(). Write extend() using pointers.
-------------------------------------------------------------------
this is what i have so far, it doesnt compile becuase its not correct i think.

Code:

```#include <iostream.h> #include <iomanip.h> void extend(double[],double[],double[]); main() {       const int Size=10;       double Price[Size]={10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};       double Quantity[Size]={4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};       double Amount[Size]; extend(Price,Quantity,Amount);       cout<<"\n"<<"Price"<<setw(10)<<"Quantity"<<setw(9)<<"Amount"<<endl;       cout<<"\n\r------------------------"<<endl;       cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)<<setprecision(4);       cout<<Price<<Quantity<<Amount<<endl;   return 0; } void extend(double *Price, double *Quantity, double *Amount)   {   const int Size=10;   int i;   for(i=0;i<Size;i++)   return;   }```
• 12-20-2006
swgh
Why have you got so much whitespace in your code?

>main()

Main returns an int. Change to

Code:

`int main ( void )`
When you pass an array, you pass the array name and the array size

Code:

`functionPass ( myArray, 25 )`
• 12-20-2006
timtorba
Quote:

Originally Posted by swgh
Why have you got so much whitespace in your code?

My instructor makes us becuase she is older than dinosuars and has trouble seeing, therfore we need to space things out, extremely.
• 12-20-2006
Prelude
Ironic that you have no horizontal whitespace, ne?
• 12-20-2006
Mario F.
The excuses these kids invent... tsk
• 12-20-2006
timtorba
another questio,,,,, do i use a for loop to get the product for the Amount Array
• 12-20-2006
swgh
Quote:

another questio,,,,, do i use a for loop to get the product for the Amount Array
Yes. It will print the array values. if that is what you meant.
• 12-20-2006
timtorba
ok I think ive decided to take this program one peice at a time. Right now all i am trying to do is list the Price and Quantity, i got it running but it doesnt run the way i expected it to.
Code:

```#include <iostream.h> #include <iomanip.h> int main() {                 const int Size=10;         int a,i;                 double *zingA,Price[10]={10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};                 double *zingI,Quantity[10]={4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};                 zingA=&Price[0];                         zingI=&Quantity[0];                 for(a=0;a<Size;a++)         for(i=0;i<Size;i++);           cout<<"\n"<<"Price"<<setw(10)<<"Quantity"<<setw(9)<<"Amount"<<endl;                 cout<<"\n\r------------------------"<<endl;                 cout<<Price[a]<<Quantity[i]<<endl; return 0;```
instead of getting the #'s listed i get this. after i run it

Price Quantity Amount

------------------------
2.12206e-313-4.95295e-136
• 12-20-2006
Salem
> for(i=0;i<Size;i++);
This trailing ; means do nothing for a number of iterations of the loop.

Always use braces around the statements you want to be in the loop. Whilst they are optional in some cases, it's not enough to worry about. It you always do it, it will save much confusion and puzzlement in the long run.
• 12-20-2006
anon
Because loops go into {} brackets. And the semicolon at the end of the for line means, this loop does nothing.
Code:

```for(a=0;a<Size;a++) {     for(i=0;i<Size;i++) {         cout<<"\n"<<"Price"<<setw(10)<<"Quantity"<<setw(9)<<"Amount"<<endl;         cout<<"\n\r------------------------"<<endl;         cout<<Price[a]<<Quantity[i]<<endl;     } }```
This is some really messed up formatting your teacher enjoys :)
• 12-20-2006
timtorba
alright i give up, ill paypal anyone who can make this thing work
• 12-21-2006
g4j31a5
Have you even try Salem and anon's advice?

Also here:
Code:

`                cout<<Price[a]<<Quantity[i]<<endl;`
The price and quantity will be printed without any space between them as if price and quantity were one entity.
• 12-21-2006
swgh
Quote:

alright i give up, ill paypal anyone who can make this thing work
Never give up if somthing is too hard, and you are trying. That statement has a special meaning in programming. It can be tough somtimes and make you want to throw your pc out the window. But even the best coders make mistakes. Nobody is 100% perfect every compile