C++ Helping girlfriend with simple homework

by tbk125
7 replies
Hey everyone I don't know C++ but if you could help me out with any of these simple questions I would greatly appreciate it:

1)Write a C++ program that contains a number constant, and asks the user to guess it. The program loops until the user guesses the number.

2)Write a C++ program that asks the user for 2 integers and returns their sum.

3)Write a C++ program that asks the user for 2 integers and returns their average.

4) Write a C++ program that asks the user for 2 integers and returns the largest of the 2.
#girlfriend #helping #homework #simple
  • Profile picture of the author seasoned
    So you want others to do your girlfriend's homework?

    Steve
    {{ DiscussionBoard.errors[8491359].message }}
  • Profile picture of the author andrewjorgenson
    If you're girlfriend is resorting to cheating to pass a class for programming, when she gets out into the field she's screwed.

    But here, I'll help her with some psuedocode. But really, if she needs to resort to cheating and not reading her text books then she probably shouldn't be taking a computer science class.

    This code below will not directly work if she just copy pastes it, she will need to get it working on her own. It's not that I'm not giving her working source code because I'm some jerk, it's because I actually care about her. If you spoonfeed her she won't learn anything.

    These programs are very basic and require little logic.

    Code:
    #include <iostream>
    using namespace std;
    
    hasguessedcorrectly = false;
    int secretnumber =  42;
    int input_number;
    
    while(!hasguessedcorrectly)
    {
         cout << "Guess a number: ";
         cin >> input_number;
         if(input_number = secretnumber)
         {
            cout << "nnYou got it right!";
            hasguessedcorrectly = true;
         }
    }
    
    return 0;
    Code:
    #include <iostream>
    using namespace std;
    int nm1;
    int nm2;
    
    cout << "Enter your first number: ";
    cin >> nm1;
    cout << "Enter your second number: ";
    cin >> nm2;
    int final = nm1 + nm2;
    cout << "The sum of the two numbers is " << final;
    return 0;
    Code:
    #include <iostream>
    using namespace std;
    int nm1;
    int nm2;
    
    cout << "Enter your first number: ";
    cin >> nm1;
    cout << "Enter your second number: ";
    cin >> nm2;
    int final = (nm1 + nm2)/2;
    cout << "The average of the two numbers is " << final;
    return 0;
    Code:
    #include <iostream>
    using namespace std;
    int nm1;
    int nm2;
    
    cout << "Enter your first number: ";
    cin >> nm1;
    cout << "Enter your second number: ";
    cin >> nm2;
    if(nm1 > nm2)
    {
        cout << "First number is the largest, its value is: " << nm1;
    } else {
        cout << "Second number is the largest, its value is: " << nm2;
    }
    return 0;
    {{ DiscussionBoard.errors[8504194].message }}
  • Profile picture of the author CMRaper
    Tell your girlfriend study C++ harder.
    {{ DiscussionBoard.errors[8504660].message }}
    • Profile picture of the author tonyla
      you mean input_number == secretnumber
      {{ DiscussionBoard.errors[8604131].message }}
      • Profile picture of the author tonyla
        tbk125 i mean this very kindly. Those are very trivial problems, looks like you gonna have a long semester.

        also a constant is

        int const secret = 42;
        {{ DiscussionBoard.errors[8604135].message }}
        • Profile picture of the author tonyla
          need to divide by 2.0 as 2 and 2.0 are different
          {{ DiscussionBoard.errors[8604139].message }}
  • you can assign float value if you need 2.0 but go for 2 if you need integer ..

    - Dhawal
    {{ DiscussionBoard.errors[8608513].message }}

Trending Topics