Thread: *Need help in the right direction*

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    5

    *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;
    
    
       }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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 )
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ironic that you have no horizontal whitespace, ne?
    My best code is written with the delete key.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The excuses these kids invent... tsk
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    another questio,,,,, do i use a for loop to get the product for the Amount Array

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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.
    Double Helix STL

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    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

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    alright i give up, ill paypal anyone who can make this thing work

  12. #12
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    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.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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
    Double Helix STL

Popular pages Recent additions subscribe to a feed