Three simple c++ exercises

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • just curious
    New Member
    • Nov 2009
    • 5

    #1

    Three simple c++ exercises

    Create a C++ console application that uses a while loop to count, total, and average a series of positive integers entered by a user. The user enters a –1 to signal the end of data input and to display the count, total, and average of the numbers entered.

    The pseudocode for the program is:

    Start

    Enter user's input

    Begin Loop

    While user's input is greater than sentinel

    increment counter

    add user's input to the sum

    Enter user's input again

    End Loop

    Display messages

    Stop







    Exercise 2:


    Write a program that uses at least one for loop to print a section of the multiplication table. The user should be able to specify the section of the table to be displayed. The program will also display a count of the number of rows printed, the count of the columns printed and the sum of all calculated values.

    The user input will appear as such:

    **Create a Multiplication Table**
    Enter the starting value: 2
    Enter the ending value: 5

    The pseudocode for the program is:

    Start

    Enter user's Table dimension (start and end number)

    Begin For Loop (colnum = startnumber, colnum<=endnumb er, increment colnum)

    display colnum

    increment colnum

    End For loop

    initialize currentRow as startnumber

    Begin while loop (until curentRow is less than or equal to endnumber)

    display currentRow

    increment Rowcount

    Begin For Loop (colnum = startnumber, colnum<=endnumb er, increment colnum)

    display product of colnum and currentRow

    Add the product of colnum and currentRow to the sum

    End For Loop

    increment currentRow

    End of While Loop

    Display row, col and sum printed

    Stop


    The output will be:

    X 2 3 4 5
    2 4 6 8 10
    3 6 9 12 15
    4 8 12 16 20
    5 10 15 20 25



    rows printed = 4
    Columns printed = 4
    Sum: 196


    Exercise 3, last one

    Create a C++ console application that generates a random number that must be guessed by a user. The program will indicate to the user if their guess is correct, too high or too low. It will also report the number of guesses the user has made. The game ends when the user enters a negative one (-1) or has guessed the number. The program should include a do while loop, decision statements, constants and variables.

    The pseudocode for the program is:

    Start
    Generate a random number

    Begin loop
    Enter user's guess
    If guess is right
    Display message and exit loop
    If guess is high
    Display message
    If guess is low display message
    Display message
    Increment counter
    End loop

    Display number of guesses
    Stop


    When the program is executed it will produce the following output:

    **The Guessing Game**

    Enter a Guess (-1 to exit):

    After the user makes a guess the program will output one of these statements:

    Too High, Try Again!
    Too Low, Try Again!
    You’ve guessed wisely!
    After a correct guess or the user ends execution the program will output one of these statements:

    The number was X it took you X guesses.
    The number was X you gave up after X guesses.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    No one is going to write your code for you.

    If you have a specific question, then you will receive assistance.

    Post again when you have got some of this coded and are still stuck.

    Comment

    • just curious
      New Member
      • Nov 2009
      • 5

      #3
      this is my third week in learning c++ and i do not know where to start

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        3 weeks is quite enough to master all language constructions required for such tasks. If they were not explained to you, start with googling for 'c++ loop example'

        Comment

        • just curious
          New Member
          • Nov 2009
          • 5

          #5
          for the second exercise i am very confused as it seems there are three loops total required and way too many variables to be initilized. this is what i came up with, can someone please help me find out what i did wrong?

          Code:
          //create a multiplication table
          #include <iostream>
          # include <iomanip>
          using namespace std;
           
          int main()
          { 
          int startValue, int endValue, int currentCol, int currentRow;
          for(currentCol=startValue, currentCol<=endValue,startValue++);
          cout<<" the column is : " << currentCol<< endl;
          }
          while(currentRow <=endValue);
          cout<<" the current row is :" << currentRow<< endl;
          cout << currentRow ++;
          that is all i got can you please test what you have before so i ll know what i did wrong? i appreciate any help as you can see i need it bad :(

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            It doesn't even compile - variable declaration is wrong, 'for' loop syntax is wrong.
            Semicolon after 'for' should not be there. While loop appears afer closing brace of main function.

            Comment

            Working...