Skip to content

Commit

Permalink
feat: projects 1 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ADAMU MUHAMMAD MUHAMMAD committed Jan 18, 2024
1 parent f3ba2c6 commit d37710b
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 192 deletions.
191 changes: 110 additions & 81 deletions docs/C/ass1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,113 @@ sidebar_position: 3

# project 2

## Algorithm

- step 1 Prompt User for Number of Courses:
Display a prompt asking the user to enter the number of courses.
Read the user input and store it in the variable numCourses.

- step 2 Validate Number of Courses:

Check if numCourses is less than or equal to 0.

If true, display an error message and exit the program with an error code.

- step 3 Initialize Variables:
Initialize totalCredits and totalGradePoints to 0.0.

- step 4 Loop for Each Course:
Use a for loop to iterate from 1 to numCourses.

- step 5 Inside the loop:

Display a prompt for course details (course number).
Read and store user input for course credits in the variable credits.
Read and store user input for course grade in the variable grade.

- step 6 Validate Grade:
Check if grade is outside the valid range of 0 to 4 (on a 4.0 scale).
If true, display an error message and exit the program with an error code.

- step 7 Update Total Credits and Grade Points:
Add credits to totalCredits.
Add the product of credits and grade to totalGradePoints.

- step 8 Calculate GPA:
Calculate the GPA by dividing totalGradePoints by totalCredits. Store the result in the variable gpa.

- step 9 Display GPA:
Display the calculated GPA with two decimal places.

- step 10 Exit Successfully:
Return 0 to indicate successful program execution.

## Program Flow:

- User Interaction:
User is prompted to enter the number of courses.
User enters the number of courses.

- Validation:
The program checks if the entered number of courses is valid (greater than 0).
If not, an error message is displayed, and the program exits with an error code.

- Initialization:
Variables totalCredits and totalGradePoints are initialized to 0.0.

- Loop for Each Course:
The program enters a loop to gather information for each course.

- User Interaction for Each Course:
User is prompted to enter credits for the current course.
User enters credits for the current course.
User is prompted to enter the grade for the current course.
User enters the grade for the current course.

- Grade Validation:
The program checks if the entered grade is within the valid range (0 to 4).

If not, an error message is displayed, and the program exits with an error code.

- Update Total Credits and Grade Points:
The program updates the cumulative total credits and grade points for all courses.

- Calculate GPA:
The program calculates the GPA based on the accumulated total credits and grade points.

- Display GPA:
The program displays the calculated GPA.

- Exit Successfully:
The program exits with a return code of 0, indicating successful execution.

This program allows a user to input information for multiple courses and calculates the GPA based on the entered data.
# Algorithm

1. Get User Input:
- Display a prompt asking the user to enter the number of courses.
- Read the user input and store it in the variable numCourses.

2. Validate Number of Courses:

- Check if numCourses is less than or equal to 0.
- If true, display an error message and exit the program with an error code.

3. Initialize Variables:
- Initialize totalCredits and totalGradePoints to 0.0.
- Loop for Each Course:
- Use a for loop to iterate from 1 to numCourses.
- Inside the loop:
- Display a prompt for course details (course number).
- Read and store user input for course credits in the variable credits.
- Read and store user input for course grade in the variable grade.

4. Validate Grade:

- Check if grade is outside the valid range of 0 to 100.
- If true, display an error message and exit the program with an error code.

5. Calculate Grade Points:
- Call the calculateGradePoints function, passing the entered grade.
- Store the result in the variable gradePoints.

6. Update Total Credits and Grade Points:
- Add credits to totalCredits.
- Add the product of credits and gradePoints to totalGradePoints.

7. Calculate GPA:
- Calculate the GPA by dividing totalGradePoints by totalCredits. Store the result in the variable gpa.

8. Display GPA:
- Display the calculated GPA with two decimal places.

10. Exit Successfully:
- Return 0 to indicate successful program execution.

```C
#include <stdio.h>

double calculateGradePoints(double grade) {
if (grade >= 70) {
return 5.0;
} else if (grade >= 60) {
return 4.0;
} else if (grade >= 50) {
return 3.0;
} else if (grade >= 45) {
return 2.0;
} else if (grade >= 40) {
return 1.0;
} else {
return 0.0;
}
}

int main() {
int numCourses;
printf("Enter the number of courses: ");
scanf("%d", &numCourses);

// Ensure a valid number of courses is entered
if (numCourses <= 0) {
printf("Error: Please enter a valid number of courses.\n");
return 1; // Exit with an error code
}

double totalCredits = 0.0;
double totalGradePoints = 0.0;

for (int i = 1; i <= numCourses; ++i) {
double credits, grade;
printf("\nEnter details for Course %d:\n", i);

// Get user input for course credits
printf("Enter credits for Course %d: ", i);
scanf("%lf", &credits);

// Get user input for course grade
printf("Enter grade for Course %d: ", i);
scanf("%lf", &grade);

// Validate the grade entered
if (grade < 0 || grade > 100) {
printf("Error: Please enter a valid grade between 0 and 100.\n");
return 1; // Exit with an error code
}

// Calculate grade points based on the provided scale
double gradePoints = calculateGradePoints(grade);

// Update total credits and grade points
totalCredits += credits;
totalGradePoints += credits * gradePoints;
}

// Calculate GPA
double gpa = totalGradePoints / totalCredits;

// Display the GPA
printf("\nYour GPA is: %.2lf\n", gpa);

return 0; // Exit successfully
}
```
169 changes: 58 additions & 111 deletions docs/C/ass2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,131 +6,78 @@ sidebar_position: 4

## Algorithm

- Get User Input:
Display a menu for temperature conversion options.
Prompt the user to choose between Celsius to Fahrenheit (1) or Fahrenheit to Celsius (2).
1. Start

2. Input choice
a. Display "Choose conversion:"
b. Display "1. Celsius to Fahrenheit"
c. Display "2. Fahrenheit to Celsius"
d. Display "Enter choice (1 or 2): "
e. Read choice from the user

3. Input temperature
a. Display "Enter temperature: "
b. Read temperature from the user

4. Switch on choice
- Case 1:
a. Display "X Celsius is equal to Y Fahrenheit" where X is the entered temperature and Y is the result of celsiusToFahrenheit function with the entered temperature
b. Break
- Case 2:
a. Display "X Fahrenheit is equal to Y Celsius" where X is the entered temperature and Y is the result of fahrenheitToCelsius function with the entered temperature
b. Break
- Default:
a. Display "Error: Invalid choice. Please enter 1 or 2."
b. Return 1 (Exit with an error code)
c. Break
5. End

Read the user's choice and store it in the variable choice.
Prompt the user to enter the temperature for conversion.
Read the user input for temperature and store it in the variable temperature.

- Perform Temperature Conversion:
Use a switch statement to check the user's choice and perform the corresponding temperature conversion.

If the choice is 1, call the celsiusToFahrenheit function and display the result.

If the choice is 2, call the fahrenheitToCelsius function and display the result.

If the choice is neither 1 nor 2, display an error message and exit the program with an error code.

- Display Result:
Display the result of the temperature conversion with two decimal places.

- Exit Successfully:
Return 0 to indicate successful program execution.

## Program Flow:

- User Interaction:
User is presented with a menu for temperature conversion options.

User is prompted to choose between Celsius to Fahrenheit (1) or Fahrenheit to Celsius (2).

User enters the choice.
User is prompted to enter the temperature for conversion.
User enters the temperature.

- Temperature Conversion:
The program uses a switch statement to identify the user's choice.

If the choice is 1, the program calls the celsiusToFahrenheit function, passing the entered temperature, and displays the result.

If the choice is 2, the program calls the fahrenheitToCelsius function, passing the entered temperature, and displays the result.

If the choice is neither 1 nor 2, an error message is displayed, and the program exits with an error code.

- Display Result:
The program displays the result of the temperature conversion with two decimal places.

- Exit Successfully:
The program exits with a return code of 0, indicating successful execution.


import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs>
<TabItem value="version1" label="version 1">

```c
function helloWorld() {
console.log('Hello, world!');
}
```

</TabItem>
<TabItem value="version2" label="version 2">

```c
#include <stdio.h>

int main() {
double num1, num2, result;
char operator;

// Get user input
printf("Enter first number: ");
scanf("%lf", &num1);

printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator); // Note the space before %c to consume any whitespace characters

printf("Enter second number: ");
scanf("%lf", &num2);
//function prototype
double celsiusToFahrenheit(double celsius);
double fahrenheitToCelsius(double fahrenheit);

// Perform arithmetic operations
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
int main() {
int choice;
double temperature;

// Get user's choice
printf("Choose conversion:\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter choice (1 or 2): ");
scanf("%d", &choice);

// Get temperature from user
printf("Enter temperature: ");
scanf("%lf", &temperature);

// Perform conversion based on user's choice
switch (choice) {
case 1:
printf("%.2lf Celsius is equal to %.2lf Fahrenheit\n", temperature, celsiusToFahrenheit(temperature));
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero is not allowed.\n");
return 1; // Exit with an error code
}
case 2:
printf("%.2lf Fahrenheit is equal to %.2lf Celsius\n", temperature, fahrenheitToCelsius(temperature));
break;
default:
printf("Error: Invalid operator.\n");
printf("Error: Invalid choice. Please enter 1 or 2.\n");
return 1; // Exit with an error code
}

// Display the result
printf("Result: %.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);

return 0; // Exit successfully
}


```

</TabItem>
<TabItem value="version3" label="version 3">

```checks
class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, World");
}
// Function to convert Celsius to Fahrenheit
double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
```

</TabItem>
</Tabs>
// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
```

0 comments on commit d37710b

Please sign in to comment.