Programming Lab 3 Report
Programming Lab 3 Report
Introduction to C programming
ENGG1410
October 6, 2024
In this part, we wrote a program that solves a quadratic equation in the form of the equation
2
𝑎𝑥 + 𝑏𝑥 + 𝑐 = 0. The program asks the user to enter values for a, b, and c, it checks the
discriminant to understand whether the roots are real and distinct, real and equal or imaginary.
Based on that, it either calculates the roots or tells the user that the roots are imaginary. The main
issue I had with this part was understanding how to utilize the math in order to get the desired
For this section, the program takes x and y coordinates from the user and determines whether the
point is on an axis, in a quadrant (if so which quadrant) or at the origin. The coordinates are then
rounded to two decimal places before figuring out the point’s location. Figuring out what to use
to tackle this problem and calculating the dot product of the vectors and the magnitudes in order
to apply the cosine rule correctly was one issue which required some time and effort in planning.
Debugging:
In this part, we were given a program that simulates a dice rolling game and we were required to
debug the program. The code had multiple issues such as assignment and logic errors and
For this last section, we worked on debugging a program that calculates a student’s final grade
based on quiz, assignment and exam scores. There were a few problems for this one mainly with
the char variables and weight calculations. There was some logical errors but the overall
difficulty I found with this was that it was time consuming and you have to pay close attention to
each part.
Assumptions and constraints
Assumptions:
● For part 1 - assuming the user will enter valid coefficients a, b, and c, with a not being 0
● Part 2 - assume the user will input valid floating point numbers for the coordinates.
● Part 3 - The user will have to understand the game rules and follow the instructions
correctly.
● Part 4 - We assume the user will enter valid integer marks for all the forms of assessment.
Constraints:
● For part 1 - The program only calculates real roots and will not handle complex numbers.
● Part 3 - The program needs to seed the random number generator to make sure different
dice rolls.
● Part 4 - The program only accepts marks within the specified ranges for each of the
categories.
and c using printf and scanf to take in the float values.We also implemented an if statement to
make sure that a is not zero since it would invalidate the quadratic equation, if they did it would
print a error message. Then , we calculated the discriminant using the provided equation to
determine the type of roots. Depending on the value of D, we used conditional statements to
calculate and also display the roots. If D > 0, we computed two distinct real roots, if D = 0 we
found one real root but if D < 0 then we showed the roots are imaginary.
(The program required math.h library ) For this program, we asked the user to input the x and y
coordinates, rounding them to the nearest hundredth for precision using the round function. After
we implemented checks using conditional statements to determine if the point was at origin, on
an axis, or in one of the four quadrants. Doing this involved evaluating the rounded values and
In the dice roll simulation, we began by seeding the random number generator with srand() and
corrected the syntax for generating random numbers by changing the == to = for assignment. We
calculated the sum of two dice rolls and used condition statements to evaluate the outcome which
is winning, losing or to continue the game. After making the changes the program displayed the
In order to calculate the final score and letter grade, we asked the user for all of the three forms
of assessment marks, and then corrected errors such as the messing address of operators which
were in the scanf and also using the correct syntax for the character assignment. We calculated
the weighted average and applied a bonus, utilizing arithmetic operations and conditional
statements to determine the final score and the specified letter grade. After debugging the
program displayed the results that we desired. I found that going line by line like I'm writing the
code from scratch makes debugging less time consuming and aids me in finding the right
solutions.
In this lab, I successfully used various new C concepts for all the sections including solving
quadratic equations, determining the position of points in cartesian coordinates, debugging the
dice roll game, and calculating students grades. I felt each part helped add on to my
As said previously at the start, I struggled with the math operations and how to utilize them
efficiently in code but after this lab I believe that I received enough practice for me to be
confident in knowing how to use it when given a specific problem. I learned the main importance
of handling different scenarios, such as checking the discriminant for quadratic equations and
solving skills and made me come up with more efficient methods to save time while thoroughly
checking for errors in the program. Overall, this lab was beneficial in making me understand
how to structure and execute different algorithms effectively. I see some areas I can improve on
and with practicing these concepts through the lab and at home I feel I can be thorough in
Code:
Part 1:
#include <stdio.h>
#include <math.h>
int main(){
if(a==0){
return 1;
discriminant = (b*b)-(4*a*c);
if(discriminant>0){
root1=(-b + sqrt(discriminant))/2*a;
root2=(-b - sqrt(discriminant))/2*a;
}else if(discriminant==0){
root1=(-b / 2*a);
}else{
return 0;
Part 2:
#include <stdio.h>
#include <math.h>
int main() {
double x, y;
else if (y == 0.00) {
printf("(%.2f, %.2f) is on the x axis.\n", x, y);
}
else if (x == 0.00) {
printf("(%.2f, %.2f) is on the y axis.\n", x, y);
}
else {
if (x > 0 && y > 0) {
printf("(%.2f, %.2f) is in quadrant I.\n", x, y);
} else if (x < 0 && y > 0) {
printf("(%.2f, %.2f) is in quadrant II.\n", x, y);
} else if (x < 0 && y < 0) {
printf("(%.2f, %.2f) is in quadrant III.\n", x, y);
} else if (x > 0 && y < 0) {
printf("(%.2f, %.2f) is in quadrant IV.\n", x, y);
}
}
return 0;
}
Part 3:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int dice1, dice2, sum;
char play_again;
srand(time(0)); // Should have time
dice1 = (rand() % 6)+1; // should have +1 to roll from 1 to 6 and should have 1 = for assignment
dice2 = (rand() % 6)+1; // same thing here
sum = dice1 + dice2;
printf("You rolled a %d and a %d.\n", dice1, dice2);
printf("The sum is %d.\n", sum);
if (sum == 7 || sum == 11) {
printf("You win!\n");
} else if (sum == 2 || sum == 3 || sum == 12) { //sum = 12 should be == 12 conditional operator
printf("You lose!\n");
} else {
printf("No win or loss, play again.\n", play_again); //variable play_again should be declared here
}
return 0;
}
Part 4:
#include <math.h>
#include <stdio.h>
int main() {
int quiz1, quiz2, assignment, exam;
double weighted_average, final_score, bonus;
char grade;
weighted_average =
((((quiz1 + quiz2) / 2.0) / 0.20) * 0.2) + ((assignment / 0.5) * 0.3) + (exam * 0.5);