CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Please Help Calling function

  1. #1
    Join Date
    Apr 2007
    Posts
    3

    Exclamation Please Help Calling function

    .
    The whole assignment is so confusing it is creazy.

    a. [4 points] Write a definition of function one so that it returns the sum of x and y if x is greater than y; otherwise, it should return x minus 2 times y.

    b. [5 points] Write the definition of function two as follows:

    1. Read a number and store it in z.
    2. Update the value of z by adding the value of a to its previous value.
    3. Assign the variable first the value returned by function one with parameters 6 and 8.
    4. Update the value of first by adding the value of x to its previous value.
    5. If the value of z is more than twice the value of x to its previous value, return z; otherwise, return 2 times first minus z.

    c. Write a C++ program that test parts a and b. (Declare additional variables in the function main, if necessary)
    Call function one and pass two parameter values: 4 and 5. Display the returned value from function one.
    Input an integer number and double number assigned them to variables um and dec respectively.
    Call function two and pass these two values. Display the returned value from function two.


    MY CODE IS HERE

    #include<iostream>
    using namespace std;
    int one(int, int);
    double two(int x, double a);
    int main()
    {int num;
    double dec;

    return one(num,dec);
    return two(num,dec);

    //Write a C++ program that test parts a and b.
    //(Declare additional variables in the function main, if necessary)
    // Call function one and pass two parameter values: 4 and 5.
    // Display the returned value from function one.
    // Input an integer number and double number assigned them to variables num
    // and dec respectively.
    // Call function two and pass these two values. Display the returned
    // value from function two.

    return 0;
    }

    int one(int x, int y)
    {
    if (x>y)
    return x+y;
    else
    return x-2*y;


    }
    double two(int x, double a)
    {
    int first;
    double z;
    cin>>z;
    cout<<endl;
    z= a+z;
    first = one(6,8);
    first =x+first;


    if (z>2*first)
    return z;
    else
    return 2*first -z;


    }

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Please Help Calling function

    a)
    Code:
    int FunctionOne(int x, int y)
    {
        return (x > y) ? (x + y) : (x - (2*y));
    }
    b) Item 5 makes no sense to me. Whose previous value? Did you mean z greater than twice value of x?

    Code:
    double FunctionTwo(int x, double a)
    {
        double z;
    
        cin >> z;
        z += a;
    
        int first = FunctionOne(6,8);
        first += x;
    
        return (z > 2*x) ? z : (2*first) - z;
    }
    c)
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "Calling function one with 4 and 5 returns " << FunctionOne(4,5) << endl;
    
        int num;
        double dec;
    
        cin >> num;
        cin >> dec;
    
        cout  << "Calling function two with " << num << " and " << dec << " returns " << FunctionTwo(num,dec) << endl;
    
        return 0;
    }
    Fairly straightforward really if you can make sense of the appalling and ambiguous wording of the assignment.

    Regards
    Alan

  3. #3
    Join Date
    Apr 2007
    Posts
    3

    Re: Please Help Calling function

    Thanks a lot for helping me i appriciate it a lot. you are great.
    the assinment overall doesnt make sence to me. really weird problem.

    I will review what you wrote to see if i can understand it.
    I appriciate your help a lot.
    thank you very much .

  4. #4
    Join Date
    Apr 2007
    Posts
    3

    Exclamation Re: Please Help Calling function

    I got this code anyone can fix it if you can find the problem. This is killing me because the assignment i cant understand at all. I have a feeling i missed something. at begining of the program it runs -6. i am so lost with this problem.

    step five of part b states:

    if the value of z is more than twice the value of forst retrn z; otherwise , return 2 times first minus z.

    #include<iostream>
    using namespace std;
    int one(int, int);
    double two(int x, double a);
    int main()
    {
    cout << "Calling function one with 4 and 5 returns " << one(4,5) << endl;

    int num;
    double dec;

    cin >> num;
    cin >> dec;

    cout << "Calling function two with " << num << " and " << dec << " returns " << two(num,dec) << endl;

    return 0;
    }

    int one(int x, int y)
    {
    if (x>y)
    return x+y;
    else
    return x-2*y;


    }
    double two(int x, double a)
    {
    int first;
    double z;
    cin>>z;
    cout<<endl;
    z= a+z;
    first = one(6,8);
    first =x+first;


    if (z>2*first)
    return z;
    else
    return 2*first -z;


    }

  5. #5
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Please Help Calling function

    if the value of z is more than twice the value of first return z; otherwise , return 2 times first minus z.
    This is different to what you originally posted and is why I couldn't make any sense of the statement you did provide. It is still ambiguous

    2 times first minus z

    is that

    (2 * first) - z

    or

    2 * (first -z)


    Given the nature of the check to see if z > 2 * first, I suspect it is the former, since this prevents the result from being negative.

    This makes function two read as


    Code:
    double FunctionTwo(int x, double a)
    {
        double z;
    
        cin >> z;
        z += a;
    
        int first = FunctionOne(6,8);
        first += x;
    
        return (z > (2*first)) ? z : (2*first) - z;
    }
    I also note you haven't modified your one and two functions as I posted earlier for you. If you want to pass your assignment with a decent grade, then use the stuff I posted, which is better coding.

    Regards
    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)