Uploaded by andrewnguyenn23

PRF192 03 Pointers Contiguous Storage Parallel Arrays (1)

advertisement
PRF192 – Programming Fundamentals Using C
Contiguous Storage
Part 03
Contiguous Storage
------------------------------ BASIC -----------------------------1. Write a program that allows users to enter a numeric array with N elements. The program
will count and display the number of negative numbers, the number of zeros and the number
of positive numbers.
Example 1: How many element of numeric array? 0
Example 2:
The number of element of numeric array must be greater than 0!
How many element of numeric array? 10
Please enter value for 10 elements:
The value of a[0] is: -7
The value of a[1] is: 1
The value of a[2] is: 9
The value of a[3] is: -15
The value of a[4] is: 0
The value of a[5] is: 13
The value of a[6] is: -6
The value of a[7] is: 4
The value of a[8] is: 0
The value of a[9] is: 58
The number of negative elements is 3
The number of zero
elements is 2
The number of positive elements is 5
2. Write a program that allows users to enter a numeric array with N elements. The program
will count and display the number of odd numbers and the number of even numbers.
Example 1: How many element of numeric array? -8
Example 2:
The number of element of numeric array must be greater than 0!
How many element of numeric array? 10
Please enter value for 10 elements:
The value of element 1 is: 19
The value of element 2 is: -25
The value of element 3 is: 36
The value of element 4 is: 0
The value of element 5 is: 1
The value of element 6 is: 3
The value of element 7 is: -47
The value of element 8 is: 0
The value of element 9 is: 59
The value of element 10 is: 12
The number of odd elements is 6
The number of even elements is 4
3. Write a program that allows users to enter a numeric array with N elements. The program
will sum and average the elements of the array.
Example 1: How many element of numeric array? -8
Example 2:
Võ Hồng Khanh
The number of element of numeric array must be greater than 0!
How many element of numeric array? 10
Please enter value for 10 elements:
Value of the 1st element is: 19
Value of the 2nd element is: 28
Value of the 3rd element is: 3
Value of the 4th element is: 482
Value of the 5th element is: 12
Value of the 6th element is: 5
Value of the 7th element is: 41
Value of the 8th element is: 156
Value of the 9th element is: 300
Value of the 10th element is: -30
Value of the 11th element is: 184
Value of the 12th element is: 6
The total
value of the numeric array is 1206
The average value of the numeric array is 100.50
khanhvh@fe.edu.vn
Page 1 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
4. Write a program that allows users to enter a numeric array with N elements. The program
will find and display the minimum and the maximum value of the array.
Example 1:
Example 2:
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 10
Please enter value for 10 elements:
Value of the 1st element is: 33
Value of the 2nd element is: 18
Value of the 3rd element is: -85
Value of the 4th element is: -44
Value of the 5th element is: 67
Value of the 6th element is: 0
Value of the 7th element is: -5
Value of the 8th element is: -85
Value of the 9th element is: 478
Value of the 10th element is: -27
The minimum value of the numeric array is -85
The maximum value of the numeric array is 478
5. Write a program that allows users to enter a numeric array with N elements. The program
will display all elements of the array in reverse order.
Example 1:
Example 2:
How many element of numeric array? -5
The number of element of numeric array must be greater than 0!
How many element of numeric array? 5
Please enter value for 5 elements:
Value of the 1st element is: 24
Value of the 2nd element is: 52
Value of the 3rd element is: 7
Value of the 4th element is: 182
Value of the 5th element is: -6
The entered array is:
24, 52, 7, 182, -6
The reverse array is:
-6, 182, 7, 52, 24
------------------------------ NUMERALS -----------------------------6. Write a program that allows entering an integer N ≥ 0. The program will convert and display
the binary value of the entered number.
Example 1: Please enter integer N: -3
Example 2:
N must be greater than or equal 0!
Please enter integer N: 277
Conversion results: 277d = 100010101b
7. (*) Write a program that allows entering an integer N (2 bytes  16 bits). The program will
convert and display the binary value of the entered number.
Hint: represent negative numbers in the form of 2's complement.
Example 1: Please enter integer N: 240
Example 2:
Example 3:
Example 4:
Example 5:
Example 6:
Võ Hồng Khanh
Conversion results: 240d = 0000000011110000b
Please enter integer N: -240
Conversion results: 240d = 1111111100010000b
Please enter integer N: 1
Conversion results: 240d = 0000000000000001b
Please enter integer N: -1
Conversion results: 240d = 1111111111111111b
Please enter integer N: 2415
Conversion results: 240d = 0000100101101111b
Please enter integer N: -2415
Conversion results: 240d = 1111011010010001b
khanhvh@fe.edu.vn
Page 2 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
8. Write a program that allows entering an integer N ≥ 0. The program will convert and display
the hexadecimal value of the entered number.
Example 1:
Example 2:
Example 3:
Please enter integer N: -3
N must be greater than or equal 0!
Please enter integer N: 51966
Conversion results: 51966d = CAFEh
Please enter integer N: 61946
Conversion results: 61946d = F1FAh
Theory:
# The form of binary number B is bn-1bn-2 .b2b1b0.
# Binary (the base value: q = 2) to decimal conversion formula is:
N = bn-1qn-1 + bn-2qn-2 + . + b2q2 + b1q1 + b0q0
For example:
* B = 0110000000001101b
* Decimal value of B is
N = 0*215 + 1*214 + 1*213 + 0*212 + 0*211 + 0*210 + 0*29 + 0*28 + 0*27 + 0*26 + 0*25 + 0*24 + 1*23 + 1*22 + 0*21 + 1*20
=
1*214 + 1*213 +
1*23 + 1*22 +
1*20
=
16384 + 8192 +
8 +
4 +
1
= 24589
* So, the decimal value of the binary number 0110000000001101b is 24589d
9. (*) Write a program that allows user to enter a 16-bit binary number and then convert this
binary number into decimal values.
Hint: Use .long long. datatype to present a 16-bit binary number.
Example 1:
Example 2:
Please a 16-bit binary number: 1011101010111110
Conversion results: 1011101010111110b = 47806d
Please a 16-bit binary number: 0110000000011101
Conversion results: 0110000000011101b = 24605d
10. (*) Write a program that allows user to enter the integer N ≥ 0 with the base number A. The
program will convert and display the value of N with the base number B.
Note: - N has a maximum of 16 digits.
- A must be 2 or 8 or 10.
- B must be 2 or 8 or 10 or 16.
Example 1: Please an integer number: 235312
Example 2:
Example 3:
Võ Hồng Khanh
Please enter the base A of your number:
The base A must be 2 or 8 or 10!
Please enter the base A of your number:
Please enter the base B of the result :
The base B must be 2 or 8 or 10 or 16!
Please enter the base B of the result :
Conversion results: 235312q = 80586d
16
8
-5
10
Please an integer number: 0110000000001101
Please enter the base A of your number: 2
Please enter the base B of the result : 16
Conversion results: 0110000000011101b = 600Dh
Please an integer number: 130784
Please enter the base A of your number: 8
130784 is not an octal number!
Please enter the base A of your number: 2
130784 is not a binary number!
Please enter the base A of your number: 10
Please enter the base B of the result : 2
Conversion results: 130784d = 00010011101011001010b
khanhvh@fe.edu.vn
Page 3 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
Theory:
In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the
Fibonacci sequence, such that each number is the sum of the two preceding ones. The sequence is
starting from 0 and 1. That is,
=
−
+
−
>
In some books, and particularly in old ones, F0, the "0" is omitted, and the Fibonacci sequence
starts with F1 = F2 = 1. The beginning of the sequence is thus:
(0,) 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . etc
11. Write a program that displays the Fibonacci sequence with N numbers.
Example 1: Please enter positive integer N: -5
Example 2:
N must be greater or equal 0!
Please enter positive integer N: 8
The Fibonacci sequence with 8 numbers is:
0, 1, 1, 2, 3, 5, 8, 13
12. Write a program to display the Nth element of the Fibonacci sequence.
Example 1: Please enter positive integer N: -17
Example 2:
N must be greater or equal 0!
Please enter positive integer N: 10
34 is the 10th element of the Fibonacci sequence
------------------------------ SORTING -----------------------------13. Write a program that allows users to enter a numeric array with N elements. The program
uses Bubble sort to sort the array in an ascending order and display the sorted array.
Example 1: How many element of numeric array? -8
Example 2:
The number of element of numeric array must be greater than 0!
How many element of numeric array? 10
Please enter value for 10 elements:
Value of the 1st element is: 19
Value of the 2nd element is: 28
Value of the 3rd element is: -3
Value of the 4th element is: -482
Value of the 5th element is: 0
Value of the 6th element is: 5
Value of the 7th element is: 41
Value of the 8th element is: -156
Value of the 9th element is: 300
Value of the 10th element is: 184
The entered array is:
19, 28, -3, -482, 0, 5, 41, -156, 300, 184
The sorted array is:
.
-482, -156, -3, 0, 5, 19, 28, 41, 184, 300
14. Write a program that allows users to enter a numeric array with N elements. The program
uses Selection sort to sort the array in a descending order and display the sorted array.
15. Write a program that allows users to enter a numeric array with N elements. The program
uses Insertion sort to sort the array in an ascending order and display the sorted array.
Võ Hồng Khanh
khanhvh@fe.edu.vn
Page 4 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
16. Write a program that allows users to enter a numeric array with N elements. The program
checks whether the entered array has been sorted in ascending or descending order.
Example 1:
Example 2:
Example 3:
Example 4:
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 5
Please enter value for 5 elements:
Value of the 1st element is: 6
Value of the 2nd element is: 7
Value of the 3rd element is: 10
Value of the 4th element is: 15
Value of the 5th element is: 28
The entered array is:
6, 7, 10, 15, 28
The array is sorted in an ascending order
How many element of numeric array? 5
Please enter value for 5 elements:
Value of the 1st element is: 852
Value of the 2nd element is: 419
Value of the 3rd element is: 36
Value of the 4th element is: 4
Value of the 5th element is: -5
The entered array is:
852, 419, 36, 4, -5
The array is sorted in an descending order
How many element of numeric array? 4
Please enter value for 4 elements:
Value of the 1st element is: 2
Value of the 2nd element is: 19
Value of the 3rd element is: -7
Value of the 5th element is: 0
The entered array is:
2, 19, -7, 0
The array is not sorted
------------------------------ DATA STRUCTURES -----------------------------17. [Insert new element] Write a program that allows users to enter 3 information:
1) A numeric array A with N elements.
2) A newValue value.
3) An index value.
The program will insert newValue into the place that indexed index.
Example 1:
Example 2:
Example 3:
Võ Hồng Khanh
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 3
Please enter value for 3 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
The entered array is:
4, -2, 19
Please enter the value that you want to insert: 27
Please enter index that the new value will be inserted: -7
The index must be from 0 to 3!
Please enter index that the new value will be inserted: 2
The array after insert 27 into the place that indexed [2] is:
4, -2, 27, 19
How many element of numeric array? 3
Please enter value for 3 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
The entered array is:
4, -2, 19
Please enter the value that you want to insert: 27
Please enter index that the new value will be inserted: 3
The array after insert 27 into the place that indexed [3] is:
4, -2, 19, 27
khanhvh@fe.edu.vn
Page 5 / 16
PRF192 – Programming Fundamentals Using C
Example 4:
Contiguous Storage
How many element of numeric array? 3
Please enter value for 3 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
The entered array is:
4, -2, 19
Please enter the value that you want to insert: 27
Please enter index that the new value will be inserted: 0
The array after insert 27 into the place that indexed [0] is:
27, 4, -2, 19
18. [Remove element by index] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements.
2) An index value.
The program will remove the element that indexed index.
Example 1:
Example 2:
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 5
Please enter value for 5 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
The entered array is:
4, -2, 19, 36, 10
Please enter index of the element that you want to remove: 5
The index must be from 0 to 4!
Please enter index of the element that you want to remove: 2
The array after remove the element that indexed [2] is:
4, -2, 36, 10
19. [Search first matched element] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements.
2) A searchValue.
The program will find the first element which value is equal with searchValue.
Example 1:
Example 2:
Example 3:
Võ Hồng Khanh
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 8
Please enter value for 8 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
Value of the 6th element is: 19
Value of the 7th element is: -5
Value of the 8th element is: 19
The entered array is:
4, -2, 19, 36, 10, 19, -5, 19
Please enter the value that you want to find: 5
The value 5 is not found in this array
How many element of numeric array? 8
Please enter value for 8 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
Value of the 6th element is: 19
Value of the 7th element is: -5
Value of the 8th element is: 19
The entered array is:
4, -2, 19, 36, 10, 19, -5, 19
Please enter the value that you want to find: 19
The first element which value is 19 is found at index [2]
khanhvh@fe.edu.vn
Page 6 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
20. [Search last matched element] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements.
2) A searchValue.
The program will find the last element which value is equal with searchValue.
Example 1:
Example 2:
Example 3:
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 7
Please enter value for 7 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 19
Value of the 6th element is: -5
Value of the 7th element is: 19
The entered array is:
4, -2, 19, 36, 19, -5, 19
Please enter the value that you want to find: 7
The value 7 is not found in this array
How many element of numeric array? 7
Please enter value for 7 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 19
Value of the 6th element is: -5
Value of the 7th element is: 19
The entered array is:
4, -2, 19, 36, 19, -5, 19
Please enter the value that you want to find: 19
The last element which value is 19 is found at index [6]
21. [Search all matched elements] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements.
2) A searchValue.
The program will find the all elements which value is equal with searchValue.
Example 1:
Example 2:
Example 3:
Võ Hồng Khanh
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 8
Please enter value for 8 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
Value of the 6th element is: 19
Value of the 7th element is: -5
Value of the 8th element is: 19
The entered array is:
4, -2, 19, 36, 10, 19, -5, 19
Please enter the value that you want to find: 7
The value 7 is not found in this array
How many element of numeric array? 8
Please enter value for 8 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
Value of the 6th element is: 19
Value of the 7th element is: -5
Value of the 8th element is: 19
The entered array is:
4, -2, 19, 36, 10, 19, -5, 19
Please enter the value that you want to find: 19
All elements whose values are 19 are found at indexes:
[2], [5], [7]
khanhvh@fe.edu.vn
Page 7 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
22. [Replace all matched elements] Write a program that allows users to enter 3 information:
1) A numeric array A with N elements.
2) An oldValue.
3) A newValue.
The program will replace all elements which value is equal with oldValue by newValue.
Example 1:
Example 2:
Example 3:
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 8
Please enter value for 8 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
Value of the 6th element is: 19
Value of the 7th element is: -5
Value of the 8th element is: 19
The entered array is:
4, -2, 19, 36, 10, 19, -5, 19
Please enter the value that you want to find: 1
The value 1 is not found in this array
How many element of numeric array? 8
Please enter value for 8 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 36
Value of the 5th element is: 10
Value of the 6th element is: 19
Value of the 7th element is: -5
Value of the 8th element is: 19
The entered array is:
4, -2, 19, 36, 10, 19, -5, 19
Please enter the old value that you want to find: 19
Please enter the new value that you want to replace: 1000
The array after replaced all elements whose value is 19 by 1000:
4, -2, 1000, 36, 10, 1000, -5, 1000
23. [Remove all matched elements] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements.
2) An searchValue.
The program will remove all elements which value is equal with searchValue.
Example 1:
Example 2:
Example 3:
Võ Hồng Khanh
How many element of numeric array? 0
The number of element of numeric array must be greater than 0!
How many element of numeric array? 3
Please enter value for 3 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
The entered array is:
4, -2, 19
Please enter the value that you want to find: 1
The value 1 is not found in this array
How many element of numeric array? 7
Please enter value for 7 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 19
Value of the 4th element is: 19
Value of the 5th element is: 36
Value of the 6th element is: -5
Value of the 7th element is: 19
The entered array is:
4, -2, 19, 19, 36, -5, 19
Please enter the value that you want to remove: 19
The array after remove all elements whose value is 19:
4, -2, 36, -5
khanhvh@fe.edu.vn
Page 8 / 16
PRF192 – Programming Fundamentals Using C
Example 4:
Contiguous Storage
How many element of numeric array? 7
Please enter value for 7 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -2
Value of the 3rd element is: 5
Value of the 4th element is: 5
Value of the 5th element is: 5
Value of the 6th element is: 19
Value of the 7th element is: 5
The entered array is:
4, -2, 5, 5, 5, 19, 5
Please enter the value that you want to remove: 5
The array after remove all elements whose value is 5:
4, -2, 19
24. [Merge 2 arrays] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements.
2) A numeric array B with M elements.
The program will append all elements of array B after array A.
Example 1:
Example 2:
How many element of numeric array A ? 0
The number of element of array A must be greater than 0!
How many element of numeric array A? 3
Please enter value for 3 elements:
Value of the 1st element is: 4
Value of the 2nd element is: -1
Value of the 3rd element is: 97
How many element of numeric array B? -9
The number of element of array B must be greater than 0!
How many element of numeric array B? 4
Please enter value for 4 elements:
Value of the 1rd element is: 19
Value of the 2th element is: 36
Value of the 3th element is: 22
Value of the 4th element is: -5
The array A is:
4, -1, 97
The array B is:
19, 36, 22, -5
The merged array after append all elements of array B after array A is:
4, -1, 97, 19, 36, 22, -5
25. [Merge 2 sorted arrays] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements that sorted ascending.
2) A numeric array B with M elements that sorted ascending.
The program will merge array A and array B into one array that sorted ascending.
Note: You CAN NOT append array B after array A and then sort the result array.
Example 1:
Example 2:
Võ Hồng Khanh
How many element of numeric array A ? 0
The number of element of array A must be greater than 0!
How many element of numeric array A? 3
Please enter value for 3 elements:
Value of the 1st element is: 5
Value of the 2nd element is: 49
Value of the 3rd element is: 97
How many element of numeric array B? -9
The number of element of array B must be greater than 0!
How many element of numeric array B? 4
Please enter value for 4 elements:
Value of the 1rd element is: -8
Value of the 2th element is: 7
Value of the 3th element is: 25
Value of the 4th element is: 190
The array A is:
5, 49, 97
The array B is:
-8, 7, 25, 190
The merged array after merge array A and array B into once array that sorted ascending:
-8, 5, 7, 25, 49, 97, 190
khanhvh@fe.edu.vn
Page 9 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
26. [Merge 2 sorted arrays] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements that sorted ascending.
2) A numeric array B with M elements that sorted ascending.
The program will merge array A and array B into one array that sorted descending.
Note: You CAN NOT append array B after array A and then sort the result array.
Example 1:
Example 2:
How many element of numeric array A ? 0
The number of element of array A must be greater than 0!
How many element of numeric array A? 3
Please enter value for 3 elements:
Value of the 1st element is: 5
Value of the 2nd element is: 49
Value of the 3rd element is: 97
How many element of numeric array B? -9
The number of element of array B must be greater than 0!
How many element of numeric array B? 4
Please enter value for 4 elements:
Value of the 1rd element is: -8
Value of the 2th element is: 7
Value of the 3th element is: 25
Value of the 4th element is: 190
The array A is:
5, 49, 97
The array B is:
-8, 7, 25, 190
The merged array after merge array A and array B into once array that sorted ascending:
190, 97, 49, 25, 7, 5, -8
27. [Merge 2 sorted arrays] Write a program that allows users to enter 2 information:
1) A numeric array A with N elements that sorted ascending.
2) A numeric array B with M elements that sorted descending.
The program will merge array A and array B into one array that sorted descending.
Note: You CAN NOT append array B after array A and then sort the result array.
Example 1:
Example 2:
Võ Hồng Khanh
How many element of numeric array A ? 0
The number of element of array A must be greater than 0!
How many element of numeric array A? 3
Please enter value for 3 elements:
Value of the 1st element is: 5
Value of the 2nd element is: 49
Value of the 3rd element is: 97
How many element of numeric array B? -9
The number of element of array B must be greater than 0!
How many element of numeric array B? 4
Please enter value for 4 elements:
Value of the 1rd element is: 190
Value of the 2th element is: 25
Value of the 3th element is: 7
Value of the 4th element is: -8
The array A is:
5, 49, 97
The array B is:
190, 25, 7, -8
The merged array after merge array A and array B into once array that sorted ascending:
190, 97, 49, 25, 7, 5, -8
khanhvh@fe.edu.vn
Page 10 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
------------------------------ 2-DIMENSIONAL ARRAY -----------------------------Theory:
In the square-matrix A which has N rows and N columns, the element Aij is called the saddle
element if it is the smallest value of row i and it is also the largest value of column j.
28. Write a program to enter square-matrix A that has M rows and N columns and then finds all
saddle elements of matrix A.
Example 1: Please enter value for the matrix A:
Example 2:
Example 3:
Example 4:
Example 5:
Võ Hồng Khanh
Please enter number of rows: 5
Please enter number of columns: -3
The number of columns must greater than 0
Please enter value for the matrix A:
Please enter number of rows: -2
The number of rows must greater than 0
Please enter value for the matrix A:
Please enter number of rows: 3
Please enter number of columns: 3
Please enter value for a[1][1]: 1
Please enter value for a[1][2]: 6
Please enter value for a[1][3]: 1
Please enter value for a[2][1]: 1
Please enter value for a[2][2]: 8
Please enter value for a[2][3]: -9
Please enter value for a[3][1]: 0
Please enter value for a[3][2]: 2
Please enter value for a[3][3]: 1
All saddle elements of the matrix is found at
[1][1], [1][3]
Please enter value for the matrix A:
Please enter number of rows: 3
Please enter number of columns: 3
Please enter value for a[1][1]: 4
Please enter value for a[1][2]: 6
Please enter value for a[1][3]: 1
Please enter value for a[2][1]: 7
Please enter value for a[2][2]: 0
Please enter value for a[2][3]: 2
Please enter value for a[3][1]: 9
Please enter value for a[3][2]: 5
Please enter value for a[3][3]: -7
No saddle element is found in this matrix
Please enter value for the matrix A:
Please enter number of rows: 3
Please enter number of columns: 2
Please enter value for a[1][1]: 4
Please enter value for a[1][2]: 8
Please enter value for a[2][1]: 1
Please enter value for a[2][2]: 5
Please enter value for a[3][1]: 3
Please enter value for a[3][2]: 9
All saddle elements of the matrix is found at
[2][2]
khanhvh@fe.edu.vn
Page 11 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
29. Write a program to enter matrix A that has M rows and N columns then perform the
following tasks:
1. Counts the number of 0 in the matrix.
2. Finds the maximum value in the matrix.
3. Finds the smallest value in the matrix.
Example 1: Please enter value for the matrix A:
Example 2:
Example 3:
Võ Hồng Khanh
Please enter number of rows: 5
Please enter number of columns: -3
The number of columns must greater than 0
Please enter value for the matrix A:
Please enter number of rows: -2
The number of rows must greater than 0
Please enter value for the matrix A:
Please enter number of rows: 7
Please enter number of columns: 5
Please enter value for a[1][1]: -1
Please enter value for a[1][2]: 2
Please enter value for a[1][3]: 1
Please enter value for a[1][4]: 1
Please enter value for a[1][5]: 1
Please enter value for a[2][1]: 3
Please enter value for a[2][2]: 4
Please enter value for a[2][3]: 1
Please enter value for a[2][4]: -1
Please enter value for a[2][5]: 1
Please enter value for a[3][1]: 4
Please enter value for a[3][2]: 5
Please enter value for a[3][3]: 5
Please enter value for a[3][4]: 0
Please enter value for a[3][5]: 5
Please enter value for a[4][1]: 6
Please enter value for a[4][2]: 7
Please enter value for a[4][3]: 0
Please enter value for a[4][4]: 5
Please enter value for a[4][5]: 0
Please enter value for a[5][1]: 8
Please enter value for a[5][2]: -9
Please enter value for a[5][3]: 5
Please enter value for a[5][4]: 0
Please enter value for a[5][5]: 5
Please enter value for a[6][1]: 4
Please enter value for a[6][2]: 1
Please enter value for a[6][3]: 7
Please enter value for a[6][4]: 8
Please enter value for a[6][5]: 2
Please enter value for a[7][1]: 0
Please enter value for a[7][2]: -3
Please enter value for a[7][3]: 6
Please enter value for a[7][4]: -7
Please enter value for a[7][5]: 3
The number of the 0 in the matrix is 4
The minimum value in the matrix is -9
The maximum value in the matrix is 8
khanhvh@fe.edu.vn
Page 12 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
30. In mathematics, matrix addition is the operation of adding two matrices by adding the
corresponding entries together. Two matrices must have an equal number of rows and
columns to be added.
The matrix product Cm×n = Am×n + Bm×n is defined to be the matrix:
=
+
Such that
=

=
11
21
1

⋮
+
12
22
2

⋯
⋱
⋯
1
2
⋮
+
1≤ ≤
11
21
1
⋮
12
22
2
,1≤≤
⋯
⋱
⋯
1
2
⋮
=
11
21
1
⋮
12
22
2
⋯
⋱
⋯
1
2
⋮
For example, the result of A3×2 + B3×2 is C3×2:
Your mission is creating a program that allows user to enter two 2-dimensional matrices
and then compute the addition of the two given matrices. This program must check input
data include number of rows and number of columns of these matrices.
Example 1: Please enter value for the first matrix A:
Example 2:
Example 3:
Võ Hồng Khanh
Please enter number of rows: 5
Please enter number of columns: -3
The number of columns must greater than
Please enter value for the first matrix
Please enter number of rows: -2
The number of rows must greater than 0
Please enter value for the first matrix
Please enter number of rows: 2
Please enter number of columns: 2
Please enter value for a[1][1]: 10
Please enter value for a[1][2]: 0
Please enter value for a[2][1]: -4
Please enter value for a[2][2]: 5
-------------------Please enter value for the first matrix
Please enter number of rows: 2
Please enter number of columns: 2
Please enter value for B[1][1]: -6
Please enter value for B[1][2]: 3
Please enter value for B[2][1]: 1
Please enter value for B[2][2]: -7
The matrix C = A + B is
4 3
-3 -2
khanhvh@fe.edu.vn
0
A:
A:
B:
Page 13 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
31. In mathematics, matrix multiplication or matrix product is a binary operation that produces
a matrix from two matrices with entries in a field.
The matrix product Cn×p = An×m*Bm×p is defined to be the matrix:
=
∗
Such that
=

=
11
21
1
1
⋮
1
12
22
2
⋯
⋱
⋯
+…+
1
2
⋮
∗

=
11
21
For example, the result of A4×2 * B2×3 is C4×3:
1
⋮
=1
⋯
12
22
2

⋱
⋯
1
2
⋮
=
11
21
1
⋮
⋯
12
22
2
1≤ ≤ ,1≤≤
⋱
⋯
1
2
⋮
Your mission is creating a program that allows user to enter two 2-dimensional matrices
and then compute the multiplication of the two given matrices. This program must check
input data include number of rows and number of columns of these matrices.
Example 1: Please enter value for the first matrix A:
Example 2:
Example 3:
Võ Hồng Khanh
Please enter number of rows: 5
Please enter number of columns: -3
The number of columns must greater than 0
Please enter value for the first matrix A:
Please enter number of rows: -2
The number of rows must greater than 0
Please enter value for the first matrix A:
Please enter number of rows: 2
Please enter number of columns: 2
Please enter value for a[1][1]: 4
Please enter value for a[1][2]: 2
Please enter value for a[2][1]: 9
Please enter value for a[2][2]: 8
-------------------Please enter value for the first matrix B:
Please enter number of rows: 4
Please enter number of columns: 4
Can’t calculate A*B because of invalid number of rows of matrix B!
khanhvh@fe.edu.vn
Page 14 / 16
PRF192 – Programming Fundamentals Using C
Example 4:
Contiguous Storage
Please enter value for the first matrix A:
Please enter number of rows: 2
Please enter number of columns: 2
Please enter value for A[1][1]: 4
Please enter value for A[1][2]: 2
Please enter value for A[2][1]: 9
Please enter value for A[2][2]: 8
-------------------Please enter value for the first matrix B:
Please enter number of rows: 2
Please enter number of columns: 3
Please enter value for B[1][1]: -2
Please enter value for B[1][2]: 4
Please enter value for B[1][3]: 0
Please enter value for B[2][1]: 3
Please enter value for B[2][2]: 6
Please enter value for B[2][3]: 1
The matrix C = A * B is
-2 28 2
6 84 8
32. Write a program that displays a 2-dimensional matrix A which size is NxN and then fills it
with integers from 1 to N2 in a spiral format.
Example 1: Please enter value for the matrix A:
Example 2:
Please enter the size of matrix: -2
The size of matrix must greater than 0
Please enter value for the matrix A:
Please enter the size of matrix: 5
The spiral matrix which size is 5x5 is:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
33. Write a program that displays a 2-dimensional matrix A which size is NxN and then fills it
with integers from 1 to N2 in a reverse spiral format.
Example 1: Please enter value for the matrix A:
Example 2:
Võ Hồng Khanh
Please enter the size of matrix: -2
The size of matrix must greater than 0
Please enter value for the matrix A:
Please enter the size of matrix: 5
The reverse spiral matrix which size is 5x5 is:
25 10 11 12 13
24 9 2 3 14
23 8 1 4 15
22 7 6 5 16
21 20 19 18 17
khanhvh@fe.edu.vn
Page 15 / 16
PRF192 – Programming Fundamentals Using C
Contiguous Storage
34. In mathematics, a magic square is a NxN square grid (where N is the number of cells on
each side) filled with distinct positive integers in the range 1, 2, ..., N2 such that each cell
contains a different integer and the sum of the integers in each row, column and diagonal is
equal. Your mission is write a program that allows user to enter a positive integer N ≥ 2 and
display a magic square which size is NxN.
Example 1: Please enter the size of square: -2
Example 2:
Example 3:
Example 4:
The size of square must greater than 0
Please enter the size of square: 3
The magic square which size is 3x3 is:
2 7 6
9 5 1
4 3 8
Please enter the size of square: 4
The magic square which size is 4x4 is:
1 14 15 4
12 7 6 9
8 11 10 5
13 2 3 16
Please enter the size of square: 5
The magic square which size is 5x5 is:
3 16 9 22 15
20 8 21 14 2
7 25 13 1 19
24 12 5 18 6
11 4 17 10 23
35. LeO decided to invest in renting land to grow vegetables. The land is a rectangle M x N
square plot of land (1  M, N  100). But only a few days later, a terrible flood took place
making some plots of flooded and the land turned into some ponds.
LeO decided to switch to fish farming and wanted to know how many ponds there
were. Please help LeO.
Note: Ponds are composed of some flooded plots with the same peak. It is easy to see
that a plot of land can have up to 4 cells which connected with it’s edges.
Your mission is creating a program that allows user to enter matrix A that has M rows
and N columns. Each cell Aij contains one of two values:
1. Character . describes the plot of land that is not flooded.
2. Character W describes the plot of land that is flooded.
The program will count and display number of ponds of the land.
Example 1: Please enter the size of land: 4 6
Example 2:
Võ Hồng Khanh
Please enter the 1st rows of the land:
Please enter the 2nd rows of the land:
Please enter the 3rd rows of the land:
Please enter the 4th rows of the land:
There is 5 ponds on the land
Please enter the size of land: 4 8
Please enter the 1st rows of the land:
Please enter the 2nd rows of the land:
Please enter the 3rd rows of the land:
Please enter the 4th rows of the land:
There is 5 ponds on the land
khanhvh@fe.edu.vn
W.W.WW.W
W.WWW..W
...W...W
WW...W..
W.W.WWWW.W
W.WWW..W..
.W.....WWW
WWWW.WW..W
Page 16 / 16
Download