473,853 Members | 1,839 Online

# Three simple c++ exercises

5 New Member
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.
Nov 8 '09 #1
5 9373
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Nov 8 '09 #2
just curious
5 New Member
this is my third week in learning c++ and i do not know where to start
Nov 8 '09 #3
newb16
687 Contributor
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'
Nov 8 '09 #4
just curious
5 New Member
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?

Expand|Select|Wrap|Line Numbers
1. //create a multiplication table
2. #include <iostream>
3. # include <iomanip>
4. using namespace std;
5.
6. int main()
7. int startValue, int endValue, int currentCol, int currentRow;
8. for(currentCol=startValue, currentCol<=endValue,startValue++);
9. cout<<" the column is : " << currentCol<< endl;
10. }
11. while(currentRow <=endValue);
12. cout<<" the current row is :" << currentRow<< endl;
13. cout << currentRow ++;
14.
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 :(
Nov 10 '09 #5
newb16
687 Contributor
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.
Nov 10 '09 #6