Chapter7 - Practice Exercises - Cpp-đã chuyển đổi
Chapter7 - Practice Exercises - Cpp-đã chuyển đổi
* Exercise 7.1
Write a program that asks for the number of hours worked by six employees. It stores the
values in an array.
Solution 7.1 (a)
#include <iostream>
using namespace
std/ int main()
int main()
int largest=arr[0]:
int /
for (i=0; ask; ++)
if (arr[i]>largest)
largest=arr[i];
return (largest);
int main()
cin>>pos:
if (pos<1 or pos > size)
return 0;
int k;
for (k=0; k<j;k++)
int n;
if(m>l or m<1)
else
return (arr) r
int main()
int size, ;
float a[100], average r
// Enter the size of the array
cout<<"Enter array size (Max:50):
"/ cin>>size r
//check whether size < 1 or not
if (size<l)
return 0;
int k;
for (k=0; k<j;k++)
int lr
float average;
float sum=0.0;
for (l=0/ l< / l++)
sum+=arr[l] /
average = sum /
return (average) /
* Exercise 7.5: Find the average of the largest and smallest element in the array
Write a program that allows declaring an array, the number of elements (size of the
array) and values for its elements. Then find calculate and display the average of the
largest and the smallest element.
* Exercise 7.6: Find the average of even and odd numbers in the array
Write a program that allows declaring an array, the number of elements (size of the
array) and values for its elements. Then calculate and display:
- The average of even number (values) of the array.
- The average of odd number (values) of the array.
* Exercise 7.7: Find duplicate element in an array
Write a program that allows declaring an array, the number of elements (size of the
array) and values for its elements. Then find and display all values that appear 2 or
more times in the array. For example, with the array [ 1 2 3 4 3 4 2 3 5 6], the result
will be: 2 3 4
* Exercise 7.8: Insert new element into an array
Write a program that allows declaring an array, the number of elements (size of the
array) and values for its elements. and then ask the user to enter the index to insert a
new element. Display the elements of the array after insertion.
* Exercise 7.9: Delete the duplicate values in an array
Write a program that allows declaring an array, the number of elements (size of the
array) and values for its elements. Then find and delete all values that appear from
2nd onwards in the array. Display the elements of the array after deletion. For
example, with the array [ 1 2 3 4 3 4 2 3 5 6], the result will be: [1 2 3 4 5 6];
* Exercise 7.10: Multiply Two arrays
Write a program that allows declaring TWO arrays with the same number of elements
(size of the array), then input the values for the two elements. Calculates and replaces
the values of elements in the first array by multiplying the value of the element in the
first array by the value of the element in the same position in the second array.
Display the values of the elements of the first array after replacing. For example, with
the first array [ 1 2 3 4 ] and the second array [2 3 4 5] the result will be: [2 6 12 20];
* Exercise 7.11 Two-dimension array:
Write a program that allows the user to:
- Declare an integer 2-dimensional array by declaring the size of the array (row and
column);
- Enter the elements of the array from the keyboard;
- Display the elements of the array including the index (row and column) and the
corresponding value of each element;
- Calculate and display the sum of value of elements in each row;
- Calculate the display sum of value of elements in each column.
For example: Assume that, you declare a 2-dimensional array A which consists of 2
rows and 3 columns. And you enter 6 values for the elements of the array as follows:
2
4
6
8
So, result of displaying the elements of the array including the index (row and column)
and the corresponding value of each element is as following:
A[0][OJ —- 2 A[0][1J —- 4 A[0J[2] —- 6
A[1J[0J —— 8 A[1J[1J = 10 A[1J[2J = 12
And, result of calculating of sum of element’s values in each row and column as follows:
Calculate and display the sum of value of elements in each row:
Sum of value of elements in row 0: 12
Sum of value of elements in row 1: 30
Calculate and display the sum of value of elements in each column:
Sum of value of elements in column 0:
10 Sum of value of elements in column
1: 14 Sum of value of elements in
column 2: 18
Solution 7.11
#include<iostream>
using namespace
std;
void Display_Array (int[10][10], int,
int)/ void Row Sum (int[10][10],
int, int); void Col Sum (int[10]
[10], int, int)/
int main()
return 0;
void
Display_Array (int arr[10][10], int row, int col)
int m,n;
for(m=0: m<row; m++)
cout<<endl;
int m,n;
for(m=0; m<row; m++)
int sumRow=0;
for(n=0; n<col; n++)
sumRow += arr[m][n];
cout<<endl;
int m,n;
for(n=0 r n<COl r n+
+)
int sumCOl=0 /
for(m=0; m<row; m++)
sumCol += arr[m][n] r
cout<<endl;
Example
8
3
11
9
11
Output
8
3
11
9
11
4