0% found this document useful (0 votes)
44 views18 pages

23SC1101 CTSD INSEM 2 Scheme of Evaluation

Uploaded by

saicodeworld
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)
44 views18 pages

23SC1101 CTSD INSEM 2 Scheme of Evaluation

Uploaded by

saicodeworld
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/ 18

CTSD INSEM-2 EXAM ANSWER KEY

November-2024
General Instructions:
Acknowledge that the suggested answer key is a sample solution and may
not be the only correct approach.

-solving approaches and logic


used by students.

Award marks based on correctness and alternative correct solutions


irrespective of adherence to the suggested key.

1. A) What is the difference between malloc() and calloc() functions? [2M]

Feature malloc() calloc()


2 (number of blocks, size of
Arguments 1 (size in bytes)
each block)
Initialization Memory is not initialized Memory is initialized to zero
Speed Faster Slower
When memory initialization When memory needs to be
Usage
is not required initialized to zero

1. B) How are command line arguments used in C? [2M]

Command line arguments provide a way to pass data to a program dynamically.


argc and argv are essential for accessing and processing these arguments.
Careful error checking should be done to ensure the correct number and types of
arguments are provided.
Command line arguments can be used for various purposes, such as specifying
input files, output directories, or configuration options.

1. C) Predict the output of the following code snippet [2M]

#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

1. F) How do you define a node structure in a linked list? [2M]

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.

2. A) Create a command-line utility that performs basic arithmetic operations. The


utility must accept three command-line arguments: An operator (+, -, *. Or /) and
two numeric operands. The program should compute the result of the arithmetic
operation specified by the operator and print it. [4M]

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

2. C) Write a C program that defines a structure called student containing three


members: long int d, char name[50] and float cgpa. Your program must read and
print n student data. [4M]

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

2. D) Write a C program to find out a maximum element in stack of integers. [4M]

#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 days: ");


scanf("%d", &n);

printf("Enter workload:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &workload[i]);
} // Input Statements --- 1 Mark

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


{
if (workload[i] > 6)
{
cur++;
max = (cur > max) ? cur :max;
}
else
{
cur = 0;
}
} // Logic --- 3 Marks

printf("Rating: %d\n", max); // Output Statement --- 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.

//C-Program for Bubble sort


#include <stdio.h>
// Function to perform bubble sort
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

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

void printMatrix(int rows, int cols, int matrix[rows][cols])


{
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

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.

Example Input and Output


Input:
Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements of the matrix:
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Original Matrix:
123
456

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]

#include <stdio.h> // 2 Marks


int main()
{
int n=15, i, search, first, last, middle;
int array[]={4,5,10,15,20,25,30,35,40,45,50,58,65,80,98};
printf("Enter value to find:\n");
scanf("%d", &search);
first = 0; // 3 Marks
last = n - 1;
middle = (first + last) / 2;
while (first <= last)
{
if (array[middle] < search)
{
first = middle + 1;
}
else if (array[middle] == search)
{
printf("%d found at location %d.\n", search, middle);
break;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last)
{
printf("Not found! %d isn't present in the list.\n", search);
}
return 0;
}

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

void pop() // 2 MARKS


{
if(top== -1)
{
printf("stack is empty/underflow");
}
else
{
ele =stack[top];
printf("deleted ele=%d",ele);
top--;
}
}
void display()
{
int i;
if (top!=-1)
{
for( i=top;i>=0;i--)
{
printf("%d",stack[i]);
}
}
else
{
printf("*******empty stack*******");
}

Page 12 of 18
}

int main() // 2 MARKS


{
int ch;
while(1)
{
printf("\n1.push\n2.pop\n3.display\n4.exit(0)");
printf("enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}

5. B) Write a c program that manages a queue of integers representing customer IDs


waiting for service. The program should allow users to:
i. Add a customer to the queue
ii. Remove a customer from the queue [5M]

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

void add_customerid() // 2 Marks


{
if(rear==n-1)
{
printf("queue is full");
}
else
{
int x;
printf("enter element\n");
scanf("%d",&x);
if(front==-1)
front=0;
rear=rear+1;
queue[rear]=x;
}
}

void remove_customerid() // 2 Marks


{
if(front==-1||front>rear)
{
printf("queue is empty\n");
}
else
{
int x=queue[front];
front=front+1;
printf("element deleted\n");
}
}

6. A) Create a linked list and develop an appropriate functions to perform following


operations on linked list
i. Insert a node at given position [6M]

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

6. B) Create a linked list and develop an appropriate functions to perform following


operations on linked list
i. The module should prompt the user to enter a key element and
trace out whether that element is in the list or not. [5M]

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

void search() // 2Marks


{
struct Node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter key to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
}

int main() // 1Mark


{
struct Node* head = NULL;
int n,i;
printf("create linked list with n nodes:\n ");
scanf("%d",&n);

Page 17 of 18
for(i=0;i<n;i++)
{
create();
}
search();
return 0;
}

Page 18 of 18

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