EXERCISE 1
Write a function f having as parameters a table t of any size and an integer n indicating the size of the array. f must return with a return a boolean b indicating if there is a value between 0 and 10 in the n first boxes of the table t. Test this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <iostream> using namespace std; void enter (int t [], int n) { int i; for (i = 0; i <n; i ++) { cout << "Enter the number" << i << ":"; cin >> t [i]; } } bool f (int t [], int n) { bool finds = false; int i = 0; while (! finds && i <n) if (t [i]> = 0 && t [i] <= 10) find = true; else i ++; return finds; } int main () { int a [10]; enter (a, 10); bool b; b = f (a, 10); if (b) cout << "There is a value between 0 and 10" << endl; else cout << "There are no values between 0 and 10" << endl; return 0; } |
EXERCISE 2
Write a function f having as parameters a table t of any size and an integer n indicating the size of the array. f must return with a return the number of values between 0 and 10 in the first n boxes of the table t.Test this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; void enter (int t [], int n) { int i; for (i = 0; i <n; i ++) { cout << "Enter the number" << i << ":"; cin >> t [i]; } } int f (int t [], int n) { int nb = 0; int i; for (i = 0; i <n; i ++) if (t [i]> = 0 && t [i] <= 10) nb ++; return nb; } int main () { int a [10]; enter (a, 10); int x; x = f (a, 10); cout << "There is" << x << "value (s) between 0 and 10" << endl; return 0; } |
EXERCISE 3
Write a function f having as parameters a table t of any size and an integer n indicating the size of the array. f has another parameter v, integer passed by reference. f must return with a return a boolean b indicating if there is a value between 1 and 10 in the n first boxes of the table t. If f returns true, v is equal to the value of the first box in the array between 0 and 10. Test this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <iostream> using namespace std; void enter (int t [], int n) { int i; for (i = 0; i <n; i ++) { cout << "Enter the number" << i << ":"; cin >> t [i]; } } bool f (int t [], int n, int & v) { bool finds = false; int i = 0; while (! finds && i <n) if (t [i]> = 0 && t [i] <= 10) {find = true; v = t [i];} else i ++; return finds; } int main () { int a [10]; bool b; int w; enter (a, 10); b = f (a, 10, w); if (b) cout << "There is a value between 0 and 10:" << w << "is the first of these values." << endl; else cout << "There are no values between 0 and 10" << endl; return 0; } |
EXERCISE 4
Write a function f having as parameters a table t1 of any size and an integer n indicating the size of the array, as well as a table t2 of the same size as t1. f must return an integer nb indicating the number of values between 0 and 10 in table t1. f must put in table t2 the different values between 0 and 10 that he encountered in table t1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> using namespace std; void enter (int t [], int n) { int i; for (i = 0; i <n; i ++) { cout << "Enter the number" << i << ":"; cin >> t [i]; } } void show (int t [], int n) { int i; for (i = 0; i <n; i ++) cost << t [i] << ""; cout << endl; } int f (int t1 [], int n, int t2 []) { int i = 0, nb = 0; for (i = 0; i <n; i ++) if (t1 [i]> = 0 && t1 [i]; = 10) {t2 [nb] = t1 [i]; nb ++; return nb; } int main () { int a [10], b [10]; int nb; enter (a, 10); nb = f (a, 10 b); cout << "Here are the values between 0 and 10:" << endl; display (b, nb); return 0; } |
EXERCISE 5
Write a function f having as parameters a table t of any size and an integer n indicating the size of the array. f must return an integer equal to the index of the first box of the array (among the first n) between 0 and 10. If there is no such value, the function returns -1. Test this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <iostream> using namespace std; void enter (int t [], int n) { int i; for (i = 0; i <n; i ++) { cout << "Enter the number" << i << ":"; cin >> t [i]; } } int f (int t [], int n) { int i = 0, ind = -1; while (ind == - 1 && i <n) if (t [i]> = 0 && t [i] <= 10) ind = i; else i ++; return ind; } int main () { int a [10]; int w; enter (a, 10); w = f (a, 10); if (w! = - 1) cout << "There is a value between 0 and 10." << "the index of the first box is" << w << endl; else cout << "There are no values between 0 and 10" << endl; return 0; } |