23SC1101 CTSD INSEM 2 Scheme of Evaluation
23SC1101 CTSD INSEM 2 Scheme of Evaluation
November-2024
General Instructions:
Acknowledge that the suggested answer key is a sample solution and may
not be the only correct approach.
#include<stdio.h>
int main()
{
int arr[]={1,3,5,7,9};
for(int i=0;i<5;i+=2)
{
}
return 0;
Page 1 of 18
}
Ans: 1 5 9
1. D) How can you access the members of structure in C? [2M]
Members of a structure can be accessed using the dot operator (.) if you have structure
variable or using the arrow operator (->) if you have a pointer to a structure.
1. Using the dot operator (.):
If you have a structure variable, you can access its members by using the dot operator.
2. Using the arrow operator (->):
If you have a pointer to a structure, you can access its members using the arrow
operator.
Example:
#include <stdio.h>
struct Person
{
char name[50];
int age;
};
int main()
{
struct Person person1 = {"John", 25};
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age
struct Person *ptr = &person1;
printf("Name using pointer: %s\n", ptr->name);
printf("Age using pointer: %d\n", ptr->age);
return 0;
}
Output:
Name: Ramesh
Age: 25
Name using pointer: Suresh
Age using pointer: 25
Dot operator (.): Used to access members of the structure when using a
structure variable (person1).
Arrow operator (->): Used to access members of the structure when using a
pointer to the structure (ptr).
1. E) Draw the status of stack after performing the following sequence of operations on
stack?
push(23), pop(), push(22), push(25), push(30), pop() [2M]
Page 2 of 18
30
25 25
23 22 22
struct Node
{
int data;
struct Node* next;
};
struct Node: Declares a new structure type named Node.
int data: An integer variable to store data within the node.
struct Node* next: A pointer to the next node in the linked list. This pointer is
used to link nodes together to form the list.
CODE-(3M)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc != 4)
{
printf("Provide appropriate input");
return 1;
}
char operator = argv[1][0];
double operand1 = atof(argv[2]);
double operand2 = atof(argv[3]);
double result;
switch (operator)
{
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case '*': result = operand1 * operand2;
break;
case '/': if (operand2 == 0)
{
printf("Error: Division by zero is not allowed.\n");
return 1;
}
Page 3 of 18
result = operand1 / operand2;
break;
default: printf("Error: Unsupported operator '%c'. Supported operators are
+, -, *, and /.\n", operator);
return 1;
}
printf("Result: %.2f\n", result);
return 0;
}
OUTPUT: (1M)
Input parameters: * 2 5
Output: Result : 10.00
2. B) Write a C program that counts the number of words in a sentence entered by the
user.
Sample Input: welcome to KL university
Sample Output: 4 [4M]
#include<stdio.h>
int main()
{
int i,count=1; //Reading the string-1M
char str1[40];
printf("Enter String\n");
scanf("%[^\n]s",str1);
for(i=0;str1[i]!='\0';i++) //Logic-2M
{
if(str1[i]==' ')
{
count++;
}
}
printf("%d",count); //Print Count-1M
}
Output:
Welcome to KL University
4
#include <stdio.h>
struct student //Definition of structure --- 1 Mark
{
long int id;
char name[50];
float cgpa;
Page 4 of 18
};
int main()
{
int n;
printf("enter n value");
scanf("%d",&n);
struct student s[n];
for (int i = 0; i < n; i++) // Reading Data --- 1.5 Marks
{
printf("Enter id, name and cgpa\n");
scanf("%ld %s %f", &s[i].id,&s[i].name,&s[i].cgpa);
}
printf("\nStudent Details are: \n");
for (int i = 0; i < n; i++) //Printing Data --- 1.5 Marks
{
printf("\nStudent %d:\n", i + 1);
printf("ID : %ld \nName : %s\nCGPA : %.2f\n",
s[i].id,s[i].name,s[i].cgpa);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_SIZE 10
int mainStack[MAX_SIZE];
int top = -1;
int maxElement = INT_MIN;
void push(int ele)
{
if (top >= MAX_SIZE - 1)
{
printf("Stack is full\n");
return;
}
if (top == -1)
{
maxElement = ele;
}
else if (ele > maxElement)
{
maxElement = ele;
}
top++;
mainStack[top] = ele;
Page 5 of 18
}
int getMax()
{
if (top < 0)
{
printf("Stack is empty\n");
return INT_MIN;
}
return maxElement;
}
void printStack()
{
int i;
if (top < 0)
{
printf("Stack is empty\n");
return;
}
printf("Current stack elements:\n");
for (i = 0; i <= top; i++)
{
printf("%d ", mainStack[i]);
}
printf("\n");
}
int main()
{
int ch;
while (1)
{
printf("\n1. Push an element\n");
printf("2. Print stack elements\n");
printf("3. Get max element\n");
printf("4. Quit\n");
printf("Enter a choice: ");
scanf("%d", &ch);
int item;
switch (ch)
{
case 1: printf("Insert element: ");
scanf("%d", &item);
push(item);
break;
case 2: printStack();
break;
case 3: printf("Maximum element: %d\n", getMax());
break;
Page 6 of 18
case 4: exit(0);
default: printf("Invalid choice\n");
}
}
return 0;
}
3. A) You are an IT company manager. Based on their performance over the last N
working days, you must rate your employee. You are given an array of N integers
called workload, where workload[i] represents the number of hours an employee
worked on an ith day. The employee must be evaluated using the following criteria:
Rating = the maximum number of consecutive working days when the employee
has worked more than 6 hours. Write a C program to give rating to an employee.
[6M]
#include<stdio.h>
int main()
{
int n, i, max = 0, cur = 0; //Declarations --- 1 Mark
int workload[100];
printf("Enter workload:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &workload[i]);
} // Input Statements --- 1 Mark
}
3. B) Develop a C program that takes N integer numbers as input and arranges them in
ascending order using bubble sort. Test the program with the following list of
elements 917, 284, 163, 112, 54 [5M]
Page 7 of 18
How the Program Works
1. Input Initialization: The array arr[] is initialized with the elements 917, 284,
163, 112, 54.
2. Bubble Sort Function: The bubble Sort function iterates through the array and
swaps adjacent elements if they are in the wrong order.
3. Output:
o Prints the original array.
o Prints the sorted array in ascending order after applying the bubble sort.
int main()
{
int n = 5; // Number of elements
int arr[] = {917, 284, 163, 112, 54}; // Input array
// Print the original array
printf("Original array: \n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
// Sort the array
bubbleSort(arr, n);
// Print the sorted array
printf("Sorted array in ascending order: \n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
Page 8 of 18
return 0;
}
Output:
4. A) Jessy is very interested in playing with matrices and he wants to interchange rows
and columns of matrix. Develop a C program to interchange rows and columns of
matrix to help Jessy [6M]
#include <stdio.h>
void transposeMatrix(int rows, int cols, int matrix[rows][cols], int result[cols][rows])
{
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
result[j][i] = matrix[i][j]; // Swap rows and columns
}
}
}
int main()
{
int rows, cols;
printf("Enter the number of rows: ");
Page 9 of 18
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols], transposed[cols][rows],i,j;
printf("Enter the elements of the matrix:\n");
for ( i = 0; i < rows; i++)
{
for ( j = 0; j < cols; j++)
{
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
transposeMatrix(rows, cols, matrix, transposed);
printf("\nOriginal Matrix:\n");
printMatrix(rows, cols, matrix);
printf("\nTransposed Matrix (Rows and Columns Interchanged):\n");
printMatrix(cols, rows, transposed);
return 0;
}
Explanation
1. Matrix Input:
o The program prompts the user to enter the dimensions of the matrix and its elements.
2. Transposing:
o The transposeMatrix function takes the original matrix and creates a new matrix by
swapping rows and columns. The element at position [i][j] in the original matrix is
placed at [j][i] in the transposed matrix.
3. Display Results:
o The printMatrix function is used to display both the original and the transposed
matrices.
4. Dynamic Handling:
o The program uses flexible input for the matrix dimensions, making it suitable for any
size within the bounds of the C array.
Page 10 of 18
Transposed Matrix (Rows and Columns Interchanged):
14
25
36
4. B) Trace out the given key element 25 in the following list of elements
{4,5,10,15,20,25,30,35,40,45,50,58,65,80,98} using binary search and also write a
function to implement binary search [5M]
Output :
Enter value to find: 25
25 found at location 5.
5. A) Develop a C program to implement stack data structure using array and your
program should also perform the following operations on it
i. push
Page 11 of 18
ii. pop
iii. display [6M]
#include<stdio.h>
#define size 5
int ele,top=-1;
int stack[size];
void push() // 2 MARKS
{
printf("enter the ele");
scanf("%d",&ele);
if(top==size-1)
{
printf("overflow/stack is full");
exit(0);
}
else
{
top++;
stack[top]= ele;
}
}
Page 12 of 18
}
#include<stdio.h> // 1 Mark
#define n 5
int front=-1,rear=-1;
int queue[n];
void add_customerid();
void remove_customerid();
int main()
{
while(1)
{
printf("1.add customer id to queue(enqueue)\n");
printf("2.remove customer id from queue(dequeue)\n");
int ch;
printf("enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: add_customerid();
break;
case 2: remove_customerid();
break;
default: printf("please enter 1 to 2 numbers only\n");
Page 13 of 18
}
}
}
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}s;
s *head=NULL,*tail=NULL,*p,*q,*k;
void create();
void InsMid();
Page 14 of 18
void Display();
int main() // WRITING MAIN FUNCTION ---2 MARKS
{
int opt;
do
{
printf("opt1:create\n");
printf("opt4:InsMid\n");
printf("opt8:Display\n");
printf("Enter option:\n");
scanf("%d",&opt);
switch(opt)
{
case 1:create();
break;
case 4:InsMid();
break;
case 8:Display();
break;
default:printf("wrong choice");
}
}while(opt>=1&&opt<=8);
}
void create() // FOR NODE CREATION -----1MARK
{
int x,n,i;
printf("enter how many ele u want to insert n:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(s*)malloc(sizeof(s));
printf("enter data:\n");
scanf("%d",&x);
p->data=x;
p->next=NULL;
if(i==1)
head=tail=p;
else
{
tail->next=p;
tail=p;
}
}
}
void InsMid() // FOR INSERTION AT POSITION -----2 MARKS
{
int x,i=1,pos;
q=(s*)malloc(sizeof(s));
printf("enter data:\n");
scanf("%d",&x);
Page 15 of 18
q->data=x;
q->next=NULL;
printf("enter position:\n");
scanf("%d",&pos);
p=head;
while(i<pos)
{
k=p;
p=p->next;
i++;
}
k->next=q;
q->next=p;
}
void Display() // FOR DISPLAYING -----1MARK
{
if(head==NULL)
printf("list is empty\n");
else
{
for(p=head;p!=NULL;p=p->next)
printf("%3d--->",p->data);
}
}
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node *head;
void create() // 2Marks
{
int x;
struct Node *new;
new = (struct Node*)malloc(sizeof(struct Node));
printf("\nenter data to create a node for the node\n");
scanf("%d",&x);
new->data=x;
new->next=NULL;
if(head==NULL)
head=new;
else
Page 16 of 18
{
struct Node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
Page 17 of 18
for(i=0;i<n;i++)
{
create();
}
search();
return 0;
}
Page 18 of 18