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

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    98

    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. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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 */
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    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. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by LearnOnTheFly View Post
    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.

    Quote 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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    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. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Also when I do add the semi-colon the program displays the answer 0

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    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. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    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. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    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. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    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. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LearnOnTheFly
    I called the function as extend (price, quanity, amount).
    Sounds correct.

    Quote 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.

    Quote Originally Posted by LearnOnTheFly
    If so, would I inlcude MAXELS in the []
    What do you mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Define new function as product of two functions
    By freechus9 in forum C Programming
    Replies: 5
    Last Post: 11-05-2011, 09:19 PM
  2. Returned value with operator overloaded function???
    By silk.odyssey in forum C++ Programming
    Replies: 8
    Last Post: 05-12-2004, 11:46 AM
  3. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  4. Returning arrays from function to main
    By musayume in forum C Programming
    Replies: 3
    Last Post: 05-12-2003, 04:33 AM
  5. msvc++ 6 wont allow declaring arrays in main() function
    By Bobish in forum C++ Programming
    Replies: 7
    Last Post: 03-02-2002, 01:11 PM