[go: up one dir, main page]

0% found this document useful (0 votes)
35 views11 pages

Midterm Sample Questions

Nice

Uploaded by

lai20230092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views11 pages

Midterm Sample Questions

Nice

Uploaded by

lai20230092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Sample Questions

Q1
Using Codeblocks IDE, create a new Codeblocks project named “ExamQ1”. Use this project
to write and run a C program that performs the following:
1. Use the symbolic constant (#define) to specify 1D array ARRAY_SIZE of 15.
2. Write your name and ID as a comment (1 mark).
3. Create a function called "Randfill" that fills the array with random integers ranging
from -20 to 20. The function should also count the number of positive and negative integers
in the array and print the positive and negative counts in the same function. Hint: void
Randfill(int arr[], int size)
4. Create a function called "searchArray" that searches for a key entered by the user in the
array. If the key is found, the function should return the index of the first occurrence of the
key. If the key is not found, the function should return -1. Hint: int searchArray (int arr[], int
size, int key)
5. Create a function called "printrArray" to print the array in reverse order. The function
should also calculate and return the sum of the array elements. Hint: int printrArray(int arr[],
int size)
6. In the main function:
a. Define an array of integers with size 15 (You must use the ARRAY_SIZE constant for the
size of the array, and the array name should be your first name).
b. Call the Randfill function and print the number of positive and negative integers in the
array.
d. Prompt the user to enter a key and call the searchArray function. Print the index of the first
occurrence of the key if it is found, otherwise, print a message indicating that the key is not in
the array.
e. Call the printrArray function and print the sum of the array elements.
f. Print a message indicating the maximum and minimum values in the array.
Note: Please stick to the names given for functions, array size, and the name of the array.
The output should match the sample below.
Q2
Write a C program that performs the following tasks:

1. Add your name, your University ID number, and your section number as a comment
at the beginning of this program. (1 mark)
2. Define a function called isDivisiblebyTwelve that takes a number as input
and returns if the number is divisible by the number twelve or not.
3. Define a function called sumNum that takes a number as input and returns the sum
of digits that are divisible by the number twelve.

The main Function:

1. Prompt the user to enter a number between 1000 and 9999.


2. Continuously ask the user to input the number until a valid number within the range is
entered.
3. Pass the user-input number to the sumNum function.
4. Print the result of the sumNum in the main function.
Sample 2
Q1
Using Codeblocks IDE, write a C program that performs the following:

1. Use the symbolic constant (#define) to specify 1D array size ARRAY_SIZE of 100.
(1 Mark)
2. Create a function that takes a string. The function computes and return the hash value
as follows: (3 Marks)

hash=(hash×31) +newcharacter, till the end of the string, and the initial hash value should be
0, NOT 1.

Note: Ensure the removal of the Null value from the hash calculations

3. In the main function, do the following:

1. Define char array of size ARRAY_SIZE.(.5 mark)


2. Use the array above to enter three strings.(1 .5 mark)
3. Send each string to the function to calculate the hash value (.5)
4. If the hashed value is in the range (1000-10000) inclusive, the program
displays a message, and asks the user to enter the string again. Otherwise, print
the name with its hash value. (1 mark)
5. Add a comment that shows your name with its hash value. (1 Mark)
6. Print the name with the middle hash value of the three strings (1.5 Marks)

Sample Output:
Q2
1. Using Codeblocks IDE, write a C program that performs the following:
1. Use the symbolic constant (#define) to specify 1D array ARRAY_SIZE of 10.
(.5 Mark)
2. Create a function that takes an array, the size of the array, and a number. The
function should convert the number to binary and store it in the array. If the
array size cannot accommodate all the binary digits, the function should return
0. Otherwise, save the binary representation of the number in the array in the
correct format.(4 Marks)
3. In the main function do the following:
1. Define array of size ARRAY_SIZE.(.5 mark)
2. Ask the user to enter a number.(.5 mark)
3. Send the array and the number to the function and check if you can
store it in the array or not.(.5 mark)
4. Print the binary array if you can convert the number, or error message.
(1 mark)
Hint: To convert an integer to binary, repeatedly divide the
number by 2 and record the remainders (remainders represent the
binary bits)

Sample Output:
Q3
Using Codeblocks IDE, write a C program that performs the following:

1. Create a function that takes two numbers, representing the lower and upper bounds.
The function should prompt the user to enter a number repeatedly until it falls within
the specified bounds. Finally, the function should return the number entered. (2
Marks)
2. Create a function that takes a number. The function should return whether the number
is prime or not. (2 Marks) (A prime number is a number greater than 1 that has
no positive divisors other than 1 and itself.)
3. Create a function that takes a number. The function should shift the digits of the
number to the right by one digit. For example, if the number is 1234, after calling the
function, the generated and returned number should be 4123. (2.5 Marks)
4. Create a function that takes a number. The function separates the number digits into
pairs( suppose that the number contains even digits), checks if each pair is a prime
number, and finally returns the count of prime numbers found among these pairs. For
example, if the input number is 115613, the function will separate the number into
three pairs: 11, 56, and 13. It will then check each number for primality and return the
count of prime numbers, which in this case is 2 (11 and 13).(4 Marks)
5. In the main function, do the following:
1. Use the function created in point 1 to input a number in the range of (999999,
100000). (.5 Mark)
2. Send the number generated to the function in point 4 to check how many
prime number pairs are present in the number.(1 Mark)
3. Finally, shift the number generated in point 1 according to the count of prime
numbers calculated in the number generated.(2 Marks)

Sample Output:
Sample 3
Q1
Part 1 [2 marks]

Write a function that takes an integer array, and a size of the array, the function fills array
elements as follows:

1. Index 0 input by user


2. Index 1 input by user
3. Index 2 filled randomly in the range from index 0 to index 1(inclusive),
4. Repeat the process from point 1 to point 3 to fill the rest of the elements of the array
5. No need to swap the elements or check which is maximum, or minimum of index 0
and 1.

For example

Suppose we have 6 elements of an array, the user input index 0 for example 1 and 5 in index
1, then index 2 will be randomly from 1 to 5, then the user will input index 3 for example 5
and 10 in index 4, then index 5 will be randomly from 5 to 10.

Part 2 [2 marks]

Write a function that prints the elements of the array in reverse order, all in one line with one
space between elements.

For example

if the array sent to the function has the following elements

{1, 2, 3, 4}

The function will print the elements of the array as following:

4321

Part 3 [2 marks]

Write a function that takes a number, then returns 1 if the number is prime, or 0 otherwise.

Note: Prime numbers are numbers greater than 1 that only have two factors, 1 and the number
itself. This means that a prime number is only divisible by 1 and itself. If you divide a prime
number by a number other than 1 and itself, you will get a non-zero remainder.

For example

Suppose we send 3 to the function, then it will return 1 as 3 is a prime number.


Part 4 [.5 marks]

Use the symbolic constant (#define) to define constant ARRAYSIZE with value of 9.

Part 5 [3.5 marks]

In the main function do the following:

1. Define 1D integer array of size ARRAYSIZE define in Part 4 (.5 mark)


2. Use the function defined in Part 1 to fill the elements of the array you define in point
1. (.5 mark)
3. Use the function defined in Part 2 tp print the elements of the array you define in
point 1.(.5 mark)
4. Generate random number from 0 to ARRAY_SIZE(exclusive the ARRAY_SIZE).
(.5 mark)
5. Print all the prime numbers from 1 to the number generated in point 4. (1.5 mark)

For example

Suppose array elements are {1,40, 7, 2, 10, 7}, and the random number is 2, then the output
will be as following:

2357

Sample output

Enter value for index 0: 1

Enter value for index 1: 10

Enter value for index 3: 2

Enter value for index 4: 5

Enter value for index 6: 10

Enter value for index 7: 20

Array elements: 15 20 10 4 5 2 9 10 1

Random number: 1

Prime numbers up to 10: 2 3 5 7


Q2

Part 1 [2 marks]

Write a function that fills an array with random ASCII characters. The function should take a
character array and the size of the array. Each character in the array should be a random
ASCII character in the range from 20 to 127 (inclusive).

Note: Each run of the program will produce a different set of random ASCII characters
within the specified range.

For example

if the array sent to the function has size of 10 the function will fill it randomly in the range
from 20 to 127

V@s;SxJ!p

Part 2 [2 marks]

Write a function that prints the elements of the array, all in one line without any space
between characters, with a tab space after printing ten characters.

For example

if the array sent to the function has the following elements

abcdefghijklmnopqrstuvwxyz

The function will print the elements of the array as following:

abcdefghij klmnopqrst uvwxyz

Part 3 [2 marks]

Write a function that arranges characters in an array in the following order: lowercase letters
first, followed by uppercase letters, and then any other characters. The function should take
only a character array.

For example

if the array sent to the function has the following elements

E+6wu&~<pWe$67]qd

The function will arrange the elements of the array as following:


Small letter followed by capital letter, finally all the other letters

wupeqdWE~<6&$67]+

Part 4 [2 marks]

Write a function that takes a character array as input, and returns the length of the longest
substring in character array that consists entirely of vowels (a, e, i, o, u, A, E, I, O, U).

if the array is empty or does not contain any vowels, the function should return 0.

For example

if the array sent to the function has the following elements

abcdefjioAvvbnmae

The function will return the longest substring of vowel either small or capital letter:

The vowel letter in the array

abcdefjioAvvbnmae

The longest one is 3 so the function will return 3

Part 5 [2 marks]

In the main function do the following:

1. Define a 1D array with size 50.


2. Call the function in part 1 to fill the elements of the array
3. Call the function in part 2 to print the elements of the array
4. Call the function in part 3 to arrange the elements of the array
5. Call the function in part 2 again to print array elements again after arrange them
6. Call the function to calculate the longest vowel substring in the array

Sample output
Q3
Part 1 [3 marks]

Create a function that takes a number; the function splits the digits, finds the maximum and
minimum digit, subtracts the minimum from the maximum and returns the result .

For example

If the function receives a number for example 321, the function splits the digits, finds the
maximum which is 3, finds the minimum which is 1, and subtracts the minimum from the
maximum 3 - 1 which is 2, which will be returned.

Part 2 [7 marks]

In the main function do the following:

1. Ask the user to enter a number within the range (100000 – 555555)(inclusive).
Validate the input; if the number is not within the valid range, keep asking the user to
input a valid number until a valid number is entered.. (2 mark)

For example

Ask the user to enter a number, suppose the user enters 10, the number is not valid, so ask the
user to enter a number again, suppose the user enters 100000000, again the number is not
valid, keep repeating the process until a valid number is entered, for example 123456.

2. Ask the user to enter a number that will determine how many times to repeat the
process will be shown in the sample output.(.5 mark)
3. Ask the user to input a number that will determine which part of the number to send
to function in part 1, so if the user enters 0 send the left half of the number on the
other hand if user enter 1 send the right half of the number (suppose that the user will
input only a valid number).(1 mark)

For example

Suppose the user enters 0 and the number is 123456, the left half which is 123 will be sent to
the function.

4. Call the function in part 1 by sending the half number according to the value entered
by the user in point 3 in our example 123.
5. Add the returned value of the function to the original number (123456+2 = 123458).
6. Print the new number 123458 . (1.5 mark)
7. Repeat the steps from point 3 to point 6 n times, where n is a value entered by the user
in point 2. (2 mark)
Note: You are NOT allowed to use <math.h>

Sample output

Enter a number within the range (100000 – 555555): 10

Enter a number within the range (100000 – 555555): 100000000

Enter a number within the range (100000 – 555555): 123456

Enter the number of times to repeat the process: 3

Original number: 123456

Enter 0 to select the left half or 1 to select the right half: 0

Iteration 1: 123456 + 2 = 123458

Enter 0 to select the left half or 1 to select the right half: 1

Iteration 2: 123458 + 4 = 123462

Enter 0 to select the left half or 1 to select the right half: 1

Iteration 3: 123462 + 4 = 123466

Final number: 123466

You might also like