Array Problem Assignment Nowrin
Array Problem Assignment Nowrin
Array Insertion
#include<stdio.h>
int main(){
int student[40],pos,i,size,value;
printf("enter no of elements in array of students:");
scanf("%d",&size);
printf("enter %d elements are:\n",size);
for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that poition:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value is\n");
for(i=0;i<=size;i++)
printf("%d\n",student[i]);
return 0;
}
Output:
1
scanf("%d", &position);
return 0;
}
Output:
03. Write a program in C to insert the values in the array (sorted list).
#include <stdio.h>
int main()
{
int arr1[100],i,n,p,inval;
printf("\n\nInsert New value in the sorted array :\n");
printf("-----------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
/* Stored values into the array*/
printf("Input %d elements in the array in ascending order:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("Input the value to be inserted : ");
2
scanf("%d",&inval);
printf("The exist array list is :\n");
for(i=0;i<n;i++)
printf("% 5d",arr1[i]);
/* Determine the position where the new value will be insert.*/
for(i=0;i<n;i++)
{
if(inval<arr1[i])
{
p = i;
break;
}
else
{
p=i+1;
}
}
/* move all data at right side of the array */
for(i=n+1;i>=p;i--)
arr1[i]= arr1[i-1];
/* insert value at the proper position */
arr1[p]=inval;
printf("\n\nAfter Insert the list is :\n");
for(i=0;i<=n;i++)
printf("% 5d",arr1[i]);
printf("\n");
}
Output
04. Write a program in C for a 2D array of size 4x4 and print the matrix.
#include <stdio.h>
int main()
{
int arry[4][4],i,j;
3
printf("Input elements in the matrix :\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arry[i][j]);
}
}
return 0;
}
Output:
4
//input value for first array
printf("Enput the value for first matrix: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("Element- [%d],[%d] : ", i, j);
scanf("%d",&a[i][j]);
}
}
//input value for second array
printf("Enput the value for second matrix: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("Element- [%d],[%d] : ", i, j);
scanf("%d", &b[i][j]);
}
}
//Add two array
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
sum[i][j]= a[i][j] + b[i][j];
}
}
//Now print the sum of a matrix
}
printf("\n");
return 0;
}
Output:
5
06. Subtraction of two matrix
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], sub[10][10],r,c,i,j;
printf("Enter Row and Column number: \n");
//scan row and column
scanf("%d %d",&r,&c);
}
//input value for second array
printf("Enput the value for second matrix: \n");
for (i = 0; i < r; i++)
6
{
for (j = 0; j < c; j++)
{
printf("Element- [%d],[%d] : ", i, j);
scanf("%d", &b[i][j]);
}
}
//Show two matrix which inputed:
printf("\nThe first matrix is : \n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
}
printf("\n\n");
}
//Now print the subtraction of a matrix
}
printf("\n");
return 0;
}
Output
7
07. Insertion sort
/* Insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
8
break;
}
if (flag)
array[d+1] = t;
}
return 0;
}
Output
int main()
{
int array[100], n, c, d, swap;
9
}
}
return 0;
}
Output
10
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}
return 0;
}
Output
t = x;
x = y;
y = t;
printf("\n\n");
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
return 0;
}
Output
11
11. Binary Search:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Output:
12
13