Docsity

Problem Exercises Chapter 7, Summaries of Labour Law

Toggle sidebar
bg1
Problem Exercices Chapter 7
Page 1
Problem Exercises
1. Write C++ statements to accomplish each of the following:
a.Display the value of the seventh element of character array f.
b.Input a value into element 4 of single floating-point array b.
c. Initialize each of the 5 elements of single integer array g to 8.
d.Total and print the elements of floating-point array c of 100 elements.
e.Copy array โ€œaโ€ into the first portion of array โ€œbโ€. Assume that the two
arrays have the following declaration:
double a[11], b[34];
f. Determine and print the smallest and largest values contained in 99-
element floating-point array w.
Answer:
a) cout << f[6] << '\n';
b) cin >> b[3];
c) int g[5] = {8, 8, 8, 8, 8};
or
for (int j = 0; j < 5; j++) {
g[j] = 8;
}
d) float total = 0;
for (int k = 0; k < 100; k++) {
total += c[k];
cout << c[k] << '\n'
}
cout << total << '\n'
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Problem Exercises Chapter 7 and more Summaries Labour Law in PDF only on Docsity!

Page 1 Problem Exercises

1. Write C++ statements to accomplish each of the following: a.Display the value of the seventh element of character array f. b.Input a value into element 4 of single floating-point array b. c. Initialize each of the 5 elements of single integer array g to 8. d.Total and print the elements of floating-point array c of 100 elements. e.Copy array โ€œaโ€ into the first portion of array โ€œbโ€. Assume that the two arrays have the following declaration: double a[11], b[34]; f. Determine and print the smallest and largest values contained in 99- element floating-point array w. Answer: a) cout << f[6] << '\n'; b) cin >> b[3]; c) int g[5] = {8, 8, 8, 8, 8}; or for (int j = 0; j < 5; j++) { g[j] = 8; } d) float total = 0; for (int k = 0; k < 100; k++) { total += c[k]; cout << c[k] << '\n' } cout << total << '\n'

Page 2 e) for (int i = 0; i < 11; i++) { b[i] = a[i]; } f) // Assume that all variables are declared and initialized for (int j = 0; j < 99; j++) { if (w[j] < smallest) { smallest = w[j]; } else if (w[j] > largest) { largest = w[j]; } }

Page 4 f) t[0][2], t[1][2] g) t[0][1] = 0; h) t[0][0] = 0; t[0][1] = 0; t[0][2] = 0; t[1][0] = 0; t[1][1] = 0; t[1][2] = 0; i) for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) t[i][j] = 0; } } j) for (int r = 0; r < 2; r++) { for (int c = 0; c < 3; c++) { cin >> t[r][c] ; } } or cin >> t[0][0] >> t[0][1] >> t[0][2]; cin >> t[1][0] >> t[1][1] >> t[1][2]; k) int smallest = t[0][0]; for (int r = 0; r < 2; r++) { for (int c = 0; c < 3; c++) { if (t[r][c] < smallest) { smallest = t[r][c]; } } } cout << smallest;

Page 5 l) cout << t[0][0] << ' ' << t[0][1] << ' ' << t[0][2] <<'\n'; m) Array โ€œtโ€ does not contain a fourth column. n) cout << " 0 1 2 \n"; for (int r = 0; r < 2; r++) { cout << r << ' '; for (int c = 0; c < 3; c++) { cout << t[r][c] << " "; } cout << '\n'; } o) int total = 0; for (int n = 0; n < 2; n++) { total += t[n][1]; }

Page 7

5. Label the elements of 3-by-5 double-subscripted array sales to indicate the

order in which they are set to zero by the following program segment: for (int row = 0; row < 3; row++) { for (int column = 0; column < 5; column++) { sales[row][column] = 0; } } Answer: sales[0][0], sales[0][1], sales[0][2], sales[0][3], sales[0][4], sales[1][0], sales[1][1], sales[1][2], sales[1][3], sales[1][4], sales[2][0], sales[2][1], sales[2][2], sales[2][3], sales[2][4]

Page 8

6. Having 2 integer arrays sArr and dArr of the same size. Write a function that

fills the array dArr with values such that each element of dArr is the corresponding value of array sArr rounded up to the next multiple of 10. Thus, 1 round to 10, 10 stays as 10, 11 rounds to 20, 28 rounds to 30, and so on... #include #include #include <stdlib.h> #include using namespace std; void roundArr(const int [], int [], int); int main() { const int size = 10; int sArr[size], dArr[size]; srand((unsigned)time(NULL)); for (int i = 0; i < size; i++) { sArr[i] = 1 + (rand() % 100); cout << setw(4) << sArr[i]; } cout << endl; roundArr(sArr, dArr, size); for (int j = 0; j < size; j++) { cout << setw(4) << dArr[j]; } cout << endl; return 0; }

Page 10

7. Having 2 two-dimensional arrays sArr and dArr of the same size. Write a

function that fills the array dArr with values such that each element of dArr is the corresponding value of array sArr minus the minimum value of array sArr. Minimum Value sArr dArr 14 3 21 6 43 64 23 96 10 13 8 7 #include #include <stdlib.h> #include using namespace std; int findMin(const int [][4], int, int); void remBias(const int [][4], int [][4], int, int, int); void dispArr(const int [][4], int, int); void main() { const int rows = 3, cols = 4; int sArr[rows][cols], dArr[rows][cols]; int minimum; srand((unsigned)time(NULL)); for (int i = 0 ; i < rows; i++) for (int j = 0; j < cols; j++) sArr[i][j] = (rand() % 100) + 1; dispArr(sArr, rows, cols); minimum = findMin(sArr, rows, cols); cout << "\n The minimum is: " << minimum << "\n\n"; remBias(sArr, dArr, rows, cols, minimum); dispArr(dArr, rows, cols); 11 0 18 3 40 61 20 93 7 10 5 4

Page 11 }

Page 13 Sample output: