Mid C Sample
Mid C Sample
Q2
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.
4. The main Function:
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
Sample Output:
Q2
Sample Output:
Q3
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:
Another exam 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:
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
{1, 2, 3, 4}
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.
Use the symbolic constant (#define) to define constant ARRAYSIZE with value
of 9.
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
Array elements: 15 20 10 4 5 2 9 10 1
Random number: 1
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
abcdefghijklmnopqrstuvwxyz
Part 3 [2 marks]
For example
E+6wu&~<pWe$67]qd
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
abcdefjioAvvbnmae
The function will return the longest substring of vowel either small or capital
letter:
abcdefjioAvvbnmae
Part 5 [2 marks]
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]
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)
Sample output