CE150: Introduction to programming with C
Introduction to programming with C
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CE150: Introduction to programming with C
Assignment 1 (30% of total grade)
Submission date: Noon, Wednesday 22/11/2023 (Week 8)
Instructions:
You must submit a single .c file with your code for the following exercise. If you use OnlineGDB, simply
copy and paste the code to a text editor and save the file with the required extension (for example,
assignment1.c). The submitted code must compile without any errors or warnings reported, and the
program must run successfully. You must also include comments to explain parts of your code; there
is no need to explain every single line of code. You must not submit executable programs.
You will lose marks if you do not follow these instructions. In particular, you will lose marks if you do
not submit a .c file, if your code does not run successfully (even if it does not do exactly what the
exercise asks), or if you do not include sufficiently enough comments.
Exercise
Write a program that repeatedly presents to the user a menu of options. Once the user enters their
choice, it runs the corresponding function. The menu must include the following four options:
Option 1: Compute and print the sum of all integers that are simultaneously divisible by two given
integers up to a given upper bound.
Option 2: Compute and print the decimal representation of a binary number.
Option 3: Terminate the program.
(A) [40%] The main function.
It must repeatedly present the menu to the user using a loop. The choice of the user is an integer
(with possible valid values 1, 2 or 3) that is stored in a variable named option. Based on the value of
option, the program does the following:
• User enters 1: The program asks the user to enter three integers which are stored in variables
x, y and n. These numbers are then given as input to a function named sum_of_divisibles (see
(B) for details about how to implement this function). The output of the function is an integer
that is the sum of all the numbers up to n that are divisible by x and y. This output is printed
using an appropriate message.
• User enters 2: The program asks the user to enter a binary number which is stored in an
integer variable x. The digits of the number must be 0 or 1 so that it is binary. This number is
then given as input to a function named bin_to_dec (see (C) for details about how to
implement this function). The output of the function is an integer number that is the decimal
representation of the given binary number. This output is printed using an appropriate
message.
• User enters 3: The program terminates.
• If the user enters any other value, then the program prints a message of invalid option.
(B) [30%] The function sum_of_divisibles.
The function takes as input three integers x, y and n. The output of the function is an integer number
sum which is the sum of all the numbers up to n that are divisible by x and y. Using a loop, the function
goes through all possible integers up to and including n, checks if the current number is divisible by
x and y, and if this is indeed the case, adds the number to the running sum. Once all valid numbers
have been considered, the loop terminates, and function returns the sum.
Example: Calling the function as sum_of_divisibles(2, 3, 12) must return the number 18 which is the
sum of the numbers 6 and 12, which are the only numbers between 1 and 12 that are divisible by 2
and 3.
(C) [30%] The function bin_to_dec.
The function takes as input an integer x which stores a binary number (every digit is either 0 or 1).
The output of the function is an integer number y which is the decimal representation of the given
binary number. Using a loop, the function breaks the value stored in x into its digits. In each step of
the loop, the function computes the last digit (from the right) of x by computing the remainder of the
division of x with 10; this is stored in an integer variable digit. The function then checks if digit is 0 or
1. If it is not, the function terminates by returning the value -1 indicating that the given number is
not binary. If it is, it adds digit *2i to y, where i counts the current step of the loop with the first step
equal to 0. Then, it updates x to be equal to the division of x with 10, and goes to the next step of the
loop until x becomes 0 in which case the loop terminates.
Example: Calling the function as bin_to_dec(10111) must return the number 23. This is because
23 = 1*24 + 0*23 + 1*22 + 1*21 + 1*20 = 16 + 0 + 4 + 2 + 1