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

Lab Ass 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1 #include<stdio.

h>
#include<stdlib.h>
void sort(int *a,int n){
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (a[i]<a[j])
{
a[i]=a[i]+a[j];
a[j]=a[i]-a[j];
a[i]=a[i]-a[j];
}
}
}
}
int main ()
{
int n;
int *a;
printf("Enter the length of an array: ");
scanf("%d", &n);

printf("\nEnter the elements of an array: ");


a =(int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

sort(a,n);
printf("The 2nd largest number is = %d\n", a[1]);
printf("The 2nd smallest number is = %d\n", a[n - 2]);

free(a);
return 0;
}
2 #include<stdio.h>
#include<stdlib.h>

void swap(int *a, int *b);

void EvenOdd(int a[], int n)


{
int l = 0, r = n-1;
while (l < r)
{
while (a[l]%2 == 0 && l<r)
l++;

while (a[r]%2 == 1 && l<r)


r--;

if (l < r)
{
swap(&a[l], &a[r]);
l++;
r--;
}
}
}

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int *a,n,i=0;
printf("Enter the size of array: ");
scanf("%d",&n);
a =(int*)malloc(n*sizeof(int));
printf("Enter the numbers of array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
EvenOdd(a, n);
printf("Array after arranging \n");
for (i = 0; i <n; i++)
printf("%d ", a[i]);

free(a);
return 0;
}
3 #include<stdio.h>
#include<stdlib.h>

void selection_sort(int *arr,int n)


{
int i,j,temp;
for(i=0;i<n;i++)
{
int k=i;
for(j=i+1;j<n;j++)
{
if(arr[k]>arr[j])
{
temp=arr[k];
arr[k]=arr[j];
arr[j]=temp;
}
}
}
}

int main()
{
int *array,i,j,n;
printf("Enter the size of your array: ");
scanf("%d",&n);

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

printf("\nEnter the elements in your array:-\n");


for(i=0;i<n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&array[i]);
}

selection_sort(array,n);

printf("\nThe sorted array is:-\n");

for(i=0;i<n;i++)
printf("%d ",array[i]);
}
4 #include<stdio.h>
#include<stdlib.h>
void sort(int r,int c,int ** arr){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c/2;j++){
int temp=arr[i][j];
arr[i][j]=arr[i][c-1-j];
arr[i][c-1-j]=temp;
}
}
}
int main(){
int row,column;
printf("Enter no of rows: \n");
scanf("%d",&row);
printf("Enter no of columns: \n");
scanf("%d",&column);
int *arr[row];
for(int i=0;i<row;i++){
arr[i]=(int *)malloc(column*sizeof(int));
}
for(int i=0;i<row;i++){
printf("Enter elements of %d row: \n",i+1);
for(int j=0;j<column;j++){
scanf("%d",&arr[i][j]);
}
}
sort(row,column,arr);
printf("Matrix after required swapping is : \n");
for(int i=0;i<row;i++){
printf("\n");
for(int j=0;j<column;j++){
printf("%d",arr[i][j]);
}
}
return 0;
}
5 #include<stdio.h>
#include<stdlib.h>

typedef struct Matrix{


int rows;
int cols;
int**arr;
}matrix;

void printMatrix(matrix);
matrix matrixMultiply(matrix,matrix);
matrix fillMatrix();

int main()
{
//.WAP to find multiplication of two matrices(2D arays) and print the result.

matrix a,b;
printf("Enter for 1st matrix: \n");
a=fillMatrix();
printf("Enter for 2nd matrix: \n");
b=fillMatrix();

printf("The entered matrices are: \n");


printMatrix(a);
printf("\n");
printMatrix(b);

printf("Multiplying...\n");
matrix c=matrixMultiply(a,b);
printMatrix(c);

}
matrix fillMatrix()
{
matrix m;
printf("Enter rows and cols: ");
scanf("%d %d",&m.rows,&m.cols);
m.arr=(int*)malloc(m.rows*sizeof(int));
for(int i=0;i<m.rows;i++){
m.arr[i]=(int*)malloc(m.cols*sizeof(int));
}
printf("Enter %d integers: ",m.rows*m.cols);
for(int i=0;i<m.rows;i++){
for(int j=0;j<m.cols;j++){
scanf("%d",&m.arr[i][j]);
}
}
return m;
}
matrix matrixMultiply(matrix a,matrix b)
{
matrix c;
if(a.cols != b.rows){
printf("Incompatible...\n");
exit(0);
}
c.rows=a.rows;
c.cols=b.cols;
c.arr=(int*)malloc(c.rows*sizeof(int));
for(int i=0;i<c.rows;i++){
c.arr[i]=(int*)malloc(c.cols*sizeof(int));
}
for(int i=0;i<c.rows;i++){
for(int j=0;j<c.cols;j++){
c.arr[i][j]=0;
for(int k=0;k<a.cols;k++){
c.arr[i][j]+=a.arr[i][k]*b.arr[k][j];
}
}
}
return c;
}
void printMatrix(matrix m)
{
for(int i=0;i<m.rows;i++){
for(int j=0;j<m.cols;j++){
printf("%3d",m.arr[i][j]);
}
printf("\n");
}
}

You might also like