List of C++ Function Exercises (Resolved and Commented)

We have reached the end of another section of our C++ course, about functions.
In the exercises below, you should use all your knowledge learned so far, such as the basics, loops, loopings, conditional tests and, of course, function.

Use and use again the functions. Create many functions.
Each function must do a single thing, in the simplest and most direct way possible.

Make them easily coupled, that is, you should receive and return information whenever possible. This allows you to use your function codes later.

Remember: a large program is nothing more than a series of small programs.
Your functions should be these little programs, making specific functionalities, ok?

Function Exercises in C++

01. Create a program that receives two smaller sides of a right triangle and a function returns the value of the hypotenuse.

02. Create a program that receives the three sides of a triangle, passes these values ​​to a function that says whether that triangle exists or not (due to the condition of the triangle's existence, each side must be greater than the subtraction module of the other two sides and must be less than the sum of the other two sides)

03. Make a program that asks the user for a positive integer 'n' and print a square of side 'n' filled with hashtags. For example, for n = 4, it should appear on the screen:

####
####
####
####

04. Program a software that receives three numbers, stops for a function and it returns the largest one. Make another function that receives the same numbers and returns the smallest one.


05. A number is said to be perfect when it is equal to the sum of its divisors.
For example, six is ​​perfect, because: 6 = 1 + 2 + 3
Programs a software that asks the user for a number and tells them if it is perfect or not.

06. Create software that receives a number from the user, passes that value to a function and it returns that number written in reverse. For example, you gave the value 1234, so it will return 4321. Tip: first, create a function that counts how many digits a number has.

07. Make a program to toss a coin. When we call a function, it must return heads or tails. In another function, make 'n' coin tosses, 'n' is the amount the user wants, and show the percentage of times he has heads and tails. If you flip the coin 10, 100, 1000, a million times ... what tends to happen?

08. Create data in C++. Roll the dice: that is, a function must draw a random number from 1 to 6. Now, make the previous die roll 100 times, a thousand times and 1 million times. Each time it runs, you must store the value it provided, at the end, you show how many times each number was drawn. Does it match the results of the statistic?

09. Create an odds and even game. You must choose 0 for even or 1 for odd, then provide a number. The computer generates a number from 0 to 10, adds the values ​​and says who won, besides showing the score and asking if you want to play another round.
How to program odds or even in C++

10. Like the odd or even game, create the Rock, Paper or Scissors game, in C ++.

11. Create a game where the computer draws a number from 1 to 10, and you try to guess what it is.

12.  Are we really going to make a cool and interesting game? Make the computer draw a number from 1 to 100. Each time you kick, it should tell you if you kicked below the real value, above or if you got it right. At the end, it tells you the number of attempts you had and whether or not you hit the record. Oh, at the end of each round, the program asks if you want to play again or not, displaying the current record.
Guess the number

No comments:

Post a Comment