Techneical Proficiency

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Experiment no.

1
Program Name:- Write a C program to print 1D array.

Theory:-
· Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
· To create an array, define the data type (like int) and specify the name of the
array followed by square brackets [].
· To insert values to it, use a comma-separated list inside curly braces, and
make sure all values are of the same data type

Program:-

#include <stdio.h>

int main() {

// Declare a 1D array of integers

int numbers[5] = {10, 20, 30, 40, 50};

// Print the elements of the array

printf("The elements of the array are:\n");

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

printf("%d ", numbers[i]);

return 0;

Output:
The elements of the array are:

10 20 30 40 50
Experiment no.2
Program Name:- Write a 2D program in C.
Theory:-
· A 2D array is also known as a matrix (a table of rows and columns).
· A multi-dimensional array can be defined as an array that has more than one
dimension.
· Having more than one dimension means that it can grow in multiple
directions. Some popular multidimensional arrays are 2D arrays and 3D
arrays.

Program:-
#include <stdio.h>

int main() {

// Declare a 2D array (3 rows, 4 columns)

int matrix[3][4] = {

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

// Print the elements of the 2D array

printf("The elements of the 2D array are:\n");

for (int i = 0; i < 3; i++) { // Loop through rows

for (int j = 0; j < 4; j++) { // Loop through columns

printf("%d ", matrix[i][j]);

printf("\n"); // Newline after each row

}
return 0;

Output:-
{

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

Experiment no.3
Program Name:- Write a program of Pointer in C
· Pointers in C are variables that store the memory address of another variable.
Pointers are essential for dynamic memory management and for creating more
efficient code that manipulates variables indirectly

Program:-
#include <stdio.h>

int main() {

int a = 10;

int b = 20;

int *ptr; // Pointer to an integer

// Assign the address of 'a' to the pointer 'ptr'

ptr = &a;

// Print the value of 'a' using the pointer

printf("The value of a is: %d\n", *ptr); // Dereferencing the pointer to get the value of 'a'

// Now assign the address of 'b' to the pointer 'ptr'


ptr = &b;

// Print the value of 'b' using the pointer

printf("The value of b is: %d\n", *ptr); // Dereferencing the pointer to get the value of 'b'

// Changing the value of 'a' through the pointer

*ptr = 30; // Dereferencing 'ptr' to change 'b' to 30 (since ptr now points to b)

printf("The new value of b is: %d\n", b); // Prints 30, as we modified 'b' through the pointer

return 0;

Output:-
The value of a is: 10

The value of b is: 20

The new value of b is: 30

Experiment no.4
Program Name:- Write a program C program for implementation of
FCFC.

Theory:-

· FCFS is a non-preemptive scheduling algorithm as a process holds the CPU


until it either terminates or performs I/O.
· FCFS stands for First Come First Serve. In the FCFS scheduling algorithm, the
job that arrived first in the ready queue is allocated to the CPU and then the
job that came second, and so on.

Program:-
#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[]) {


// Waiting time for the first process is always 0

wt[0] = 0;

// Calculate waiting time for the remaining processes

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

wt[i] = bt[i - 1] + wt[i - 1]; // Waiting time is previous process's burst time + waiting time

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {

// Turnaround time is burst time + waiting time

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

tat[i] = bt[i] + wt[i];

void findAverageTime(int processes[], int n, int bt[]) {

int wt[n], tat[n];

int total_wt = 0, total_tat = 0;

// Function to find waiting time

findWaitingTime(processes, n, bt, wt);

// Function to find turnaround time

findTurnAroundTime(processes, n, bt, wt, tat);

printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");

// Calculate total waiting time and turnaround time

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


total_wt += wt[i];

total_tat += tat[i];

printf("%d\t\t%d\t\t%d\t\t%d\n", processes[i], bt[i], wt[i], tat[i]);

// Calculate average waiting time and turnaround time

printf("\nAverage waiting time: %.2f\n", (float)total_wt / n);

printf("Average turnaround time: %.2f\n", (float)total_tat / n);

int main() {

int n;

// Get the number of processes

printf("Enter the number of processes: ");

scanf("%d", &n);

int processes[n], burst_time[n];

// Get the burst time for each process

printf("Enter the burst time for each process:\n");

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

processes[i] = i + 1; // Process IDs are usually 1, 2, 3...

printf("Process %d: ", i + 1);

scanf("%d", &burst_time[i]);

// Calculate and print average times

findAverageTime(processes, n, burst_time);
return 0;

Output:-
Enter the number of processes: 3

Enter the burst time for each process:

Process 1: 5

Process 2: 8

Process 3: 3

Process Burst Time Waiting Time Turnaround Time

1 5 0 5

2 8 5 13

3 3 13 16

Average waiting time: 6.00

Average turnaround time: 8.00

Experiment no.5
Program Name:- Write a program to compare two Strings in C.

Theory:-

· To compare two strings in C, we can use the built-in function strcmp() (from
the string.h library) or write our own logic to compare each character of both
strings manually. Below is a C program that demonstrates both methods.

Method 1: Using strcmp() function


The strcmp() function compares two strings lexicographically:

· If both strings are equal, it returns 0.


· If the first string is lexicographically smaller, it returns a negative value.
· If the first string is lexicographically larger, it returns a positive value.

Program:-
#include <stdio.h>

#include <string.h>

int main() {

char str1[100], str2[100];

// Input two strings

printf("Enter first string: ");

gets(str1); // Gets the first string

printf("Enter second string: ");

gets(str2); // Gets the second string

// Compare the strings using strcmp

if (strcmp(str1, str2) == 0) {

printf("The strings are equal.\n");

} else {

printf("The strings are not equal.\n");

return 0;

Output:-

Enter first string: hello

Enter second string: hello

The strings are equal.

Output 2:-

Enter first string: hello

Enter second string: world


The strings are not equal.

Experiment no.6
Program Name:- Write a program to Find Length of the String in C.

Theory:-

· In C, you can find the length of a string using the standard library function
strlen(), or you can manually calculate the length by iterating through the
string. Below is a program that demonstrates both methods.

Method 1: Using strlen() Function

· The strlen() function from the string.h library returns the number of characters
in the string excluding the null terminator (\0).

Program:-
#include <stdio.h>

#include <string.h>

int main() {

char str[100];

// Input a string

printf("Enter a string: ");

gets(str); // gets() is used for input, though not safe in production code

// Use strlen() to find the length of the string

int length = strlen(str);

// Output the length of the string

printf("Length of the string is: %d\n", length);

return 0;
}

Output:-

Enter a string: Hello, World!

Length of the string is: 13

Experiment no.7
Program Name:- Write a program to copy the String in C.

Theory:-

· To copy a string in C, you can either use the built-in strcpy() function from the
string.h library or write your own function to manually copy the characters
from one string to another.
· Here’s how you can implement both approaches.

Method 1: Using strcpy() Function


· The strcpy() function is provided by the C standard library (string.h) to copy
the contents of one string into another. It copies the source string, including
the null terminator (\0), to the destination string.

Program:-
#include <stdio.h>

#include <string.h>

int main() {

char str1[100], str2[100];

// Input a string

printf("Enter the string: ");

gets(str1); // gets() is used for input, though not safe in production code

// Copy str1 to str2 using strcpy

strcpy(str2, str1);
// Output the copied string

printf("The copied string is: %s\n", str2);

return 0;

Output:-
Enter the string: Hello, World!

The copied string is: Hello, World!

Experiment no.8
Program Name:- Write a structure function in C.

Theory:-

Structures
· Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
· Unlike an array, a structure can contain many different data types (int, float,
char, etc.).

#include <stdio.h>

// Create a structure called myStructure

struct myStructure {

int myNum;

char myLetter;

};

int main() {

// Create a structure variable of myStructure called s1


struct myStructure s1;

// Assign values to members of s1

s1.myNum = 13;

s1.myLetter = 'B';

// Print values

printf("My number: %d\n", s1.myNum);

printf("My letter: %c\n", s1.myLetter);

return 0;

Output:-

My number: 13
My letter: B

Experiment no.9
Program Name:- Write a program of Round Robin in C.

Theory:-

· Round Robin is a CPU scheduling algorithm where each process is cyclically


assigned a fixed time slot. It is the preemptive version of the First come First
Serve CPU Scheduling algorithm.
· Round Robin CPU Algorithm generally focuses on Time Sharing technique.
· The period of time for which a process or job is allowed to run in a pre-
emptive method is called time quantum.

Program:-
#include <stdio.h>
#define MAX_PROCESSES 10

typedef struct {

int processID;

int burstTime;

int remainingTime;

} Process;

void roundRobin(Process processes[], int numProcesses, int quantum) {

int time = 0;

int completed = 0;

while (completed < numProcesses) {

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

if (processes[i].remainingTime > 0) {

if (processes[i].remainingTime <= quantum) {

time += processes[i].remainingTime;

processes[i].remainingTime = 0;

completed++;

printf("Process %d completed at time %d\n", processes[i].processID, time);

} else {

time += quantum;

processes[i].remainingTime -= quantum;

printf("Process %d executed for %d time units, remaining time: %d\n",

processes[i].processID, quantum, processes[i].remainingTime);

}
}

int main() {

Process processes[MAX_PROCESSES];

int numProcesses, quantum;

printf("Enter the number of processes: ");

scanf("%d", &numProcesses);

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

processes[i].processID = i + 1;

printf("Enter burst time for process %d: ", i + 1);

scanf("%d", &processes[i].burstTime);

processes[i].remainingTime = processes[i].burstTime;

printf("Enter the time quantum: ");

scanf("%d", &quantum);

roundRobin(processes, numProcesses, quantum);

return 0;

Output:-
Enter the number of processes: 3

Enter burst time for process 1: 5

Enter burst time for process 2: 8

Enter burst time for process 3: 7

Enter the time quantum: 3


Process 1 executed for 3 time units, remaining time: 2

Process 2 executed for 3 time units, remaining time: 5

Process 3 executed for 3 time units, remaining time: 4

Process 1 completed at time 11

Process 2 executed for 3 time units, remaining time: 2

Process 3 executed for 3 time units, remaining time: 1

Process 2 completed at time 17

Process 3 completed at time 18

Experiment no.10
Program Name:- Write a program of Linkedlist in C.

Theory:-

· A linked list is a linear data structure where each element, known as a node, is
connected to the next one using pointers. Unlike array, elements of linked list
are stored in random memory locations.

A linked list is a sequence of nodes where each node contains two parts:

· Data: The value stored in the node.


· Pointer: A reference to the next node in the sequence.
(There can be multiple pointers for different kind of linked list.)

Program:-
#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;
// Function to create a new node

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to insert a node at the end of the list

void insertNode(Node** head, int data) {

Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

// Function to delete a node with a specific value

void deleteNode(Node** head, int key) {

Node* temp = *head;

Node* prev = NULL;

// If head node itself holds the key to be deleted

if (temp != NULL && temp->data == key) {

*head = temp->next;
free(temp);

return;

// Search for the key to be deleted

while (temp != NULL && temp->data != key) {

prev = temp;

temp = temp->next;

// If the key was not present in the list

if (temp == NULL) return;

// Unlink the node from the linked list

prev->next = temp->next;

free(temp);

// Function to print the linked list

void printList(Node* head) {

Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

Node* head = NULL;


insertNode(&head, 10);

insertNode(&head, 20);

insertNode(&head, 30);

printf("Linked list after inserting nodes: ");

printList(head);

deleteNode(&head, 20);

printf("Linked list after deleting a node: ");

printList(head);

return 0;

Output:-

Linked list after inserting nodes: 10 -> 20 -> 30 -> NULL

Linked list after deleting a node: 10 -> 30 -> NULL

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