Level 2 Answers
Level 2 Answers
1) Code 1 answer
Yes, it will compile and execute. The answer is:
66
Garbage value
44
Garbage value
printf("\n %d ", array[SIZE]);: This prints the value at index 4 of the array, which is 66.
printf("\n %d ", array[SIZE+1]);: This is accessing beyond the array size, resulting in
undefined behavior. The output is unpredictable, and it may give you garbage values or crash.
printf("\n %d ", array[SIZE-2]);: This prints the value at index 2 of the array, which is 44.
printf("\n %d ", array[-2]);: This is attempting to access an element at a negative index, which
results in undefined behavior.
L1.4) Code 4 answer
The code will not compile, as “String” is not a registered data type.
Should instead be:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("%s", str);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
char *word[3];
scanf("%s", word[i]);
}
return 0;
}
Moving on, both the for loops will print each element of the array numbers. Since m is a pointer
variable holding the base address of the first element of the number array, and marks[i] =
&numbers[i] is now a proper array of pointers that holds the location of each element of the
numbers array in contiguous memory locations.
Output:
C/C++
int main() {
int marks[] = {99, 10, 50, 98, 67};
int n = sizeof(marks) / sizeof(marks[0]);
printf("Students with marks less than highest and second highest: ");
for (int i = 0; i < n; i++) {
if (marks[i] < secondHighest && marks[i] < highest) {
printf("Student %d: %d\n", i + 1, marks[i]);
}
return 0;
}
C/C++
int main() {
int marks[] = {99, 10, 50, 98, 67};
int n = sizeof(marks) / sizeof(marks[0]);
printf("Students with marks less than highest and second highest : ");
return 0;
}
C/C++
#include <stdio.h>
int main() {
int marks[] = {99, 10, 50, 98, 67};
int n = sizeof(marks) / sizeof(marks[0]);
findStudents(marks, n);
return 0;
}
Linear search with a single iteration is likely the most efficient and straightforward approach.
Linear search has a linear time complexity and is well-suited for tasks that involve scanning
through the array once without the need for sorting.
L3.2)
C/C++
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 10;
int arr[n];
printf("Enter elements (Each digit one by one: ): ");
for (int i = 0; i < n; i++) {
scanf("%1d", &arr[i]);
}
int k;
printf("Enter key: ");
scanf("%d", &k);
if (k < 0 || k >= n) {
printf("-1");
exit(0);
}
leftRotate(arr, n, k);
for (int i = 0; i < n; i++) {
printf("%d", arr[i]);
}
return 0;
}
Second method:
C/C++
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int n = 10;
int arr[n];
printf("Enter elements (Each digit one by one: ): ");
for (int i = 0; i < n; i++) {
scanf("%1d", &arr[i]);
}
int k;
printf("Enter key: ");
scanf("%d", &k);
if (k < 0 || k >= n) {
printf("-1");
exit(0);
}
leftRotate(arr, n, k);
for (int i = 0; i < n; i++) {
printf("%d", arr[i]);
}
return 0;
}
Method 2 is considered more efficient than than 1 because it doesn’t use additional space for
creating a temp array.
L3.3)
a)
i) Method 1:
C/C++
#include <stdio.h>
int main() {
float temp[10] = {34.56, 78.89, 67.12, 34.56, 12.23, 34.56, 67.12, 34.56, 12.23,
34.56};
int freq[10] = {0};
return 0;
}
C/C++
#include <stdio.h>
int main() {
float temp[10] = {34.56, 78.89, 67.12, 34.56, 12.23, 34.56, 67.12, 34.56, 12.23,
34.56};
return 0;
}