AGH Computer Science C Programming Laboratory 6
AGH Computer Science C Programming Laboratory 6
2024
1. [3points] Write a C program that will read the elements of the array from the keyboard and
calculate their sum.
a) Declare an integer array of size 10. In your program, use the preprocessor directive #define to
create a SIZE constant of value 10.
b) Use a for loop and the scanf function to load the values into the array.
c) Use a for loop to calculate the sum of all the elements in the array.
d) Print all the values from the array in reverse order.
e) Print the sum of the elements of the array.
Test data:
2. [5points] Write a C program to generate 10 pseudo-random numbers and place them in an array.
Use the rand function from the <stdlib.h> header to generate the pseudo-random numbers. Print the
index and the value of the minimum and maximum elements in the array.
a) Declare an integer array of size 10. In your program, use the preprocessor directive #define to
create a SIZE constant of value 10.
b) Use a for loop and the rand function to fill the array with pseudo-random numbers between 10
and 90.
c) Assume that the first element in the array is both a maximum and a minimum.
d) Iterate through the array to find the maximum and minimum elements in the array.
e) Print the index and value of the minimum and maximum elements in the array.
Test data:
1
3. [7points] Write a C program that implements selection sort.
First iteration:
// Find the minimum element in arr[0...4]
// and place it at arr[0]
11 25 12 22 64
Second iteration:
// Find the minimum element in arr[1...4]
// and place it at arr[1]
11 12 25 22 64
Third iteration:
// Find the minimum element in arr[2...4]
// and place it at arr[2]
11 12 22 25 64
Fourth iteration:
// Find the minimum element in arr[3...4]
// and place it at arr[3]
11 12 22 25 64 Test data:
d) Two nested loops are needed for sorting. For the outer loop to
insert elements into the array in ascending order, the inner loop
must find the minimum value in the part of the array that has not
yet been sorted. Once the inner loop finds the minimum, the
outer loop must place that element at the beginning of the part of
the array that has not yet been sorted.
2
4. [5points] Let's see how the sort time depends on the size of the array. Use the clock function to
measure how long the sort takes to run.
clock_t clock (void); - returns the processor time consumed by the program.
The value returned is expressed in clock ticks, which are units of time of a constant but system-
specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).
Test data:
Next time:
Laboratory 07 – Functions, Arrays.
To prepare for the next class, read the lecture or book chapter on functions and arrays.
Check how sample programs from the lecture or book work. Check if you can modify them
in any way you want. Write some example programs that use functions and arrays.