CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 40

Thread: Help wit C++

  1. #1
    Join Date
    Aug 2010
    Posts
    51

    Help wit C++

    here are my practice problems:


    the answers that i have are at the bottom.(if you guys can check it and provide the right answer if i got it wrong, that would be great)



    5. Write a C++ statement that displays a random integer from 50 through 100 on the computer screen.

    6. Write a C++ statement that assigns the square root of a number to a double variable named sqRoot. The number is stored in a double variable named num.

    7. Write the C++ code for a function that prompts the user to enter a character and then stores the character in a char variable named response. The functions should return the contents of the response variable. Name the function getCharacter. (The function will not have any actual arguments passed to it.) Also write an appropriate function prototype for the getCharacter function. In addition, write a statement that invokes the getCharacter function and assigns its return value to a char value named custCode.

    8. Write the C++ code for a function that receives four double numbers. The function should calculate the average of the four numbers and then return the result. Name the function calcAverage. Name the formal parameters num1, num2, num3, num4. Also write an appropriate function prototype for the calcAverage function. In addition, write a statement that invokes the calcAverage function and assigns its return value to a double variable named quotient. Use the following numbers as the actual arguments: 45.67, 8.35, 125.78, 99.56.

    9.Write the C++ statement that adds the cube of the number stored in the num1 variable to the square root of the number stored in the num2 variable. The statement should assign the result to the answer variable. All of the variables have the double data type.

    10. Write a C++ statement that assigns to the answer variable the square root of the following expression: x^2 * y^3. The x,y and answer variable have the double data type.

    5. 1 + 50 % (100 – 50 + 1)
    6. sqRoot (num)
    7. char getcharacter (char)
    cout<<”Enter a character”:
    cin>> response;
    getcharacter (response)
    {
    return custcode;
    } // end of getcharacter function
    8.
    9. answer = pow(num1) + sqrt(num2);
    10. answer = pow(x,2) * pow(y,3);

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    #10 is correct. The rest are all wrong to some degree, although #9 is close.

    #5 asks for a random number. Nothing in your statement is random.

    #6 does not compute a square root.

    #7 is vaguely headed in the right direction, but has a number of problems, the most obvious of which is----where did this "custcode" variable come from?

  3. #3
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by Lindley View Post
    #10 is correct. The rest are all wrong to some degree, although #9 is close.

    #5 asks for a random number. Nothing in your statement is random.

    #6 does not compute a square root.

    #7 is vaguely headed in the right direction, but has a number of problems, the most obvious of which is----where did this "custcode" variable come from?
    read the problem statement for number 7 and you will see the custCode variable.

    Thanks so far though

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    Yes, I see the name mentioned in the problem. But you still do not appear to understand what the problem is asking you to do:

    7. Write the C++ code for a function
    So, you're going to need a function. So far so good.

    that prompts the user to enter a character and then stores the character in a char variable named response.
    Still good so far, although you aren't actually declaring "response" anywhere before trying to use it.

    The functions should return the contents of the response variable. Name the function getCharacter. (The function will not have any actual arguments passed to it.)
    Here we run into trouble. You've named the function getcharacter rather than getCharacter (capitalization is important). You've specified it takes a char argument when the question says it doesn't take any arguments. And you aren't returning the result variable.

    Also write an appropriate function prototype for the getCharacter function.
    Tough to say whether any of the things you wrote were intended to be a prototype, but you need one and one isn't there.

    In addition, write a statement that invokes the getCharacter function and assigns its return value to a char value named custCode.
    You are not doing this.
    Last edited by Lindley; October 26th, 2010 at 02:12 PM.

  5. #5
    Join Date
    Aug 2010
    Posts
    47

    Re: Help wit C++

    #5: google for "random numbers in c", I glanced at the top 3 results and 2 of them have enough information to answer this.
    #6: reread the question... you correctly figured out the square root of something in #9, just do the same sort of thing and assign it to the variable they want
    #7: yeah... start this one over... it doesn't bear anything but passing resemblance to the question
    #8: Here, I'll get you started. "a function that receives four double numbers" might look like:
    Code:
    void question_eight(double numberA, double numberB, double numberC, double numberD)
    {
     // This is a void function so doesn't need a return statement.
    }
    Now, the next thing it says is that it's going to have to return the average of the supplied numbers. So you'd pick a type of value to return (let's say a double), and replace the 'void' type with that type. There, you now have an empty function set up to take four doubles (but of course it needs to return something... and they have naming requirements that I didn't cover... and of course there's some math... and so forth).
    #9: You used the 'pow' function correctly in #10, but not here.
    #10: I don't see any square rooting being done here.

  6. #6
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    here are my updated answers(i will be working on number 7 and 8)

    5. 50 + rand() % (100 – 50 + 1)
    6. sqRoot = sqrt (num);
    9. answer = pow(num1) + sqrt(num2);
    10. answer = pow(x,2) * pow(y,3);

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    here are my updated answers(i will be working on number 7 and 8)

    5. 50 + rand() &#37; (100 – 50 + 1)
    Why 100 - 50 + 1? If you mean 51, write 51.
    rand requires srand be called first.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    here are my updated answers(i will be working on number 7 and 8)

    6. sqRoot = sqrt (num);
    You need to define sqRoot.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    here are my updated answers(i will be working on number 7 and 8)


    9. answer = pow(num1) + sqrt(num2);
    I believe it's already been pointed out that you're not calling pow correctly, and again, you need to define your variables.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    Quote Originally Posted by GCDEF View Post
    rand requires srand be called first.
    In the context of this question I wouldn't worry about it. After all, we don't want them thinking they need to call srand() before *every* use of rand(), would we?

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by Lindley View Post
    In the context of this question I wouldn't worry about it. After all, we don't want them thinking they need to call srand() before *every* use of rand(), would we?
    No we wouldn't, and the prof did ask for a single statement. Just making sure the OP knows how it works, and the prof may be looking for srand. I don't know.

  12. #12
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by GCDEF View Post
    I believe it's already been pointed out that you're not calling pow correctly, and again, you need to define your variables.

    int num1 = 0;
    int num2 = 0;
    int answer = 0;
    cout<<”Enter num 1 “;
    cin>>num1;
    cout<<”Enter num 2 “;
    cin>>num2;
    answer = pow(num1) + sqrt(num2);

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    int num1 = 0;
    int num2 = 0;
    int answer = 0;
    cout<<”Enter num 1 “;
    cin>>num1;
    cout<<”Enter num 2 “;
    cin>>num2;
    answer = pow(num1) + sqrt(num2);
    I don't know how many ways to say you're not calling pow correctly, and if you read the problem statement, you'll see the prof calls for a specific data type, and it wasn't int.

  14. #14
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by GCDEF View Post
    I don't know how many ways to say you're not calling pow correctly, and if you read the problem statement, you'll see the prof calls for a specific data type, and it wasn't int.
    the syntax for the pow function is:

    pow(x,y)




    9. double num1 = 0;
    double num2 = 0;
    double answer = 0;
    cout<<”Enter num 1 “;
    cin>>num1;
    cout<<”Enter num 2 “;
    cin>>num2;
    answer = pow(num1,3) + sqrt(num2);

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    My reading of the problem is that you only need the last line of that (but move the declaration of answer onto that line for completeness). However, the professor probably will accept that entire thing as the answer.

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured