0% found this document useful (0 votes)
6 views8 pages

Lab 3

The document contains two C programs: one for calculating training heart rates based on gender, age, resting heart rate, and fitness level, and another for calculating a final course grade based on quiz scores, midterm, and final exam grades. The first program outputs the training heart rate after user input, while the second program computes and displays the final grade after processing quiz and exam scores. Both programs include error handling for invalid inputs.

Uploaded by

aliasgharjivraj
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)
6 views8 pages

Lab 3

The document contains two C programs: one for calculating training heart rates based on gender, age, resting heart rate, and fitness level, and another for calculating a final course grade based on quiz scores, midterm, and final exam grades. The first program outputs the training heart rate after user input, while the second program computes and displays the final grade after processing quiz and exam scores. Both programs include error handling for invalid inputs.

Uploaded by

aliasgharjivraj
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/ 8

Problem 1:

Algorithm:

Code:
#include <stdio.h>
#include <math.h>

double calculateMaxHR(char gender, int age) {


double maxHR;

if (gender == 'M') {
maxHR = 203.7 / (1 + exp(0.033 * (age - 104.3)));
} else { // gender is 'F'
maxHR = 190.2 / (1 + exp(0.0453 * (age - 107.5)));
}

return maxHR;
}

int calculateTHR(double maxHR, int restingHR, double fitnessLevel) {


int THRfinal;
THRfinal = (int)((maxHR - restingHR) * fitnessLevel + restingHR);
return THRfinal;
}

int main() {
char gender, FIT;
int age, restingHR, trainingHR;
double maxHR, fitnessLevel;

printf("Enter your gender [Male (M), Female (F)]: ");


scanf(" %c", &gender);

if (gender == 'M' || gender == 'F') {


printf("Enter your age: ");
scanf("%d", &age);

printf("Enter your resting heart rate: ");


scanf("%d", &restingHR);

printf("Enter your fitness level [Low (L), Medium (M), High (H)]: ");
scanf(" %c", &FIT); // Reuse the gender variable for fitness level

switch (FIT) {
case 'L':
fitnessLevel = 0.55;
break;
case 'M':
fitnessLevel = 0.65;
break;
case 'H':
fitnessLevel = 0.85;
break;
default:
printf("Error: Invalid fitness level\n");
fitnessLevel = 0;
break;
}

maxHR = calculateMaxHR(gender, age);


trainingHR = calculateTHR(maxHR, restingHR, fitnessLevel);

printf("Your training heart rate is %d\n", trainingHR);


} else {
printf("Error: Invalid gender\n");
return 1; // Exit the program due to invalid gender
}

return (0);
}

Output:
Enter your gender [Male (M), Female (F)]: M
Enter your age: 19
Enter your resting heart rate: 64
Enter your fitness level [Low (L), Medium (M), High (H)]: M
Your training heart rate is 147
Enter your gender [Male (M), Female (F)]: F
Enter your age: 20
Enter your resting heart rate: 63
Enter your fitness level [Low (L), Medium (M), High (H)]: H
Your training heart rate is 168

Enter your gender [Male (M), Female (F)]: F


Enter your age: 63
Enter your resting heart rate: 82
Enter your fitness level [Low (L), Medium (M), High (H)]: L
Your training heart rate is 129

Problem 3:
Algorithm:

Code:
#include <stdio.h>

double calculateCourseGrade(double quizzes[10], int midterm, int finalExam) {


// Declare variables
double smallestQuiz = quizzes[0], quizTotal = 0.0, quizAverage, courseGrade;
// Find the lowest quiz grade
for (int i = 1; i < 10; i++) {
if (quizzes[i] < smallestQuiz) {
smallestQuiz = quizzes[i];
}
}

// Calculate the average of the quizzes without the lowest one


for (int i = 0; i < 10; i++) {
if (quizzes[i] != smallestQuiz) {
quizTotal += quizzes[i];
}
}
quizAverage = quizTotal / 9.0;

// Calculate the final course grade


if (midterm >= finalExam) {
courseGrade = 0.25 * quizAverage + 0.35 * midterm + 0.40 * finalExam;
} else {
courseGrade = 0.25 * quizAverage + 0.25 * midterm + 0.50 * finalExam;
}

return courseGrade;
}

int main() {
// Input variables
double quizzes[10];
int midterm, finalExam;
double finalGrade;

// Input for quizzes


printf("Enter the grades for 10 quizzes (0 to 10):\n");
for (int i = 0; i < 10; i++) {
printf("Grade %d: ", i+1);
scanf(" %lf", &quizzes[i]); // Use %lf for double
}

// Input for midterm and final exam


printf("Enter the grades for the midterm (0 to 100): ");
scanf(" %d", &midterm);

printf("Enter the grade for the final exam (0 to 100): ");


scanf(" %d", &finalExam);

// Calculate and output the final grade


finalGrade = calculateCourseGrade(quizzes, midterm, finalExam);
printf("The final grade is: %.2f\n", finalGrade);

return 0;
}

Output:
Enter the grades for 10 quizzes (0 to 10):
Grade 1: 9.5
Grade 2: 6
Grade 3: 4
Grade 4: 10
Grade 5: 7.8
Grade 6: 3.4
Grade 7: 9
Grade 8: 5.6
Grade 9: 9
Grade 10: 10
Enter the grades for the midterm (0 to 100): 73
Enter the grade for the final exam (0 to 100): 84
The final grade is: 62.22
Enter the grades for 10 quizzes (0 to 10):
Grade 1: 9.5
Grade 2: 8.4
Grade 3: 9
Grade 4: 10
Grade 5: 7.8
Grade 6: 10
Grade 7: 9
Grade 8: 9.6
Grade 9: 9
Grade 10: 10
Enter the grades for the midterm (0 to 100): 89
Enter the grade for the final exam (0 to 100): 81
The final grade is: 65.90
Enter the grades for 10 quizzes (0 to 10):
Grade 1: 8.5
Grade 2: 8.5
Grade 3: 9
Grade 4: 8.5
Grade 5: 7.5
Grade 6: 7
Grade 7: 9
Grade 8: 9.5
Grade 9: 10
Grade 10: 10
Enter the grades for the midterm (0 to 100): 80
Enter the grade for the final exam (0 to 100): 70
The final grade is: 58.24

You might also like