0% found this document useful (0 votes)
62 views10 pages

Level 2 Answers

The document contains solutions to coding problems in C/C++. It provides multiple ways to solve each problem through different algorithms and approaches. For each coding problem, it explains the problem statement and provides 2-3 solutions with detailed code implementations.

Uploaded by

MinSugar1015
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)
62 views10 pages

Level 2 Answers

The document contains solutions to coding problems in C/C++. It provides multiple ways to solve each problem through different algorithms and approaches. For each coding problem, it explains the problem statement and provides 2-3 solutions with detailed code implementations.

Uploaded by

MinSugar1015
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/ 10

L1.

1) Code 1 answer
Yes, it will compile and execute. The answer is:

Message has 13 characters excluding '\0'


Message is Hello, world!

Enter numbers into array: 1


2
3
4
5

Entered numbers are: 1 2 3 4 5

L1.2) Code 2 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;
}

L2.1) Code 1 answer


The code will throw an error, because the size of the array marks[] is not mentioned during
declaration.

L2.2) Code 2 answer


No, the code will not compile because pointers in the array “word” are not pointing to allocated
memory for strings. Corrected version:

#include <stdio.h>
#include <stdlib.h>
int main() {
char *word[3];

for (int i = 0; i < 3; i++) {


printf("\nEnter a word: ");

word[i] = (char *)malloc(100 * sizeof(char));

scanf("%s", word[i]);
}

printf("\nFirst Word entered is %s\n", word[0]);

for (int i = 0; i < 3; i++) {


free(word[i]);
}

return 0;
}

L2.3) Code 3 answers


The lines:
Marks = &numbers;
Marks = numbers;
Are problematic. Marks is an array of pointers and those two lines are essentially trying to
assign an array address to an array of pointers. Without those lines, the lines:
M = numbers;
M = &numbers;
Does the same thing, and the name of an array returns the base address of the first element of
an array and using the & does the same. Since m is a pointer variable, the assignment is legal.

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:

The values from 'm' array is... 12 13 14 15

The values from 'marks' array is... 12 13 14 15

L2.4) Code 4 answers


This will return whatever the user inputs as the four marks. This is because of int *marks; As
opposed to the previous example L2.1 where we tried to access memory locations that do not
exist through the code int marks[]; (size wasn’t defined). Regardless, it is probably a good idea
to dynamically allocate memory in this case.
L3.1) Code 1 answers
Essentially, we can use different sorting algorithms to get through this task.
Let’s first start with the most basic way:

C/C++
int main() {
int marks[] = {99, 10, 50, 98, 67};
int n = sizeof(marks) / sizeof(marks[0]);

int highest = 0, second = 0;

for (int i = 0; i < n; i++) {


if (marks[i] > highest) {
second = highest;
highest = marks[i];
} else if (marks[i] > second && marks[i] < highest) {
secondHighest = marks[i];
}

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;
}

Second method is to use selection sort:

C/C++
int main() {
int marks[] = {99, 10, 50, 98, 67};
int n = sizeof(marks) / sizeof(marks[0]);

for (int i = 0; i < n - 1; i++) {


int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[min]) {
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}

printf("Students with marks less than highest and second highest : ");

for (int i = 2; i < n; i++) {


printf("Student %d: %d\n", i + 1, marks[i]);
}

return 0;
}

Third method: quick sort

C/C++
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = low - 1;

for (int j = low; j <= high - 1; j++) {


if (arr[j] >= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}

swap(&arr[i + 1], &arr[high]);


return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

void findStudents(int marks[], int n) {


quickSort(marks, 0, n - 1);

printf("Students with marks less than the second highest:\n");


for (int i = 2; i < n; i++) {
printf("Student %d: %d\n", i + 1, marks[i]);
}
}

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>

void leftRotate(int arr[], int n, int k) {


int temp[k];
for (int i = 0; i < k; i++) {
temp[i] = arr[i];
}
for (int i = k; i < n; i++) {
arr[i - k] = arr[i];
}
for (int i = 0; i < k; i++) {
arr[n - k + i] = temp[i];
}
}

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;
}

void leftRotate(int arr[], int n, int k) {


for (int i = 0; i < k; i++) {
for (int j = 0; j < n - 1; j++) {
swap(&arr[j], &arr[j + 1]);
}
}
}

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};

for (int i = 0; i < 10; ++i) {


for (int j = 0; j < 10; ++j) {
if (temp[i] == temp[j]) {
freq[i]++;
}
}
}

for (int i = 0; i < 10; ++i) {


printf("%.2f: %d\n", temp[i], freq[i]);
}

return 0;
}

ii) Method 2: same thing but using structures.

b) Method 2: finding max and min without sorting

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};

float max = temp[0], min = temp[0];


for (int i = 1; i < 10; ++i) {
if (temp[i] > max) {
max = temp[i];
} else if (temp[i] < min) {
min = temp[i];
}
}

// Print alternatively the highest and lowest temp


printf("%.2f\n", max);
printf("%.2f\n", min);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy