Lab Ass 3
Lab Ass 3
Lab Ass 3
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);
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>
if (l < r)
{
swap(&a[l], &a[r]);
l++;
r--;
}
}
}
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>
int main()
{
int *array,i,j,n;
printf("Enter the size of your array: ");
scanf("%d",&n);
array=(int*)malloc(n*sizeof(int));
selection_sort(array,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>
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("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");
}
}