Programming Lab2 Report
Programming Lab2 Report
Introduction to C programming
ENGG1410
In this part of the lab, we were given a problem to convert a decimal integer (Base 10) that the
user inputs, with a maximum value of 15, and then convert it into binary (Base 2). The program
has to display the binary number given as a four digit output which means that if the binary
equivalent has fewer digits, it should still be shown using four bits making sure that all the inputs
For this section we were required to calculate the angle 0 between two vectors based on the
coordinates the user provides in a 2D plane. The problem needed us to apply the cosine rule to
determine that angle, using the dot product and magnitudes of the vectors obtained from the
coordinates.
We are given the problem to convert a user provided total number of minutes and convert it into
hours and remaining minutes. The program also needs to be able to round the remaining minutes
to the nearest quarter hour which makes sure that the output gives the correct hour and minute
values according to the rounding rules that we were to follow in the task.
Part 4 - Summing the Digits of an Integer:
In this part we had to calculate the sum of the digits of an integer which is inputted by the user,
and the integer must be within the range of 0 to 99999. The program is required to take each digit
of the number and compute the total while also handling input errors appropriately.
For the debugging task we had to identify and fix errors in the c program which is supposed to
encrypt a 4 digit combination using a specific coding plan. The original program contains
multiple logical and syntax errors that prevent it from correctly processing the input combination
Assumptions:
● The user will input an integer between 0 and 15. Negative numbers or inputs that are not
● The user will enter a valid float point number for the coordinates. - part 2
● The user will give a non-negative integer for the total minutes. - part 3
● The user will enter a integer between the boundaries of 0 to 99999 - part 4
● The user will give a 4 digit integer combination, not including negative numbers and
Constraints:
● The program will only show the binary format using four bits which means it will only
● Inputs outside the range will make the program give an error message. - part 4
The program is supposed to ask the user to enter a decimal integer from 0 - 15 and convert it to
binary by using the bitwise operations (&, >>). We also use a loop to extract each bit and then it
To solve this we ask the user to input the coordinates using the printf and then get it from the
user and read the coordinates using scanf, we also calculated the dot product using basic
arithmetic. For the magnitudes of those vectors we calculated it using sqrt. To get the cosine of
the angle 0 we divided the dot product by the product of the magnitudes, and then we got the
angle in radians using acos. Then we had to convert it to degrees so we used MpI, and we
We used scanf to read the total minutes from the user. Then we wrote hours = total_minutes/60;
minutes = total_minutes%60; this calculates hours by integer division of total minutes by 60 and
then determining the remaining minutes using the modulus operator. We used if else statements
to round the remaining minutes according to the specific rules that were given in the lab. We then
We asked the user for an integer input and initialized a sum variable to zero. We then used a loop
with modulus to extract each digit of the number and divided it to reduce the number. Each digit
was added to the sum until all digits were processed and then it gave the final total sum of the
digits.
Part 5 - Debugging
● We corrected the whole process for the digit extraction by using modulus and division.
Now d4 extracts the last digit(4th) and the enc comb removes the last digit and the whole
process is repeated. In simpler terms, we use the %10 to get each digit from the right side
of the number and /10 to remove the last digit which makes sure the digits are taken in
the correct order. The main issue was dividing encComb by 1000 and 100 (it took the
digits improperly)
For the lab we focused on converting decimal numbers to binary, calculated angles using cosine
rule, built time conversion programs with rounding, summing digits of an integer and also we
gained more experience on debugging a program. For this we were forced to use mathematical
concepts in our code and multiple math operators and other math related code located in the math
library such as M_PI which was a new piece of code I learned in that lab. I again found the
debugging effective as it improved my skill on finding the problem and figuring out solutions to
address the issues within the code. I had some challenges with calculations but I figured it out
through testing. Another part that helped was the trigonometric program since it refreshed me on
how to build mathematical based programs. We learned about if-else statements and this lab was
the time we got to apply the concept and see how it works which I found interesting and helped
with more understanding about the statement and when to use it. Overall these sets of programs
helped me gain more confidence in debugging and planning how to tackle the problem.
Part 1:
#include <stdio.h>
int main() {
int decimal, binary[4];
printf("Enter an integer (0-15): ");
scanf("%d", &decimal);
if(decimal < 0 || decimal > 15) {
printf("Error: Please enter a number between 0 and 15.\n");
return 1;
}
for (int i = 3; i >= 0; i--) {
binary[i] = decimal % 2;
decimal = decimal / 2;
}
printf("Binary representation: ");
for (int i = 0; i < 4; i++) {
printf("%d", binary[i]);
}
printf("\n");
return 0;
}
Part 2:
#include <stdio.h>
#include <math.h>
#define _USE_MATH_DEFINES
int main(){
double x1, y1, x2, y2;
double dot_product, magnitude_A, magnitude_B, cos_theta, theta_radian, theta_degree;
dot_product = x1 * x2 + y1 * y2;
theta_radian = acos(cos_theta);
theta_degree = theta_radian * (180.0 / M_PI);
printf("The angle theta between the two points is: %.3f radians or %.2f degree \n",
theta_radian, theta_degree);
Part 3:
#include <stdio.h>
int main(){
int total_minutes, hours, minutes, rounded_minutes;
hours = total_minutes/60;
minutes = total_minutes%60;
}
Part 4:
#include <stdio.h>
int main() {
int number, sum = 0;
return 0;
}
Part 5:
# include <stdio.h>
int main ( void ) {
int encComb ;
printf ( " Enter an encrypted 4 - digit combination : " );
scanf ( "%d", &encComb );
d4 = encComb % 10;
encComb = encComb / 10;
d3 = encComb % 10;
encComb = encComb / 10;
d2 = encComb % 10;
encComb = encComb / 10;
d1 = encComb ;
printf ( "\n The real combination is: %d%d%d%d \n ", d4, 9 - d3 , 9 - d2 , d1);
return 0;
}