Download C++ Functions Exercises and more Exercises Computer Science in PDF only on Docsity!
C++
User-Defined Functions
Practice Exercises
1. Write a C++ program to implement the Number Guessing Game. In this game, the
computer chooses a random number between 1 and 100, and the player has to guess
the number in a maximum of 10 attempts. Each time the player enters a guess, the
computer tells him whether the guess is too high, too low, or right. Once the player
guesses the number, the game is over, and the program displays the message "YOU
WIN". If the 10 attempts are over and the user doesn't guess the number, the
program displays the message "YOU LOSE", along with the number that the user
should have guessed.
#include #include using namespace std; void main() { srand(time(0)); int num = rand() % 100 + 1; int guess, flag = 0; cout << "We have a secret number between 1 and 100.\nYou have 10 attempts to guess it.\n"; for (int i = 1; i <= 10; i++) { cout << "Attempt " << i << ": "; cin >> guess; if (guess == num) { flag = 1; cout << "YOU WIN!\n"; break; } else if (guess < num && i != 10) cout << "The secret number is higher.\n"; else if (guess > num && i != 10) cout << "The secret number is lower.\n"; } if (flag == 0) cout << "YOU LOSE! The secret number was: " << num << endl; }
2. An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3,
, 5 , and 7 are prime, but 4, 6, 8, and 9 are not.
a. Write a function to determine if a number is prime.
b. Use this function in a program that determines and prints all the prime numbers
between 1 and 1000.
#include using namespace std; #include bool prime(int); int main() { int count = 0; cout << "The prime numbers from 1 to 1000 are: \n"; for (int num = 2; num <= 1000; ++num) { if (prime(num)) { ++count; cout << setw(6) << num; if ((count % 10) == 0) cout << '\n'; } } cout << endl << endl; return 0; } bool prime(int n) { for (int i = 2; i <= n / 2; i++) { if ((n % i) == 0) return false; } return true; }
5. The Greatest Common Divider (GCD) of two integers is the largest integer that
evenly divides each of the numbers. Write a function that returns the Greatest
Common Divider of two integers.
#include using namespace std; int gcd(int, int); int main() { int a, b; for (int j = 1; j <= 3; ++j) { cout << "Enter two integers: "; cin >> a >> b; cout << "The Greatest Common Divisor of " << a << " and " << b << " is " << gcd(a, b) << "\n\n"; } } int gcd(int x, int y) { int greatest = 1; for (int i = 2; i <= ((x < y)? x : y); ++i) { if ((x % i == 0) && (y % i == 0)) greatest = i; } return greatest; }
6. Write a program that simulates coin tossing. For each toss of the coin, the program
should print Heads or Tails. Let the program toss the coin 100 times and count the
number of times each side of the coin appears. Print the results. The program
should call a separate function flip that takes no arguments and returns 0 for tails
and 1 for heads.
#include using namespace std; #include #include int flip(void); int main() { int headCount = 0, tailCount = 0; srand(time(0)); for (int loop = 1; loop <= 100; loop++) { if (flip() == 0) { tailCount++; cout << "Tails "; } else { headCount++; cout << "Heads "; } if ((loop % 10) == 0) cout << '\n'; } cout << "\n Number of Heads: " << headCount << "\n Number of Tails: " << tailCount << "\n\n"; } int flip(void) { return rand() % 2; }
#include using namespace std; int F5(int); void main() { cout << "Function5: \n"; cout << F5(8) << endl << endl; } // Recursive function int F5(int j) { if (j <= 1) return 1; if (j % 2) return j + F5(j - 2); return F5(j - 1); } OUTPUT: 16 #include using namespace std; void F6(int, float&); void main() { int ival; float fval; cout << "Function6: \n"; ival = 5; fval = 10.55; cout << ival << " " << fval << endl; F6(ival, fval); cout << ival << " " << fval << endl << endl; } // Call by reference void F6(int m, float& k) { m += 2; k += 2; cout << m << " " << k << endl; } OUTPUT: Function6: 5 10. 7 12. 5 12.
#include using namespace std; double F4(); float abc; void main() { cout << "Function4: \n"; cout << F4() << endl << endl; } double F4() { float abc = 1.5; ::abc = 0.25; cout << abc / ::abc << " *** "; { int abc = 2; cout << abc / ::abc << endl; } return abc; } OUTPUT: Function4: 6 *** 8 1. #include using namespace std; void F7(int = 1, float k = 2.5, int p = 5); void main() { cout << "Function7: \n"; F7(5, 7.5); F7(); F7(3); cout << endl; } void F7(int m, float k, int p) { m += 2; k += 2; p += 2; cout << m << " " << k << " " << p << endl; } OUTPUT: Function7: 7 9.5 7 3 4.5 7 5 4.5 7 #include using namespace std; void FunNy(int); void FunNy(double); void main() { cout << "FunNy Functions: \n"; FunNy(10); FunNy(10.0); cout << endl; } void FunNy(int a) { cout << "Not FunNy " << (a + 1) << endl; } void FunNy(double a) { cout << "So FunNy " << (a - 1) << endl; } OUTPUT: FunNy Functions: Not FunNy 11 So FunNy 9