0% found this document useful (0 votes)
43 views35 pages

List of Practicals of Algorithm Semester-1 Part 2

The document discusses several C programs related to data structures and algorithms: 1) It defines a student structure with name, date of birth, and salary fields, and demonstrates creating a structure variable and printing its data. 2) It provides C code examples for common sorting algorithms like bubble, selection, and insertion sort on integer arrays. 3) It shows linear and binary search algorithms to find a target number within a sorted integer array. 4) It demonstrates defining a structure with employee details like name, ID, and nested date structures for DOB and DOJ, and printing an employee record.

Uploaded by

Nachiket Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views35 pages

List of Practicals of Algorithm Semester-1 Part 2

The document discusses several C programs related to data structures and algorithms: 1) It defines a student structure with name, date of birth, and salary fields, and demonstrates creating a structure variable and printing its data. 2) It provides C code examples for common sorting algorithms like bubble, selection, and insertion sort on integer arrays. 3) It shows linear and binary search algorithms to find a target number within a sorted integer array. 4) It demonstrates defining a structure with employee details like name, ID, and nested date structures for DOB and DOJ, and printing an employee record.

Uploaded by

Nachiket Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

A structure to contain name, date of joining (date, month & year) and salary

#include <stdio.h>

#include <conio.h>

int main()

struct student

char name[80];

float salary;

char DOB[80];

};

struct student stud1;

printf("\n Enter the name : ");

scanf("%s", stud1.name);

printf("\n Enter the salary : ");

scanf("%f", &stud1.salary);

printf("\n Enter the DOB : ");

scanf("%s", stud1.DOB);

printf("\n ********STUDENT'S DETAILS *******");

printf("\n NAME = %s", stud1.name);

printf("\n SALARY = %f", stud1.salary);


printf("\n DOB = %s", stud1.DOB);

getch();

return 0;

}
Bubble sort

#include <stdio.h>

#include <conio.h>

#include <string.h>

int main()

int arr[5]={25,7,31,13,2};

int i, j, temp;

system ("cls");

printf("Bubble sort.\n\n");

printf("Array before sorting:\n");

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

printf("%d\t",arr[i]);

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

for(j=0; j<=3-i; j++)

if(arr[j]>arr[j+1])

{
temp= arr[j];

arr[j]= arr[j+1];

arr[j+1]= temp;

printf("\n");

printf("Array after sorting:\n");

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

printf("%d\t",arr[i]);

return 0;

}
Selection sort

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

int arr[5]={25,17,31,2};

int i,j,temp;

printf("Selection Sort.\n\n");

printf("Array before sorting:\n");

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

printf("%d\t",arr[i]);

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

for(j=i+1;j<=4;j++)
{

if(arr[i]>arr[j])

temp= arr[i];

arr[i]=arr[j];

arr[j]=temp;

printf("\n");

printf("Array after sorting:\n");

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

printf("%d\t",arr[i]);

return 0;

}
Insertion Sort

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

int arr[5]={1,2,3,4,5};

int i,j,k,temp;

printf("Insertion sort.\n\n");

printf("Array before sorting:\n");

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

printf("%d\t",arr[i]);

for(i=1;i<=4;i++)

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

if(arr[j]>arr[i])

temp=arr[j];
arr[j]=arr[i];

for(k=i;k>j;k--)

arr[k]=arr[k-1];

arr[k+1]=temp;

printf("\n");

printf("Array after sorting:\n");

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

printf("%d\t",arr[i]);

return 0;

}
Linear Search

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

int arr[10]={11,2,9,4,5,6,7,8,9,10};

int i,num;

printf("Enter number to Search: ");

scanf("%d",&num);

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

if(arr[i]==num)

break;

if(i==10)

printf("Number is not present in the array.\n");

else

printf("The number is at position %d in the array.\n",i);

return 0;
}
Binary Search

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

int arr[10]={1,2,3,4,5,6,7,8,9,90};

int mid,lower=0,upper=9,num,flag=1;

printf("Enter number to search:");

scanf("%d",&num);

for(mid=(lower+upper)/2;lower<=upper;mid=(lower-upper)/2)

if(arr[mid]==num)

printf("The number is at position %d in the array.\n",mid);

flag=0;

break;

if(arr[mid]>num)

upper=mid-1;

else
lower=mid+1;

if(flag)

printf("Element is not present in array.\n");

return 0;

#include <stdio.h>

struct student {

char firstName[50];

int roll;

float marks;

} s[5];

int main() {

int i;

printf("Enter information of students:\n");

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

s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);

printf("Enter first name: ");

scanf("%s", s[i].firstName);

printf("Enter marks: ");

scanf("%f", &s[i].marks);

printf("Displaying Information:\n\n");

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

printf("\nRoll number: %d\n", i + 1);

printf("First name: ");

puts(s[i].firstName);

printf("Marks: %.1f", s[i].marks);

printf("\n");

return 0;

}
Accessing an array using malloc

#include <stdio.h>

#include <stdlib.h>

int main()

{
int* ptr;

int n, i;

printf("Enter number of elements:");

scanf("%d",&n);

printf("Entered number of elements: %d\n", n);

ptr = (int*)malloc(n * sizeof(int));

if (ptr == NULL) {

printf("Memory not allocated.\n");

exit(0);

else {

printf("Memory successfully allocated using malloc.\n");

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

ptr[i] = i + 1;
}

// Print the elements of the array

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

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

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

return 0;

Accessing an array using calloc

#include <stdio.h>

#include <stdlib.h>

int main()

{
// This pointer will hold the

// base address of the block created

int* ptr;

int n, i;

// Get the number of elements for the array

n = 5;

printf("Enter number of elements: %d\n", n);

// Dynamically allocate memory using calloc()

ptr = (int*)calloc(n, sizeof(int));

// Check if the memory has been successfully

// allocated by calloc or not

if (ptr == NULL) {

printf("Memory not allocated.\n");

exit(0);

else {

// Memory has been successfully allocated

printf("Memory successfully allocated using calloc.\n");

// Get the elements of the array


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

ptr[i] = i + 1;

// Print the elements of the array

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

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

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

return 0;

#include<stdio.h>

#include<conio.h>

struct Date

{
int mm,dd,yy;

};

struct Employee

char name[50];

int emp_id;

struct Date DOB,DOJ;

};

main()

{ int i;

struct Employee e;

clrscr();

printf("\nEnter name : ");

scanf("%s",&e.name);

printf("\nEnter emp_id. : ");

scanf("%d",&e.emp_id);

printf("\nEnter Date of Joining\n ");

printf("(dd-mm-yy) : ");

scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);

printf("\nEnter Date of birth\n ");

printf("(dd-mm-yy) : ");

scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);

printf("\nName : %s",e.name);

printf("\nEmployee ID : %d",e.emp_id);

printf("\nEnter DOJ : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);


printf("\nEnter DOB : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);

getch();

Accessing an array using malloc and re allocate using realloc

#include <stdio.h>

#include <stdlib.h>

int main()

int index = 0, i = 0, n,

*marks; // this marks pointer hold the base address

// of the block created

int ans;

marks = (int*)malloc(sizeof(

int)); // dynamically allocate memory using malloc

// check if the memory is successfully allocated by

// malloc or not?

if (marks == NULL) {

printf("memory cannot be allocated");

}
else {

// memory has successfully allocated

printf("Memory has been successfully allocated by "

"using malloc\n");

printf("\n marks = %pc\n",

marks); // print the base or beginning

// address of allocated memory

do {

printf("\n Enter Marks\n");

scanf("%d", &marks[index]); // Get the marks

printf("would you like to add more(1/0): ");

scanf("%d", &ans);

if (ans == 1) {

index++;

marks = (int*)realloc(

marks,

(index + 1)

* sizeof(

int)); // Dynamically reallocate

// memory by using realloc

// check if the memory is successfully

// allocated by realloc or not?

if (marks == NULL) {

printf("memory cannot be allocated");


}

else {

printf("Memory has been successfully "

"reallocated using realloc:\n");

printf(

"\n base address of marks are:%p",

marks); ////print the base or

///beginning address of

///allocated memory

} while (ans == 1);

// print the marks of the students

for (i = 0; i <= index; i++) {

printf("marks of students %d are: %d\n ", i,

marks[i]);

free(marks);

return 0;

}
#include <stdio.h>

#include<conio.h>

struct marks

int subl, sub2, sub3;

int total;

};

main()

{
int i; //Declaring and initializing array of structures of type 'marks' . . . .

struct marks student[3] = {45,67,81,0}, {75,53,69,0}, { 57,36,71,0};

struct marks totals; // One more structure variable 'totals' to store totals

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

//Computing totals for each student

student[i].total = student[i].sub1 + student[i].sub2 + student[i].sub3;

// Computing subject-wise totals and storing in 'totals'

totals.sub1 = totals.sub1 + student[i].sub1;

totals.sub2 = totals.sub2 + student[i].sub2;

totals.sub3 = totals.sub3 + student[i].sub3;

totals.total = totals.total + student[i].total;

//Printing totals for each student

printf("\nstudent total\n\n");

for(i = 0; i <= 2, i++)

printf ("student[%d] %fin",i+1,student[i].total);

//Printing totals for each subject

printf('\n Subject total\n\n");

printf("%s\t %d\n %s\t %d\n %s\t %d\n", "subject1", totals.sub1, "subject2", totals.sub2, "subject3",
totals.sub3);

printf("\n Grand total = %d\n", totals.total);

}
d) Thus, free command is very important to de-allocate the memory from all the 3 previous programs.
b. Write a program to insert data at the end of the array.

#include <stdio.h>

void main()

int position, i, n, value,ch, arr[100];

printf("C Program to insert element at end of Array\n");

printf("First enter number of elements you want in Array\n");

scanf("%d", &n);

arr[n];

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

printf("Please give value for index %d : ",i);

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

printf("Let's Insert Element at end \n ");

printf("Please give a number to insert at end \n");

scanf("%d", &value);

arr[n] = value;

printf("Element %d is inserted at %d index \n",value,n);

printf("New Array is \n ");

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

printf("%d \t",arr[i]);

}
}

Write a program to insert data at the beginning of the array.

#include <stdio.h>

void main()

int position, i, n, value,ch, arr[100];

printf("C Program to insert element at end of Array\n");

printf("First enter number of elements you want in Array\n");

scanf("%d", &n);

arr[n];

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


{

printf("Please give value for index %d : ",i);

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

printf("Let's Insert Element at beginning \n ");

printf("Please give a number to insert at beginning \n");

scanf("%d", &value);

arr[0] = value;

printf("Element %d is inserted at %d index \n",value,0);

printf("New Array is \n ");

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

printf("%d \t",arr[i]);

}
Write a program to insert data after any particular location of an array.

#include<stdio.h>

#define size 5

int main()

int arr[size] = {1, 20, 5, 78, 30};

int element, pos, i;

printf("Enter position and element\n");

scanf("%d%d",&pos,&element);
if(pos <= size && pos >= 0)

for(i = size; i > pos; i--)

arr[i] = arr[i-1];

arr[pos] = element;

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

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

else

printf("Invalid Position\n");

return 0;

}
Write a program to delete data after any particular location of an array.

#include<stdio.h>

void main()

int a[100],i,n,pos;

printf("\nEnter no of elements\n");

scanf("%d",&n);

printf("Enter the elements\n");

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

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

printf("Elements of array are\n");

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

printf("a[%d] = %d\n",i,a[i]);

printf("Enter the position from which the number has to be deleted\n");

scanf("%d",&pos);

for(i=pos;i<n-1;i++)
{

a[i]=a[i+1];

n=n-1;

printf("\nOn Deletion, new array we get is\n");

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

printf("a[%d] = %d\n",i,a[i]);

}
Write a program to delete data at the end of the array.

#include <stdio.h>

void main()

int position, i, n, value;

printf("C Program to delete element at end of Array\n");


printf("First enter number of elements you want in Array\n");

scanf("%d", &n);

int arr[n];

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

printf("Please give value for index %d : ",i);

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

value=arr[n-1]; //assigning last value in value variable

printf("Element %d is deleting at %d index \n",value,n-1);

n=n-1;//here decreasing value to reduce size of array

printf("New Array after deleting element at end \n ");

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

printf("%d \t",arr[i]);

}
5: d)Write a program to delete data at the beginning of the array.

Doubt

Confused in taking assigning variable to n for new array.

Note: All programs are tried by me. Thank you, Sir.

You might also like