You are on page 1of 5

Ctsd Test 4

2100032256
Yashashvi B
CSE-35

1. Given an array arr[] of 10 elements, Develop a C function code to search for a given
element x in arr[] by using linear search and show index position of x as an output.

#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);

printf("Enter array elements:");


for(i=0;i<n;++i)
scanf("%d",&a[i]);

printf("Enter element to search:");


scanf("%d",&x);

for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
2. A Clinic has 2 doctors on duty. As patients arrive, they are arranged according to their token number. They
have announced to divide the patients into two groups. By that note, all those patients who have their token
number as an even number will wait to meet Dr.Diwakar and those with odd number will be waiting to meet Dr.
Amrutha. Help the hospital staff to find count of number of patients and divide it to Doctor wise count. The token
number should be unique to each patient. Develop a C program which accepts N token numbers and gives you
the result as the count of patients waiting for each doctor.

#include<stdio.h>
void doctor(int n,int a[]);
int main()
{
int n;
printf("enter the number of tokens");
scanf("%d",&n);
int a[n];
printf("enter the token numbers of patients");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
doctor(n,a);
return 0;
}
void doctor(int n,int a[])
{
int drdiwakarcount=0,dramruthacount=0;
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
drdiwakarcount=drdiwakarcount+1;
}
else
{
dramruthacount=dramruthacount+1;
}
}
printf("patients waiting for DR.DIWAKAR is :%d\n",drdiwakarcount);
printf("patients waiting for DR.AMRUTHA is :%d",dramruthacount);
}
}
3. Develop a C program to create an array of 100 students and each student is defined
through a structure with the following members. i) Student number ii) Student name iii)
Branch iv) Year v) fees Display all the students in sorted order with respect to their student
number.

#include <stdio.h>
#include <string.h>
int main()
{
struct student
{
int roll_no;
char name[30];
int phone_number;
};
struct student p1 = {1,"Brown",123443};
struct student p2, p3;
p2.roll_no = 2;
strcpy(p2.name,"Sam");
p2.phone_number = 1234567822;
p3.roll_no = 3;
strcpy(p3.name,"Addy");
p3.phone_number = 1234567844;
printf("First Student\n");
printf("roll_no : %d\n", p1.roll_no);
printf("name : %s\n", p1.name);
printf("phone_number : %d\n", p1.phone_number);
printf("Second Student\n");
printf("roll_no : %d\n", p2.roll_no);
printf("name : %s\n", p2.name);
printf("phone_number : %d\n", p2.phone_number);
printf("Third Student\n");
printf("roll_no : %d\n", p3.roll_no);
printf("name : %s\n", p3.name);
printf("phone_number : %d\n", p3.phone_number);
return 0;
}
4. Define a structure named as Queue. Define the operation such as Enqueue( ), Dequeue(
) and Display( ) to insert an element, to delete an element and to display the element in the
Queue. Develop a menu driven program perform the above operations on a Queue.

#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int items[SIZE], front = -1, rear = -1;
int main() {

deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);

display();
deQueue();
display();

return 0;
}

void enQueue(int value) {


if (rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("\nInserted -> %d", value);
}
}

void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
void display() {
if (rear == -1)
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d ", items[i]);
}
printf("\n");
}

You might also like