Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSC314 Data Structure Lab1

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 9

DSC314 Data structure

1.READ AND PRINT ARRAY ELEMENTS

#include <stdio.h>

int main()
{
int arr[10];
int i;

printf("\n\nRead and Print elements of an array:\n");


printf("-----------------------------------------\n");

printf("Input 10 elements in the array :\n");


for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}

printf("\nElements in array are: ");


for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
2.PASSING ARRAY TO FUNCTION
// Program to calculate the sum of array elements by passing to a function
#include <stdio.h>
float calculateSum(float num[]);
int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}
float calculateSum(float num[]) {
float sum = 0.0;
for (int i = 0; i < 6; ++i) {
sum += num[i];
}
return sum;
}

3. ADD TWO MATRICES


#include<stdio.h>

void addition(int r,int c,int a[10][10],int b[10][10]);


void main() {
int r, c, a[10][10], b[10][10],i, j;
printf("Enter the number of rows and columns of matrix: ");
scanf("%d %d",&r,&c);
printf("Enter the elements of first matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}
printf("Enter the elements of second matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
scanf("%d", &b[i][j]);
}
addition(r,c,a,b);

}
void addition(int r,int c,int a[10][10],int b[10][10])
{
int add[10][10],i,j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
add[i][j] = a[i][j] + b[i][j];
}
printf("Addition of two matrices: \n");
for (i = 0; i < r; i++){
for (j = 0; j < c; j++)
printf("%d\t", add[i][j]);
printf("\n");
}
}

4 .MULTIPLY TWO MATRICES


#include<stdio.h>
void multiply(int mat1[12][12],int mat2[12][12],int ,int ,int );

void main()
{
int mat1[12][12],mat2[12][12];
int i,j,k,m,n,p;
printf("Enter the number of rows and columns for 1st matrix\
n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the 1st matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}

//no of col of 1st mat = no of rows of 2nd mat


printf("Enter the number of columns for 2nd matrix\n");
scanf("%d",&p);
printf("Enter the elements of the 2nd matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&mat2[i][j]);
}
}

multiply(mat1,mat2,m,n,p);
}

void multiply(int mat1[12][12],int mat2[12][12],int m,int n,int p)


{
int mul[12][12],i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
{
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
}
}
}

printf("The resultant matrix formed on multiplying the two


matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
5.Selection sort

#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx,min_key;
for (i = 0; i <n-1; i++)
{
min_key=arr[i];
min_idx = i;
for (j = i+1; j <n; j++)
if (arr[j] < min_key)
{
min_key=arr[j];
min_idx = j;
}
swap(&arr[min_idx], &arr[i]);
}
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = {15, 8, 10, 6, 13,17};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

6.Insertion Sort

#include <stdio.h>

void insertionSort(int arr[], int n)


{
for (int i =1; i < n; i++) {
int next_key = arr[i];
int j = i - 1;
while (j >=0 && arr[j] >next_key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] =next_key;
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = { 15, 8, 10, 6, 13,17 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}

7.Bubble sort

#include <stdbool.h>
#include <stdio.h>

void swap(int* xp, int* yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n)


{
int i, j,first=0,last=n-1;
bool exchange = true;
while (exchange)
{

exchange = false;

for (i = last; i >first; i--)


{
if (arr[i] < arr[i-1])
{
swap(&arr[i], &arr[i-1]);
exchange = true;
}
}
first=first+1;

}
}

void printArray(int arr[], int size)


{
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = { 15, 8, 10, 6, 17, 13 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

You might also like