Answers or Algorithm Year2023
Answers or Algorithm Year2023
1. Push:
o Check if the stack is full.
o If not full, increment the top pointer.
o Add the element at the position pointed to by the top pointer.
2. Pop:
o Check if the stack is empty.
o If not empty, return the element at the position pointed to by the
top pointer.
o Decrement the top pointer.
If the value of a=20, b=3, what will be the value of the sum?
c
Copy code
#include <stdio.h>
int main() {
int a = 20;
int b = 3;
int sum = a + b;
printf("%d", sum);
return 0;
}
1. Syntax errors: Errors that occur when the code violates the syntax rules
of the programming language.
2. Runtime errors: Errors that occur during the execution of a program,
often due to illegal operations such as dividing by zero.
1. Start
2. Declare an array arr and integer n
3. Read the value of n
4. For i from 0 to n-1:
o Read arr[i]
5. For i from n-1 to 0:
o Print arr[i]
6. Stop
Question 11: Write a C program that will display odd numbers from 50 to
10.
c
Copy code
#include <stdio.h>
int main() {
int i;
for(i = 50; i >= 10; i--) {
if(i % 2 != 0) {
printf("%d ", i);
}
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
default: printf("Invalid day number");
}
return 0;
}
Question 13: Use sum() function to develop a simple C program that will
add two numbers.
c
Copy code
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d", sum(a, b));
return 0;
}
Question 14: Consider the following code snippet. What are the values of i
and n after the code is executed?
c
Copy code
int i = 10;
int n = i++ % 5;
After execution:
i is 11
n is 0
c
Copy code
int i = 10;
int n = ++i % 5;
After execution:
i is 11
n is 1
Marks Grades
Above 75 O
60-75 A
50-60 B
40-50 C
Less than 40 D
Algorithm:
1. Start
2. Declare integer marks
3. Read the value of marks
4. If marks > 75, print "Grade O"
5. Else if marks >= 60, print "Grade A"
6. Else if marks >= 50, print "Grade B"
7. Else if marks >= 40, print "Grade C"
8. Else, print "Grade D"
9. Stop
Question 17: Analyze the expression provided in the first column and
complete the second column of the table below.
Question 18: Analyze the following program and provide its output.
c
Copy code
#include <stdio.h>
int main() {
int i;
int arr[5] = {1, 5, 9};
for (i = 0; i < 5; i++){
printf("%d ", arr[i]);
}
return 0;
}
Output: 1 5 9 0 0
c
Copy code
void *realloc(void *ptr, size_t size);
Function overloading allows multiple functions with the same name but
different parameters to be defined. Example:
cpp
Copy code
#include <iostream>
using namespace std;
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(string s) {
cout << "Printing string: " << s << endl;
}
int main() {
print(10);
print(5.5);
print("Hello");
return 0;
}
1. Start
2. Declare array arr and integers low, high, mid, target
3. Initialize low = 0, high = n-1
4. While low <= high:
o Set mid = (low + high) / 2
o If arr[mid] == target, return mid
o Else if arr[mid] < target, set low = mid + 1
o Else, set high = mid - 1
5. If target not found, return -1
6. Stop
Question 21a
javascript
Copy code
let length = 30;
let width = 50;
let height = 5;
let volume = length * width * height;
console.log("Volume of the box is: " + volume);
Output:
yaml
Copy code
Volume of the box is: 7500
Question 21b