MOD2A Tutorial 4
Question 1
Write a C program to accept three strings from the user. Sort the strings into two sets based
on length from shortest to longest and alphabetically from starting alphabet of ‘a’ to ‘z’. The
C program must adhere to the following design requirements.
1. Prompt the user to enter three strings not exceeding 20 characters.
a. Read in the three strings on one line.
b. Store strings in (a) above in a two-dimensional array.
2. Use a nested WHILE statement to sort the three strings by length from shortest to
longest.
a. Use a FOR loop to print sorted strings by shortest to longest length .
b. Set a variable called retval to a 0 if all three strings entered are already sorted
by shortest to longest length.
c. Set a variable called retval to a 1 if first two strings entered need to be sorted
by shortest to longest length.
d. Set a variable called retval to a 2 if first, second and third strings entered need
to be sorted by shortest to longest length.
3. Use a nested FOR statement to sort the three strings alphabetically from starting
alphabet of ‘a’ to ‘z’.
a. Use a WHILE loop to print alphabetically sorted strings.
4. Use a switch statement in the main function to check the value of variable called
retval and display the following:
a. Test condition 0: Print “Sorted already”.
b. Test condition 1: Print “One sort”.
c. Test condition 2: Print “Two sort”.
d. Test condition default: Print “Unknown sort”.
Important: Read all of the above requirements carefully and thoroughly before writing your
code.
Question 2
Write a C program to subtract two square matrices of whole numbers. Assume each matrix
size is 3 rows x 3 columns. Define the matrix size as a constant. You must not read in the
matrices values in one line. You must prompt the user for each individual value. The program
must adhere to the following design requirements.
1. Use a nested DO-WHILE loop to prompt the user to enter the first matrix values.
2. Use a nested WHILE loop to prompt the user to enter the second matrix values.
3. Use a nested FOR loop to calculate the difference between first matrix – second matrix.
4. Use a single FOR loop to print difference matrix = matrix one – matrix 2.
Question 3
Write a C program to accept N numbers and arrange them in an ascending order. The C
program must adhere to the following design requirements.
1. Use a DO-WHILE loop to prompt the user to enter the number of elements in an array of
whole numbers, minimum size 4, maximum size 10.
2. Read in the array size – N.
3. Test if the array size falls in the range 4 to 10. If the array size is not in this range then
print an error message remain in the loop and re-prompt the user to enter array size.
4. If the array size is in the range 4 to 10, then use a WHILE loop to input the whole
numbers and store in the array up to the size of the array value entered by the user.
5. Use a nested FOR loop to sort the entered numbers in ascending order.
6. Print out the ordered list of numbers separated by tabs using a WHILE loop.
Question 4
Write a C program to add two square matrices of whole numbers. Assume each matrix size is
3 rows x 3 columns. Define the matrix size as a constant. You must read in the matrices
values in one line. The program must adhere to the following design requirements.
1. Prompt the user to enter the first matrix values.
2. Prompt the user to enter the second matrix values.
3. Use a nested WHILE loop to calculate the sum between first matrix + second matrix.
4. Use a single WHILE loop to print difference matrix = matrix one + matrix 2.
Question 5
Write a C program to sort a list of 5 whole numbers “-2, 45, 0, 11, -9” in ascending order
from lowest to highest value. Use the bubble sort algorithm to design and write your
program. The program must adhere to the following design requirements.
1. Store the above values in an array.
2. Use IF-ELSE to compare two adjacent elements.
3. Use a WHILE loop construct for the outer loop to access each array element.
4. Use a FOR loop construct for the inner loop to compare array elements.
5. Call a function called printSorted to print the sorted array using a DO-WHILE loop
construct. The function is passed the array and the array size as input parameters and
returns nothing as an output.
Hint: Reminder: Bubble sort is a sorting algorithm that compares two adjacent elements and
swaps them until they are in the intended order.
Working of Bubble Sort
Suppose we are trying to sort the elements in ascending order.
1. Starting from the first index, compare the first and the second elements.
2. If the first element is greater than the second element, they are swapped.
3. Now, compare the second and the third elements. Swap them if they are not in order.
4. Repeat the above process goes on until the last element.
5. The same process goes on for the remaining iterations.
6. After each iteration, the largest element among the unsorted elements is placed at the end.
7. Put the largest element at the end
8. In each iteration, the comparison takes place up to the last unsorted element.
9. The array is sorted when all the unsorted elements are placed at their correct positions.
First Pass: Second Pass: Place the Third Pass: Place the
The largest element is second largest element at remaining two elements at
placed in its correct position, correct position their correct positions.
i.e., the end of the array.
Question 6
Write a C program to converts a given whole positive number into its binary representation.
The program must adhere to the following design requirements:
1. Use a DO-WHILE loop to obtain a whole positive number from user.
2. Prompt the user to enter a whole number.
3. Read the input number
4. Use the IF-ELSE statement to determine if a positive number.
5. Print an error message if test condition fails and repeat loop.
6. If test conditions pass then call a function called decimalToBinary with the whole
number as input parameter.
7. In decimalToBinary function perform the following functionality:
a. Test if number is equal to zero and print the number if true and exit function.
b. If number is not zero, define an array to store the binary equivalent of whole
number.
c. Use a WHILE loop construct decimalToBinary to convert decimal to binary
d. Use a FOR loop construct print binary representation. HINT: Note the order in
which the binary representation must be printed.