Lab Template. 2024
Lab Template. 2024
SANTHIRAM
ENGINEERING COLLEGE
KASMINI VIDYA
BALACHANDRUDU
24X51A05C8
[COMPUTER PROGRAMMING
LAB]
[COMPUTER PROGRAMMING LAB] 24X51A05C8
College Vision:
Mission
To provide Advanced Educational Programs and prepare students
to achieve success and take leading roles in their chosen fields of
specialization by arising a self-sustained University.
To establish postgraduate programs in the current and Advanced
Technologies. To establish an R&D Consultancy through developing
Industry Institute Interaction, building up exceptional infrastructure.
To propel every individual, realize and act for the technical
development of the society.
Department Vision
Department Mission
“Hello World” in C
Objective
In this challenge, we will learn some basic concepts of C that will
get you started with the language. You will need to use the same
syntax to read input and write output in many C challenges. As you
work through these problems, review the code stubs to learn about
reading from stdin and writing to stdout.
Task
This challenge requires you to print on a single line, and then print
the already provided input string to stdout. If you are not familiar
with C, you may want to read about the printf() command.
Example
Life is beautiful
Function Description
Complete the main() function below.
The main() function has the following input:
string s: a string
Prints
*two strings: * "Hello, World!" on one line and the input string on the
next line.
Input Format
Sample Output 0
Hello, World!
Welcome to C programming.
Program
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main()
{
char s[100];
scanf("%[^\n]%*c",
&s);
/* Enter your code here. Read input from STDIN. Print output to STDOUT
*/
return 0;
}
Output:
Compiler Message
Success
Input (stdin)
Download
Welcome to C programming.
Expected Output
Download
Hello, World!
Welcome to C programming.
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Objective
This challenge will help you to learn how to take a character, a
string and a sentence as input in C.
To take a single character as input, you can use scanf("%c",&ch
); and printf("%c", ch) writes a character specified by the
argument char to stdout
char ch;
scanf("%c", &ch);
printf("%c", ch);
This piece of code prints the character .
You can take a string as input in C using scanf(“%s”, s). But, it
accepts string only until it finds the first space.
In order to take a line as input, you can use scanf("%[^\n]%*c", s);
where is defined as char s[MAX_LEN] where is the maximum size of
. Here, [] is the scanset character. ^\n stands for taking input until
a newline isn't encountered. Then, with this %*c, it reads the
newline character and here, the used * indicates that this newline
character is discarded.
Note: The statement: scanf("%[^\n]%*c", s); will not work because
the last
statement will read a newline character, \n, from the previous line. This
can be handled in a variety of ways. One way is to use scanf("\
n"); before the last statement.
Task
[COMPUTER PROGRAMMING LAB] 24X51A05C8
You have to print the character, , in the first line. Then print in
next line. In the last line print the sentence, .
Input Format
First, take a character, as
input. Then take the string,
as input.
Lastly, take the sentence as
input. Constraints
Strings for and will have fewer than 100 characters, including the
newline. Output Format
Print three lines of output. The first line prints the
character, . The second line prints the string, .
The third line prints the
sentence, . Sample Input 0
C
Language
Welcome To C!!
Sample Output 0
C
Language
Welcome To
C!!
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main()
char ch;
char
s[24];
char
t[100];
scanf("%c", &ch);
scanf("%s", s);
getchar(); scanf("%
[^\n]%*c", t);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", t);
return 0;
output:
Compiler Message
Success
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Input (stdin)
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Download
Language
Welcome To C!!
Expected
Output
Download
Language
Welcome To C!!
Objective
The fundamental data types in c are int, float and char. Today,
we're discussing int and float data types.
The printf() function prints the given statement to the console. The
syntax is printf("format string",argument_list);. In the function, if we
are using an integer, character, string or float as argument, then in
the format string we have to write %d (integer), %c (character), %s
(string), %f (float) respectively.
The scanf() function reads the input data from the console. The
syntax is scanf("format string",argument_list);. For ex: The
scanf("%d",&number) statement reads integer number from the
console and stores the given value in variable .
Task
Your task is to take two numbers of int data type, two numbers of
float data type as input and output their sum:
Input Format
Constraints
integer
variables float
variables
Output Format
Sample
Input 10 4
4.0 2.0
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample
Output 14 6
6.0 2.0
Explanation
program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
int a,b;
float c,d;
scanf("%d %d",&a,&b);
scanf("%f %f",&c
a+b;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
float float_sum =
c+d; float
float_diff = c-d;
printf("%d %d\n",int_sum,int_diff);
printf("%0.1f %0.1f",
float_sum,float_diff); return 0;
output:
Success
Input
(stdin)
Download
10 4
4.0 2.0
Expected Output
Download
14 6
6.0 2.0
4. Functions in C
Objective
[COMPUTER PROGRAMMING LAB] 24X51A05C8
...
...
...
For example, a function to read four variables and return the sum of
them can be written as
d) { int sum = 0;
sum += a;
sum += b;
sum += c;
sum += d;
return
sum;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
a += b is equivalent to a = a + b;
Task
Note
Input Format
Sample
Input 3
5
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample
Output 6
program:
#include <stdio.h>
int max_of_four(int a, int b, int c, int
d) { int max = a;
if (b > max) {
max = b;
if (c > max) {
max = c;
if (d > max) {
max = d;
return max;
int main() {
int a, b, c, d;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
}
output:
Compiler Message
Success
Input (stdin)
Downloa
d3
Expected Output
[COMPUTER PROGRAMMING LAB] 24X51A05C8
5. Pointers In C
Objective
void increment(int
*v) { (*v)++;
int
main()
{ int a;
scanf("%d",
&a);
increment(&a);
[COMPUTER PROGRAMMING LAB] 24X51A05C8
printf("%d",
a); return 0;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Task
Input Format
Modify the two values in place and the code stub main() will print
their values.
Sample
Input 4
Sample
Output 9
Program:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <stdio.h>
void update(int *a,int *b) {
sum = *a+*b;
diff = abs(*a-
*b);
*a = sum;
*b = diff;
int
main()
{ int a,
b;
n%d", a, b);
return 0;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Output:
Compiler Message
Success
Input (stdin)
Downloa
d4
Expected Output
6. Conditional Statements in C
Objective
if and else are two of the most frequently used conditionals in C/C+
+, and they enable you to execute zero or one conditional
statement among many such dependent conditional statements.
We use them in the following ways:
statement1;
...
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
statement1;
...
else {
statement2;
...
...
...
[COMPUTER PROGRAMMING LAB] 24X51A05C8
....
else {
...
Task
9. Input Format
Constraints
Output Format
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample
Input 5
Sample
Output five
eight
44
Greater
than 9
Program:
#include <stdio.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int
main()
{ int a;
scanf("%d", &a);
{ printf("%s",
cases[a]);
} else {
return 0;
Output:
Compiler Message
Success
Input (stdin)
Expected Output
[COMPUTER PROGRAMMING LAB] 24X51A05C8
five
7. For Loop In C
Objective
In this challenge, you will learn the usage of the for loop, which is a
programming language statement which allows code to be
executed until a terminal condition is met. They can even repeat
forever if the terminal condition is never met.
<statement>
The following loop initializes to 0, tests that is less than 10, and
increments at every iteration. It will execute 10 times.
...
Task
[COMPUTER PROGRAMMING LAB] 24X51A05C8
print "odd".
Input Format
Constraints
Output Format
Note:
Sample
Input 8
11
Sample
Output eight
nin
eve
[COMPUTER PROGRAMMING LAB] 24X51A05C8
odd
Program:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int
main()
{ int a,
b;
printf("one\n");
} else if (i == 2) {
printf("two\n");
} else if (i == 3) {
printf("three\n");
} else if (i == 4) {
printf("four\n");
} else if (i
==
5) {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
printf("five\n");
[COMPUTER PROGRAMMING LAB] 24X51A05C8
}
else if (i == 6) {
printf("six\n");
} else if (i == 7) {
printf("seven\n");
} else if (i == 8) {
printf("eight\n");
} else if (i == 9) {
printf("nine\n");
} else if (i % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
Output:
Compiler Message
Success
Input (stdin)
Downloa
d8
11
Expected Output
Download
eight
[COMPUTER PROGRAMMING LAB] 24X51A05C8
nine
even
odd
Objective
Task
number, . Constraints
Output Format
10564
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 0
16
Program:
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
int main() {
printf("%d", sum);
}
Output:
Compiler Message
Success
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Input (stdin)
10564
Expected Output
16
9. Bitwise Operators
Compiler Message
Success
Input
(stdin)
Download
10564
Expected Output
Download
16
Program:
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
max_xor = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n;
j++) { int temp_and =
i & j;
int temp_or = i
| j; int temp_xor
= i ^ j;
k) { max_and = temp_and;
{ max_xor = temp_xor;
int main() {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int n, k;
return 0;
}
Output:
Compiler Message
Success
Input (stdin)
54
Expected Output
3
[COMPUTER PROGRAMMING LAB] 24X51A05C8
4444444
4333334
4322234
4321234
4322234
4333334
4444444
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Input Format
Constraints
Sample Input 0
Sample Output 0
222
212
222
Sample Input 1
Sample Output 1
[COMPUTER PROGRAMMING LAB] 24X51A05C8
555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
Sample Input 2
7
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 2
7777777777777
7666666666667
7655555555567
7654444444567
7654333334567
7654322234567
7654321234567
7654322234567
7654333334567
7654444444567
[COMPUTER PROGRAMMING LAB] 24X51A05C8
7655555555567
7666666666667
7777777777777
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
scanf("%d", &n);
len; j++) {
printf("\n");
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
Output:
Compiler Message
Success
Input (stdin)
Downloa
d2
Expected Output
Download
222
212
[COMPUTER PROGRAMMING LAB] 24X51A05C8
222
11.1D Arrays In C
While it is true that you can sum the elements as they are read,
without first storing them to an array, but you will not get the
experience working with an array. Efficiency will be required later.
Input Format
Constraints
Output Format
Sample Input 0
[COMPUTER PROGRAMMING LAB] 24X51A05C8
16 13 7 2 1 12
Sample Output 0
51
Sample Input 1
1 13 15 20 12 13 2
Sample Output 1
76
Program:
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main() {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
/* Enter your code here. Read input from STDIN. Print output to STDOUT
*/ int n;
scanf("%d", &n);
int arr[n];
i++)
{ scanf("%d",
&arr[i]);
int sum = 0;
i++) { sum +=
arr[i];
printf("%d",
sum); return 0;
Output:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Compiler Message
Success
Input (stdin)
16 13 7 2 1 12
Expected Output
51
12.Array Reversal In C
Constraints
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Input 0
16 13 7 2 1 12
Sample Output 0
12 1 2 7 13 16
Explanation 0
Sample Input 1
7
[COMPUTER PROGRAMMING LAB] 24X51A05C8
1 13 15 20 12 13 2
Sample Output 1
2 13 12 20 15 13 1
Sample Input 2
15 5 16 15 17 11 5 11
Sample Output 2
11 5 11 17 15 16 5 15
program:
#include <stdio.h>
#include <stdlib.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main()
scanf("%d", &num);
+) { scanf("%d",
arr + i);
int temp;
temp = arr[i];
arr[i] = arr[num-
1-i]; arr[num-1-i]
= temp;
return 0;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Output:
Compiler Message
Success
Input (stdin)
16 13 7 2 1 12
Expected Output
12 1 2 7 13 16
13.Printing Tokens
Constraints
Output Format
Print each word of the sentence in a new
This is C
Sample Output 0
[COMPUTER PROGRAMMING LAB] 24X51A05C8
This
is
Explanation 0
In the given string, there are three words ["This", "is", "C"]. We
have to print each of these words in a new line.
Sample Input 1
Learning C is
fun
Sample Output 1
Learning
is
fu
Sample Input 2
How is that
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 2
How
is
that
Program:
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
int main() {
char *s;
s = malloc(1024 *
sizeof(char)); scanf("%[^\
n]", s);
*c = '\n';
printf("%s",
s); return
0;
Output:
Compiler Message
Success
Input (stdin)
This is C
Expected Output
[COMPUTER PROGRAMMING LAB] 24X51A05C8
This
is
C{-truncated-}
14.Digit Frequency In C
Given a string, , consisting of alphabets and digits, find the frequency of each
Format
The first line contains a string, which is the given number.
Constraints
Sample Input 0
a11472o5t6
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 0
0210111100
Explanation 0
Sample Input 1
lw4n88j12n1
Sample Output 1
0210100020
[COMPUTER PROGRAMMING LAB] 24X51A05C8
1v88886l256338ar0ekk
Sample Input 2
Sample Output 2
1112012050
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
/* Enter your code here. Read input from STDIN. Print output to STDOUT
*/ char s[1000];
scanf("%[^\n]", s);
freq[s[i] - '0']++;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
Output:
Compiler Message
Success
Input (stdin)
a11472o5t6
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Expected Output
0210111100
15.Dynamic Array In C
Snow Howler is the librarian at the central library of the city of HuskyLand. He
must handle requests which come in the following forms:
int* total_number_of_books;
/*
*/
int** total_number_of_pages;
/*
* This stores the total number of pages in each book of each shelf.
* The rows represent the shelves and the columns represent the books.
*/
Input Format
Constraints
[COMPUTER PROGRAMMING LAB] 24X51A05C8
For each query of the second type, it is guaranteed that a book is present on
the shelf at index.
Both the shelves and the books are numbered starting from 0.
Output Format
Write the logic for the requests of type 1. The logic for requests of types 2 and 3
are provided.
Sample Input 0
1 0 15
1 0 20
1 2 78
[COMPUTER PROGRAMMING LAB] 24X51A05C8
220
30
Sample Output 0
78
Explanation 0
Program:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <stdio.h>
#include <stdlib.h>
/*
*/
int* total_number_of_books;
/*
* This stores the total number of pages in each book of each shelf.
* The rows represent the shelves and the columns represent the books.
[COMPUTER PROGRAMMING LAB] 24X51A05C8
*/
int** total_number_of_pages;
int main()
int total_number_of_shelves;
scanf("%d", &total_number_of_shelves);
int total_number_of_queries;
scanf("%d", &total_number_of_queries);
total_number_of_books = (int
*)malloc(sizeof(int)*total_number_of_shelves);
[COMPUTER PROGRAMMING LAB] 24X51A05C8
*(total_number_of_books + i) = 0;
while(total_number_of_queries--){
int type_of_query;
scanf("%d",
&type_of_query);
if(type_of_query == 1){
int x, y;
*(total_number_of_pages + x)
=
(int*)realloc(*(total_number_of_pages+x),sizeof(int)*(booksInShelf+1));
*(*(total_number_of_pages+x)+booksInShelf) = y;
*(total_number_of_books + x) += 1;
} else if (type_of_query == 2) {
int x, y;
} else {
int x;
scanf("%d", &x);
if (total_number_of_books) {
free(total_number_of_books);
if (*(total_number_of_pages + i)) {
free(*(total_number_of_pages + i));
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
if (total_number_of_pages) {
free(total_number_of_pages);
return 0;
Objective
This challenge will help you learn the concept of recursion.
void recurse() {
.....
.....
int main() {
.....
.....
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Task
There is a series, , where the next term is the sum of pervious three
terms. Given the first three terms of the series, , , and respectively,
you have to output the nth term of the series using recursion.
Constraints
Output Format
5
[COMPUTER PROGRAMMING LAB] 24X51A05C8
123
Sample Output 0
11
Explanation 0
From steps , , , and , we can say ; then using the values from step , , ,
and , we get . Thus, we print as our answer.
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Program:
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
int term, t1 = a, t2 = b, t3 = c;
if (n == 1)
[COMPUTER PROGRAMMING LAB] 24X51A05C8
term = t1;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
else if (n == 2)
term = t2;
else if (n == 3)
term = t3;
else {
term = t1 + t2 + t3;
t1 = t2;
t2 = t3;
t3 = term;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return term;
int main() {
int n, a, b, c;
printf("%d", ans);
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
Output:
Compiler Message
Success
Input (stdin)
123
Expected
Output 11
The array name, , works as a pointer which stores the base address
of that array. In other words, contains the address where is stored in
the memory.
Function Description
parameters:
Returns
Input Format
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Constraints
(where )
= or
Sample Input 0
Sample Output 0
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Explanation 0
= [3, 2, 5] and
= . So, .
Sample Input 1
Sample Output 1
Explanation 1
= [1, 2, 3, 4, 5] and
= So, = = .
Sample Input 2
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 2
Explanation 2
= [5] and =
Program:
#include <stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
//Complete the following function.
return sum;
int main() {
int
number_of_students;
char gender;
int sum;
scanf("%d", &number_of_students);
printf("%d", sum);
free(marks);
return 0;
Output:
Compiler Message
Success
Input (stdin)
b
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Expected Output
an array of strings :
non-decreasing order.
Input Format
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Constraints
No. of Strings
You have to write your own sorting function and you cannot use
the inbuilt function
Output Format
The locked code-stub will check the logic of your code. The output
consists of the strings sorted according to the four comparsion
functions in the order mentioned in the problem statement.
Sample Input 0
wkue
qoi
[COMPUTER PROGRAMMING LAB] 24X51A05C8
sbv
fekls
Sample Output 0
fekls
qoi
sbv
wkue
wkue
sbv
qoi
[COMPUTER PROGRAMMING LAB] 24X51A05C8
fekls
qoi
sbv
wkue
fekls
qoi
sbv
wkue
fekls
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
i++) { if (freq_a[a[i] -
'a'] == 0) {
count_a++;
freq_a[a[i] - 'a'] = 1;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
i++) { if (freq_b[b[i] -
'a'] == 0) {
count_b++;
freq_b[b[i] - 'a'] = 1;
if (count_a == count_b) {
} else {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
if (len_a == len_b) {
} else {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
arr[i] = arr[j];
arr[j] = temp;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Output:
Compiler Message
Success
19.Permutations Of Strings
Strings are usually ordered in lexicographical order. That means they are
ordered by comparing their leftmost different characters. For example, because .
Also because . If one string is an exact prefix of the other it is lexicographically
smaller, e.g., .
[COMPUTER PROGRAMMING LAB] 24X51A05C8
ab bc cd
ab cd bc
bc ab cd
bc cd ab
cd ab bc
cd bc ab
ab ab bc
ab bc ab
bc ab ab
Input Format
The first line of each test file contains a single integer , the length of
the string array .
. Constraints
Output Format
[COMPUTER PROGRAMMING LAB] 24X51A05C8
ab
cd
Sample Output 0
ab cd
cd ab
Sample Input 1
a
[COMPUTER PROGRAMMING LAB] 24X51A05C8
bc
bc
Sample Output 1
a bc bc
bc a bc
bc bc a
Explanation 1
This is similar to the note above. Only three of the six
permutations are printed to avoid redundancy in output.
Program:
#include <stdio.h>
#include <stdlib.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <string.h>
int k = -1;
i++) { if (strcmp(s[i],
s[i+1]) < 0)
k = i;
if (k == -1) return 0;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int l = -1;
i++) { if
0)
l = i;
s[k] = s[l];
s[l] = tmp;
while (i < j) {
tmp = s[i];
s[i++] = s[j];
s[j--] = tmp;
return 1;
int main()
[COMPUTER PROGRAMMING LAB] 24X51A05C8
char **s;
int n;
scanf("%d", &n);
s = calloc(n, sizeof(char*));
s[i] = calloc(11,
sizeof(char)); scanf("%s",
s[i]);
do
[COMPUTER PROGRAMMING LAB] 24X51A05C8
free(s[i]);
free(s);
return 0;
Output:
Compiler Message
Success
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Input Format
Each test case tests the logic of your code by sending a test
implementation of 3, 5 and 10 elements respectively.
The error log prints the parameters which are passed to the test
implementation. It also prints the sum, minimum element and
maximum element corresponding to your code.
Constraints
Output Format
Sample Input 0
Sample Output 0
Correct Answer
Correct Answer
Correct Answer
Program:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MIN_ELEMENT 1
int result = 0;
va_list args;
va_start(args, count);
[COMPUTER PROGRAMMING LAB] 24X51A05C8
va_end(args);
return result;
va_list args;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
va_start(args, count);
result = current;
va_end(args);
return result;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
va_list args;
va_start(args, count);
result = current;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
va_end(args);
return result;
int test_implementations_by_sending_three_elements() {
srand(time(NULL));
int elements[3];
[COMPUTER PROGRAMMING LAB] 24X51A05C8
elements[1], elements[2]);
%d\n\n", maximum_element);
int expected_elements_sum
+) {
return 0;
return 0;
expected_elements_sum += elements[i];
int test_implementations_by_sending_five_elements() {
srand(time(NULL));
int elements[5];
%d\n\n", maximum_element);
int expected_elements_sum
+) {
return 0;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
expected_elements_sum += elements[i];
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int test_implementations_by_sending_ten_elements() {
srand(time(NULL));
int elements[10];
%d\n\n", maximum_element);
int expected_elements_sum
i++) {
return 0;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
expected_elements_sum += elements[i];
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main ()
int number_of_test_cases;
scanf("%d", &number_of_test_cases);
while (number_of_test_cases--) {
if (test_implementations_by_sending_three_elements()) {
printf("Correct Answer\n");
} else {
printf("Wrong Answer\n");
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
if (test_implementations_by_sending_five_elements()) {
printf("Correct Answer\n");
} else {
printf("Wrong Answer\n");
if (test_implementations_by_sending_ten_elements()) {
printf("Correct Answer\n");
} else {
printf("Wrong Answer\n");
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
Output:
Compiler Message
Success
You will convert a raw text document into its component paragraphs,
sentences and words. To test your results, queries will ask you to
return a specific paragraph, sentence or word as described below.
A word is described by .
For example:
Learning C is fun.
Learning pointers is more fun.It is good to have pointers.
Input Format
Constraints
The text which is passed to the has words separated by a space (" "),
sentences separated by a period (".") and paragraphs separated by a
newline("\n").
Output Format
Sample Input 0
2
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Learning C is fun.
12
211
3111
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 0
Learning C is fun
Learning
Explanation 0
Program:#include
<stdio.h> #include
<stdlib.h> #include
<string.h>
#include<assert.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#define MAX_PARAGRAPHS 5
char* kth_word_in_mth_sentence_of_nth_paragraph(char****
document, int k, int m, int n) {
return document[n-1][m-1][k-1];
return document[m-1][k-1];
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return document[k-1];
return document[k-1];
assert(text != NULL);
char** result =
1;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
*result = temp;
while(temp != NULL) {
size++;
result = realloc(result,size*sizeof(char*));
result[size-1] = temp;
return result;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
assert(text != NULL);
npar++;
doc[npar] = NULL;
int i = 0;
char** sentences =
while(sentences[nsen] != NULL) {
nsen++;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
doc[i] = malloc((nsen+1)*sizeof(char**));
doc[i][nsen] = NULL;
int j = 0;
j++;
i++;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return doc;
char* get_input_text() {
int paragraph_count;
scanf("%d", ¶graph_count);
memset(doc, 0, sizeof(doc));
getchar();
scanf("%[^\n]%*c", p[i]);
strcat(doc, p[i]);
if (i != paragraph_count -
1) strcat(doc, "\n");
strcpy(returnDoc, doc);
return returnDoc;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
printf("%s", word);
int word_count;
scanf("%d", &word_count);
for(int i = 0; i <
word_count; i++){
printf("%s",
sentence[i]); if( i !=
word_count - 1)
printf(" ");
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int sentence_count;
scanf("%d", &sentence_count);
print_sentence(*(paragraph + i));
printf(".");
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main()
int q;
scanf("%d", &q);
while (q--) {
int type;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
scanf("%d", &type);
if (type == 3){
int k, m, n;
char* word =
kth_word_in_mth_sentence_of_nth_paragraph(document,
k, m, n);
print_word(word);
int k, m;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
print_sentence(sentence);
else{
int k;
scanf("%d", &k);
print_paragraph(paragraph);
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
printf("\n");
Output:
Compiler Message
Success
22. Boxes through A Tunnel
You are transporting some boxes through a tunnel, where each box
is a parallelepiped, and is characterized by its length, width and
height.
The height of the tunnel feet and the width can be assumed to be
infinite. A box can be carried through the tunnel only if its height is
strictly less than the tunnel's height. Find the volume of each box
that can be successfully transported to the other end of the tunnel.
Note: Boxes cannot be rotated.
Input Format
Constraints
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Output Format
For every box from the input which has a height lesser than
feet, print
its volume in a separate line.
Sample Input 0
4
555
1 2 40
10 5 41
7 2 42
Sample Output 0
125
80
Explanation 0
The first box is really low, only feet tall, so it can pass through the
tunnel and its volume is .
The third box is exactly feet tall, so it cannot pass. The same
can be said about the fourth box.
Program:
#include <stdio.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <stdlib.h>
#define MAX_HEIGHT
41 struct box
{
/**
* Define three fields of type int: length, width and height
*/
int
length;
int
width;
int
height;
};
box; int
get_volume(box b) {
/**
* Return the volume of the box
*/
return b.length * b.width * b.height;
}
int is_lower_than_max_height(box b) {
/**
* Return 1 if the box's height is lower than MAX_HEIGHT and 0
otherwise
*/
if (b.height < MAX_HEIGHT)
{ return 1;
} else {
return 0;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int main()
{
int n;
scanf("%d",
&n);
box *boxes = malloc(n * sizeof(box));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &boxes[i].length, &boxes[i].width,
&boxes[i].height);
}
for (int i = 0; i < n; i++) {
if
(is_lower_than_max_height(boxes[i
])) { printf("%d\n",
get_volume(boxes[i]));
}
}
return 0;
}
Output:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Compiler Message
Success
Input (stdin)
[COMPUTER PROGRAMMING LAB] 24X51A05C8
555
1 2 40
10 5 41
7 2 42
Expected Output
125
80
You are given triangles, specifically, their sides , and . Print them in the same style
but sorted by their areas from the smallest one to the largest one. It is guaranteed
that all the areas are different.
The best way to calculate a area of a triangle with sides , and is Heron's formula:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
where .
Input Format
The first line of each test file contains a single integer . lines follow
with three
space-separated integers, ,
and . Constraints
, and
Output Format
Sample Input 0
7 24 25
5 12 13
345
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Sample Output 0
345
5 12 13
7 24 25
Explanation 0
The area of the first triangle is . The area of the second triangle is .
The area of the third triangle is . So the sorted order is the reverse
one.
Program:
#include <stdio.h>
#include
<stdlib.h>
#include
<math.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
struct triangle
int a;
int b;
int c;
};
return 1;
return -1;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
} else {
return 0;
Output:Compiler Message
Success
Input (stdin)
7 24 25
5 12 13
345
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Expected Output
345
5 12 13
7 24 25
24.Post Transition
Packages are stored in some order in the office queue. That means,
that they are processed using this order when sending and
receiving.
Sometimes two post offices, even in different towns, may organize the
following transaction: the first one sends all its packages to the
second one. The second one accepts the packages that satisfy the
weight condition for the second office and rejects all other ones.
These rejected packages return to the first office back and are
stored in the same order they were stored before they were sent.
The accepted packages move to the tail of the second office's queue in
the same order they were stored in the first office.
[COMPUTER PROGRAMMING LAB] 24X51A05C8
- given the town , print all packages in this town. They should be printed as
follows:
Town_name:
0:
id_0
id_1
...
1:
id_2
id_3
...
[COMPUTER PROGRAMMING LAB] 24X51A05C8
...
where , etc are the numbers of post offices and , ... are the ids of packages
from the th post office in the order of its queue, , are from the st one etc. There
should be one '\t' symbol before post office numbers and two '\t' symbols before
the ids.
- given the towns and and post office indices and , manage the transaction described
above between the post office # in town and the post office # in town
.
- given all towns, find the one with the most number of packages in all post
offices altogether. If there are several of them, find the first one from the
collection .
- given all towns and a string , find the town with the name . It's guaranteed
that the town exists.
Input Format
First line of the input contains a single integer . blocks follow, each
describing a town.
Every town block contains several lines. On the first line there is a
string - the name of the town. On the second line there is an
integer - the number of the offices in the town. blocks follow then,
each describing an office.
Every office block also contains several lines. On the first line there
are three integers separated by single spaces: (the number of
packages in the office), and (described above). blocks follow, each
describing a package.
[COMPUTER PROGRAMMING LAB] 24X51A05C8
Every package block contains exactly two lines. On the first line
there is a string which is an id of the package. On the second line
there is an integer which is the weight of the package.
Every query block contains several lines. On the first line there is an
integer , or . If this integer is , on the second line there is a string -
the name of town for which all packages should be printed. If this
integer is , on the second line there are string , integer , string and
integer separated by single spaces. That means transactions
between post office # in the town and post office # in the town
should be processed.
If the integer is , no lines follow and the town with the most number
of packages should be found.
Constraints
[COMPUTER PROGRAMMING LAB] 24X51A05C8
,.
All towns' names have only uppercase english letters and are unique.
All packages' ids have only lowercase english letters and are unique.
All queries are valid, that means, towns with the given names
always exist, post offices with the given indices always exist.
Output Format
For queries of type , print all packages in the format provided in the
problem statement. For queries of type , print "Town with the most
number of packages is " on a separate line.
Sample Input 0
213
a2
b3
124
[COMPUTER PROGRAMMING LAB] 24X51A05C8
c2
414
d1
e2
f3
h4
2B0A1
[COMPUTER PROGRAMMING LAB] 24X51A05C8
1A
1B
Sample Output 0
A:
0:
1:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
B:
0:
Explanation 0
Before all queries, town B has packages in total, town has . But
after transaction all packages from B's th post office go to the st
post office of A, except package d because it's too light.
Program:
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LENGTH 6
struct package
char* id;
int weight;
};
struct post_office
{
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int min_weight;
int max_weight;
package* packages;
int packages_count;
};
struct town
char* name;
post_office* offices;
int offices_count;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
};
void print_all_packages(town t)
printf("%s:\n", t.name);
printf("\t%d:\n", i);
printf("\t\t%s\n", t.offices[i].packages[j].id);
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int n = 0;
if (source-
>offices[source_office_index].packages[i].weight
>= target->offices[target_office_index].min_weight
&&
source-
>offices[source_office_index].packages[i].weight <= target-
>offices[target_office_index].max_weight)
++n;
package* oldPackages
=
malloc(sizeof(package)*(source-
>offices[source_office_index].packages_count - n));
newPackages[i] = target->offices[target_office_index].packages[i];
n = target->offices[target_office_index].packages_count;
int m = 0;
if (source-
>offices[source_office_index].packages[i].weight
>= target->offices[target_office_index].min_weight
&&
source-
>offices[source_office_index].packages[i].weight <= target-
>offices[target_office_index].max_weight)
[COMPUTER PROGRAMMING LAB] 24X51A05C8
newPackages[n] = source-
>offices[source_office_index].packages[i];
[COMPUTER PROGRAMMING LAB] 24X51A05C8
++n;
else
oldPackages[m] = source-
>offices[source_office_index].packages[i];
++m;
target->offices[target_office_index].packages_count = n;
free(target->offices[target_office_index].packages);
target->offices[target_office_index].packages = newPackages;
source->offices[source_office_index].packages_count = m;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
free(source->offices[source_office_index].packages);
source->offices[source_office_index].packages = oldPackages;
int number_of_packages(town t)
int ans = 0;
ans += t.offices[i].packages_count;
return ans;
int ans;
max_packages = number_of_packages(towns[i]);
ans = i;
return towns[ans];
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
if (!strcmp(towns[i].name, name))
return &(towns[i]);
return &towns[0];
int main()
int towns_count;
scanf("%d", &towns_count);
[COMPUTER PROGRAMMING LAB] 24X51A05C8
scanf("%s", towns[i].name);
scanf("%d",
&towns[i].offices_count);
towns[i].offices = malloc(sizeof(post_office)*towns[i].offices_count);
scanf("%d%d%d",
&towns[i].offices[j].packages_count, &towns[i].offices[j].min_weight,
&towns[i].offices[j].max_weight);
towns[i].offices[j].packages
= malloc(sizeof(package)*towns[i].offices[j].packages_count);
towns[i].offices[j].packages[k].id =
malloc(sizeof(char)
*
MAX_STRING_LENGTH);
scanf("%s", towns[i].offices[j].packages[k].id);
scanf("%d",
&towns[i].offices[j].packages[k].weight);
int queries;
scanf("%d", &queries);
char town_name[MAX_STRING_LENGTH];
while (queries--) {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int type;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
scanf("%d", &type);
switch (type) {
case 1:
scanf("%s", town_name);
print_all_packages(*t);
break;
case 2:
scanf("%s", town_name);
int source_index;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
scanf("%d",
&source_index);
scanf("%s", town_name);
int target_index;
scanf("%d", &target_index);
break;
case 3:
break;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
return 0;
Output:Compiler Message
Success
Input (stdin)
21
3
a2
b3
12
4
c2
[COMPUTER PROGRAMMING LAB] 24X51A05C8
B
1
41
d1
e2
f3
h4
2B0A
1 A{-truncated-}
Expected
Output
number of packages is A A:
0:
b
[COMPUTER PROGRAMMING LAB] 24X51A05C8
1:
0:
25.Structuring The
Document
struct word {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
char* data;
};
struct sentence {
};
The words in the sentence are separated by one space (" "). The last
word does not end with a space.
struct paragraph {
};
struct document {
};
For example:
Learning C is fun.
Learning pointers is more fun.It is good to have pointers.
Input Format
Constraints
Output Format
Sample Input 0
Learning C is fun.
[COMPUTER PROGRAMMING LAB] 24X51A05C8
pointers. 3
12
211
3111
Sample Output 0
Learning
Explanation 0
Program:
#include
<stdio.h>
#include
<stdlib.h>
#include
[COMPUTER PROGRAMMING LAB] 24X51A05C8
<string.h>
[COMPUTER PROGRAMMING LAB] 24X51A05C8
#include <assert.h>
#define MAX_PARAGRAPHS 5
struct word {
char* data;
};
struct sentence {
struct word*
data;
};
struct paragraph {
};
[COMPUTER PROGRAMMING LAB] 24X51A05C8
struct document {
};
doc.data = NULL;
doc.paragraph_count =
*s; ++s)
if (cur_paragraph == NULL)
{
[COMPUTER PROGRAMMING LAB] 24X51A05C8
doc.paragraph_count++;
cur_paragraph = doc.data +
doc.paragraph_count - 1; cur_paragraph->data
= NULL;
cur_paragraph->sentence_count = 0;
cur_sentence = NULL;
if (cur_sentence == NULL)
cur_paragraph->sentence_count++;
cur_paragraph->data = (struct
sentence *) realloc(cur_paragraph->data, sizeof(struct
sentence) * cur_paragraph-
>sentence_count);
cur_sentence = cur_paragraph->data
+ cur_paragraph-
>sentence_count - 1;
cur_sentence->data = NULL;
cur_sentence->word_count = 0;
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
cur_sentence->word_count++;
cur_sentence->data[cur_sentence->word_count - 1].data =
if (*s == '.')
cur_sentence = NULL;
*s = 0;
cur_sentence = NULL;
cur_paragraph = NULL;
else
if (new_word == NULL)
{
[COMPUTER PROGRAMMING LAB] 24X51A05C8
new_word = s;
return doc;
{
[COMPUTER PROGRAMMING LAB] 24X51A05C8
{ printf("%s", w.data);
sen.word_count; i++) {
print_word(sen.data[i]);
if (i != sen.word_count -
1) { printf(" ");
print_sentence(para.data[i]);
printf(".");
doc.paragraph_count; i++) {
print_paragraph(doc.data[i]);
if (i != doc.paragraph_count - 1)
printf("\n");
char*
get_input_text() {
int
paragraph_count;
[COMPUTER PROGRAMMING LAB] 24X51A05C8
scanf("%d", ¶graph_count);
[COMPUTER PROGRAMMING LAB] 24X51A05C8
memset(doc, 0, sizeof(doc));
getchar();
+) { scanf("%[^\n]%*c", p[i]);
strcat(doc, p[i]);
if (i != paragraph_count -
1) strcat(doc, "\n");
strcpy(returnDoc, doc);
return returnDoc;
int main()
int q;
scanf("%d",
&q);
while (q--)
{ int
type;
scanf("%d", &type);
if (type ==
3){ int k,
m, n;
struct word w =
kth_word_in_mth_sentence_of_nth_paragraph(Doc, k,
m, n);
print_word(w);
}
[COMPUTER PROGRAMMING LAB] 24X51A05C8
else if (type == 2) {
[COMPUTER PROGRAMMING LAB] 24X51A05C8
int k, m;
m); print_sentence(sen);
else{
int k;
scanf("%d",
&k);
print_paragraph(para);
printf("\n");
Output:
Compiler Message
Success
[COMPUTER PROGRAMMING LAB] 24X51A05C8
[COMPUTER PROGRAMMING LAB] 24X51A05C8
[COMPUTER PROGRAMMING LAB] 24X51A05C8