Course: Programming Fundamentals (C language)
LAB 8 – Arrays
Main content:
1.
2.
3.
4.
Get used to array declarations
Get used to element accesses in arrays
Get used to array passing in function calls
Check coding styles
Practice:
1. Get used to array declarations
1.1. Write a C program to generate and print the following arrays of integer numbers where N is
given by a user from the keyboard:
1.1.1. N first even numbers greater than 0.
1.1.2. The 50 random numbers less than N.
1.1.3. The first prime numbers less than N.
1.2. Given a C program as follows. Describe the property of each array on line 7 and then
describe the property of each parameter passing in the function call isMember() on line 11.
1.3. Write a C program to calculate the percentage of the tourists whose fees are greater than
13 and also calculate the percentage of the tourists who start their first visiting location at
L1. It is required that the information of the tourists given below needs organizing properly.
1
Given the information of the tourists as follows:
Full
name
John
Smith
Peter
Brown
Alice
Depp
March
Carter
Tony
Stark
Conan
May
Passport
Nationality
Fee
Starting date
Ending
date
First location
Name X
Last location
Name X
Y
Y
B1203457
English
15.3
13/2/2017
18/2/2017
L1
2.4
5
L2
5.2
8.3 SPECIAL
A3910385
English
13.2
23/12/2016
03/1/2017
L1
2.4
5
L3
4.8
7.5 NORMAL
B2384021
English
13
23/12/2016
03/1/2017
L2
5.2
8.3
L4
6.2
8.9 NORMAL
A3830291
English
10.5
28/12/2016
04/1/2017
L3
4.8
7.5
L5
6.5
9.2 SALE
D3870294
American
20
28/12/2016
04/1/2017
L1
2.4
5
L5
6.5
9.2 SPECIAL
D4911356
American
20
28/12/2016
04/1/2017
L1
2.4
5
L5
6.5
9.2 SPECIAL
2. Get used to element accesses in arrays
2.1. Complete the following code to obtain a C program that can insert a new element iNew
into the b array with no impact on the ascending order from the left to the right of the
array.
int b[10] = {-2, 0, 2, 4, 9, 10, 13, 19};
int n = 8, iNew = 11, i, j;
for (i=0; b[i]
if (i==n) (1).……………;
else {
for (j=n++; j>i; j--) (2)……………..;
b[i] = iNew;
}
2.2. Write a C program to receive a string from a user and return the number of sentences in
the input that has been keyed. In your program, write and use a function to process a string
and return the number of sentences for the main function. A sentence is counted if it ends
with the following punctuation marks: . (period, full stop), ? (question), ! (exclamation). For
simplicity, an empty sentence is also counted.
Input 1: Today is not rainy. It is a nice day for camping. Do you want to join us?!
Output 1: There are 4 sentences.
2
Tour type
Input 2: Ho Chi Minh City
Output 2: There are 0 sentences.
2.3. Write a C program using functions to capitalize each starting letter of each word in a given
sentence.
Input 1: Today is Monday, isn’t?
Output 1: Today Is Monday, Isn’t?
3. Get used to array passing in function calls
3.1. Write a C program using non-recursive functions and another version using recursive
functions to check if a 1-dimensional array of N integer numbers is symmetric where N is a
natural number greater than 0.
Example: {1, 5, -2, 0, -2, 5, 1} and {5, -3, -3, 5} are symmetric arrays while {2, 0, 1} and {9, -8,
-8, 4} are not.
3.2. Write a C program using non-recursive functions and another version using recursive
functions to check if a number is a member of a 1-dimensional array of N numbers where N
is a natural number greater than 0.
3.3. Write a C program using non-recursive functions and another version using recursive
functions to check if a 1-dimensional array of N floating point numbers is in ascending order
where N is a natural number greater than 0.
3.4. Write a C program to sort the elements of a 1-dimensional array of N floating point numbers
where N is a natural number greater than 0.
3.5. Write a C program using functions to rotate an object by 90 degree in a clockwise direction.
It is supposed that each object is captured in an NxN gray-scale image. N is a dimension size
of an input image given by a user. A gray-scale image is represented as a grid whose cells are
in [0, 255].
Input: an example for a 4x4 gray-scale image (N=4)
1
11
8
15
3
4
8
6
5
7
9
12
2
6
13
10
3
Output:
15
6
12
10
8
8
9
13
11
4
7
6
1
3
5
2
3.6. Write a C program using functions to check if a matrix mA is contained in another matrix
mB, i.e. mA is a sub-matrix of mB. If mA is contained in mB, then record and print all the first
positions of mA in mB. Otherwise, record and print 0.
Example 1: Matrix mA is given as follows:
2
-1
0
3
Matrix mB is given as follows:
2
-1
5
4
7
0
3
9
5
8
7
4
0
2
-1
8
0
12
0
3
The result is printed below:
2
0, 0
2, 3
Example 2: Another matrix mA is given as follows while mB is kept unchanged:
2
-1
0
0
3
0
The result is printed below:
0
4
3. Check coding styles
For each C program that you have written, check coding styles with the following points:
- How have you used blank lines?
- How have you used tabs for alignment?
- How have you used comments in your source code files?
- Above all, how about your naming conventions? What have you named in your source code
files?
5