0% found this document useful (0 votes)
34 views

Array Problem Assignment Nowrin

The documents contain C code examples for array operations including insertion, deletion, sorting arrays, and matrix operations like addition and subtraction. The examples demonstrate: 1) Inserting an element into an array at a given position and shifting the other elements; 2) Deleting an element from an array at a given position and shifting the other elements; 3) Sorting arrays using different sorting algorithms like insertion sort, bubble sort, and selection sort. 4) Performing addition and subtraction on 2D arrays or matrices of a given size; 5) Basic operations like swapping two integers and searching an array using the binary search algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Array Problem Assignment Nowrin

The documents contain C code examples for array operations including insertion, deletion, sorting arrays, and matrix operations like addition and subtraction. The examples demonstrate: 1) Inserting an element into an array at a given position and shifting the other elements; 2) Deleting an element from an array at a given position and shifting the other elements; 3) Sorting arrays using different sorting algorithms like insertion sort, bubble sort, and selection sort. 4) Performing addition and subtraction on 2D arrays or matrices of a given size; 5) Basic operations like swapping two integers and searching an array using the binary search algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

01.

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:

02. Array Deletion:


#include <stdio.h>
int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

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

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


scanf("%d", &array[c]);

printf("Enter the location where you wish to delete element\n");

1
scanf("%d", &position);

if (position >= n+1)


printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];

printf("New array after deleting:\n");

for (c = 0; c < n - 1; c++)


printf("%d\n", array[c]);
}

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;

printf("\nRead a 2D array of size 4x4 and print the matrix :\n");


printf("########################################################\n");

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]);
}
}

printf("\nThe matrix is : \n");


for(i=0;i<4;i++)
{
printf("\n");
for(j=0;j<4;j++)
printf("%d\t",arry[i][j]);
}
printf("\n\n");

return 0;
}
Output:

05. Addition of two matrix


#include <stdio.h>
/*Matrix Addition*/
int main()
{
int a[10][10], b[10][10], sum[10][10],r,c,i,j;
printf("Enter Row and Column number: \n");
//scan row and column
scanf("%d %d",&r,&c);

printf("Your Entered Row %d and Column %d\n", r, c);

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("The addition of two matrix is: \n");

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


{
printf("\n");
for (j = 0; j < c; j++)
{
printf("%d\t", sum[i][j]);
}

}
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);

printf("Your Entered Row %d and Column %d\n", r, c);


//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++)

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");

printf("\nThe second matrix is : \n");


for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("%d\t",b[i][j]);
}
printf("\n\n");

//Subtraction of two array


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
sub[i][j]= a[i][j]-b[i][j];
}

}
//Now print the subtraction of a matrix

printf("The subtraction of two matrix is: \n");

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


{
printf("\n");
for (j = 0; j < c; j++)
{
printf("%d\t", sub[i][j]);
}

}
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;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

for (c = 1 ; c <= n - 1; c++) {


t = array[c];

for (d = c - 1 ; d >= 0; d--) {


if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
}
else

8
break;
}
if (flag)
array[d+1] = t;
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}
Output

08. Bubble sort program


/* Bubble sort code */
#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}

9
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}
Output

09. Selection Sort in C


#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times


{
position = c;

for (d = c + 1; d < n; d++)


{
if (array[position] > array[d])
position = d;
}

10
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}
Output

10. Swap Two integer


#include <stdio.h>
int main()
{
int x, y, t;

printf("Enter two integers\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

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];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
Output:

12
13

You might also like