Algorithm Classification C8Fa59
1. Problem: Verify the algorithm that classifies numbers as "Single Digit", "Double Digit", or "Big" for the inputs (5, 9, 47, 99, 100, 200).
2. The original algorithm is:
IF Number < 9
PRINT "Single Digit"
ELSE IF Number < 99
PRINT "Double Digit"
ELSE
PRINT "Big"
3. Check each input:
- For 5: 5 < 9, so "Single Digit" (correct)
- For 9: 9 < 9 is false, 9 < 99 is true, so "Double Digit" (correct)
- For 47: 47 < 9 false, 47 < 99 true, "Double Digit" (correct)
- For 99: 99 < 9 false, 99 < 99 false, so "Big" (incorrect, 99 is two digits)
- For 100: 100 < 9 false, 100 < 99 false, "Big" (correct)
- For 200: similarly "Big" (correct)
4. The problem is with the boundary conditions: 9 and 99.
5. Corrected algorithm:
IF Number <= 9
PRINT "Single Digit"
ELSE IF Number <= 99
PRINT "Double Digit"
ELSE
PRINT "Big"
6. This includes 9 and 99 correctly.
7. Implementation in C:
```c
#include
int main() {
int Number;
printf("Enter a number: ");
scanf("%d", &Number);
if (Number <= 9) {
printf("Single Digit\n");
} else if (Number <= 99) {
printf("Double Digit\n");
} else {
printf("Big\n");
}
return 0;
}
```
---
1. Problem: Algorithm to accept only positive integers up to 100.
2. Original algorithm:
IF (0 < Number) AND (Number <= 100)
PRINT "ACCEPT"
ELSE
PRINT "REJECT"
3. Values where it fails:
- Number = 0: 0 is not > 0, so rejected (correct if zero is not accepted)
- Negative numbers: rejected (correct)
- Numbers > 100: rejected (correct)
- Non-integer inputs: algorithm does not handle non-integers
4. Improvement:
- Explicitly check if Number is an integer.
- Clarify if zero is accepted or not.
5. Improved algorithm:
IF Number is integer AND (1 <= Number <= 100)
PRINT "ACCEPT"
ELSE
PRINT "REJECT"
---
1. What is a function?
A function is a block of code designed to perform a specific task, which can be called and reused in a program.
2. Need for functions:
Functions help organize code, avoid repetition, improve readability, and facilitate debugging and maintenance.
3. Actual parameter vs Formal argument:
- Actual parameter: the real value or variable passed to a function when called.
- Formal argument: the variable defined in the function declaration to receive the actual parameter.
4. Function prototype:
A declaration of a function that specifies its name, return type, and parameters without the body, informing the compiler about the function.
5. Call by value vs Call by reference:
- Call by value: passes a copy of the argument; changes inside the function do not affect the original.
- Call by reference: passes the address; changes inside the function affect the original variable.
6. Steps in writing a function:
- Define the function prototype.
- Write the function definition.
- Call the function from main or other functions.
7. Types of operators in C:
- Arithmetic (+, -, *, /, %)
- Relational (==, !=, >, <, >=, <=)
- Logical (&&, ||, !)
- Bitwise (&, |, ^, ~, <<, >>)
- Assignment (=, +=, -=, etc.)
- Increment/Decrement (++ , --)
- Conditional (?:)
- Others (sizeof, comma operator)
8. Do-while loop to print from 10 to 1:
```c
#include
int main() {
int i = 10;
do {
printf("%d\n", i);
i--;
} while (i >= 1);
return 0;
}
```