Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Programming Lab2 Report

Uploaded by

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

Programming Lab2 Report

Uploaded by

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

Lab Report 1

Introduction to C programming

School of Engineering: University of Guelph

ENGG1410

September 29, 2024

Lab #2 - Complex calculations and decision making, debugging c programs

Group #6 - Joel George, Sohum Deshpande


Problem Statements

Part 1 - Conversion to Base 2:

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

are processed correctly.

Part 2 - Trigonometry - using the cosine rule:

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.

Part 3 - Conversion of Minutes to Hours and Nearest Quarter Hour:

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.

Part 5 - Debugging C program:

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

and giving us the desired output.


Assumptions and constraints

Assumptions:

● The user will input an integer between 0 and 15. Negative numbers or inputs that are not

integers would not be considered. - part 1

● 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

non-digit values. - part 5

Constraints:

● The program will only show the binary format using four bits which means it will only

convert numbers from 0 to 15. - part 1

● The program needs the math.h library for calculations - part 2

● The rounding is limited to intervals of 0, 15, 30 and 45 minutes. - part 3

● Inputs outside the range will make the program give an error message. - part 4

● The code only handles four digit inputs. - part 5


Solutions to the problem

Part 1 - Conversion to Base 2

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

displays the outcome as a four bit binary number.

Part 2 - Trigonometry Using the Cosine Rule

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

displayed the output using printf.

Part 3 - Conversion program

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

used the printf to display the hours and rounded minutes.


Part 4 - Summing the digits of an integer

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)

Conclusion and self assessment

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;

printf("Enter point A as (x1, y1): ");


scanf("%lf,%lf", &x1, &y1);

printf("Enter point B as (x2, y2): ");


scanf("%lf,%lf", &x2, &y2);

dot_product = x1 * x2 + y1 * y2;

magnitude_A = sqrt(x1 * x1 + y1 * y1);


magnitude_B = sqrt(x2 * x2 + y2 * y2);

cos_theta = dot_product / (magnitude_A * magnitude_B);

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;

printf("Enter total minutes: \n");


scanf("%d", &total_minutes);

hours = total_minutes/60;
minutes = total_minutes%60;

if(minutes >= 0 && minutes <=7){


rounded_minutes = 0;
}else if(minutes >= 8 && minutes <=22){
rounded_minutes = 15;
}else if(minutes >= 23 && minutes <=37){
rounded_minutes = 30;
}else if(minutes >= 38 && minutes <=52){
rounded_minutes = 45;
}else if(minutes >=53 && minutes <=59){
hours++;
rounded_minutes = 0;
}

printf("That is equivalent to: \n");


printf("%d hours and %d minutes \n", hours, rounded_minutes);

}
Part 4:

#include <stdio.h>

int main() {
int number, sum = 0;

printf("Enter an integer (0 to 99999): ");


scanf("%d", &number);

if (number < 0 || number > 99999) {


printf("Error: Please enter a number between 0 and 99999.\n");
return 1;
}

while (number > 0) {


sum += number % 10;
number = number / 10;
}

printf("The sum of the digits is: %d\n", sum);

return 0;
}

Part 5:

# include <stdio.h>
int main ( void ) {
int encComb ;
printf ( " Enter an encrypted 4 - digit combination : " );
scanf ( "%d", &encComb );

int d4, d3, d2, d1;

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;
}

You might also like