1.
Program to insert an element into an Array
1. Declare a one-dimensional array of some fixed capacity.
2. Take size of the array as input from users
3. Define array elements, taking each element as input from users.
4. Get the number to be inserted.
5. Get the position where element needs to be inserted.
6. From that position shift all elements to one position forward.
7. Insert the element in that position.
8. Exit.
Code
#include <stdio.h>
void main()
{
int array[100];
int i, n, x, pos;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are: \n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be inserted: ");
scanf("%d", &pos);
//shift all elements 1 position forward from the place
//where element needs to be inserted
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
array[pos-1]=x; //Insert the element x on the specified
position
//print the new array
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
}
2. C Program to Search an Element in an Array
[Link] an array of some certain size and define its elements in
sorted fashion.
2. Now take an input from the users which you want to search
for.
3. Take two variables pointing to the first and last index of the
array (namely low and high)
4. Run start a while and run it until low equals high.
5. Now, take a mid of low and high value, check whether value
at mid equals the user input.
6. In case it matches the user input, it means we found the
number, after which we must break out from the loop.
7. And if user input is greater than value at mid, then low is
assigned the value of mid. Similarly if user input is smaller than
value at mid, then high is assigned the value of mid.
8. In this way, the region of finding the user input becomes half.
Code:
#include <stdio.h>
void main()
{
int array[20];
int i, low, mid, high, key, size;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the key\n");
scanf("%d", &key);
/* search begins */
low = 0;
high = (size - 1);
while (low <= high)
{
mid = (low + high) / 2;
if (key == array[mid])
{
printf("SUCCESSFUL SEARCH\n");
return;
}
if (key < array[mid])
high = mid - 1;
else
low = mid + 1;
printf("UNSUCCESSFUL SEARCH\n");
3.C Program to Sort an Array in Descending Order
Code
include <stdio.h>
void main ()
{
int number[30];
int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
/* sorting begins ... */
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are
given below\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
4. C program to merge two sorted array in ascending order
[Link] the program
2. Input the length of both the arrays.
3. Input the arrays elements from user.
4. Copy the elements of the first array to the merged array
when initialising it.
5. Copy the elements of the second array to the merged
array while initialising the second array.
6. Sort the merged array now.
7. Display the merged array.
8. The program ends here.
Code
#include <stdio.h>
int main() {
//Declaring the size of arrays
int s1, s2, s3;
printf("\n Enter the size of 1st array ");
scanf("%d", & s1);
printf("\n Enter the size of 2nd array ");
scanf("%d", & s2);
s3 = s1 + s2;
printf("\n Enter the elements of 1st array\n");
// Declaring the array
int arr1[s1], arr2[s2], arr3[s3];
//Initialising the array
for (int i = 0; i < s1; i++) {
scanf("%d", & arr1[i]);
arr3[i] = arr1[i];
}
int k = s1;
printf("\nEnter the elements of 2nd array \n");
for (int i = 0; i < s2; i++) //Array Initialised
{
scanf("%d", & arr2[i]);
arr3[k] = arr3[i];
k++;
}
printf("\nThe merged array before sorting : \n\t");
for (int i = 0; i < s3; i++)
printf("%d ", arr3[i]); //Print the merged array before sorting
printf("\n The merged array after sorting\n\t");
for (int i = 0; i < s3; i++) //Sorting the array
{
int tem;
for (int j = i + 1; j < s3; j++) {
if (arr3[i] > arr3[j]) {
tem = arr3[i];
arr3[i] = arr3[j];
arr3[j] = tem;
}
}
}
for (int i = 0; i < s3; i++) //Printing the sorted Array
{
printf(" %d ", arr3[i]);
}
}
5. C Programming Matrix Multiplication
Algorithm
1. Start.
2. Enter the value of m and n (or) order of the first matrix.
3. Enter the value of p and q (or) order of the second matrix.
4. Create a matrix of size a[m][n] and b[p][q].
5. Enter the element of matrices row-wise using loops.
6. If the number of columns of the first matrix is not equal to
the number of rows of the second matrix, print matrix
multiplication is not possible and exit. If not, proceed to the
next step.
7. Create a third matrix, c of size m x q, to store the product.
8. Set a loop from i=0 to i=m.
9. Set an inner loop for the above loop from j=0 to j=q.
10. Initialise the value of the element (i, j) of the new
matrix to 0.
11. Set an inner loop inside the above loop from k=0 to
k=p.
12. Using the add and assign operator (+=) store the value
of a[i][k] * b[k][j] in the third matrix, c[i][j].
13. Print the third matrix.
14. Stop.
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
printf("Enter the value of N (N <= 10): ");
scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}
printf("Enter the elements of Matrix-B: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("The product of the two matrices is: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
6. C program to perform Addition of two matrices
include < stdio.h >
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", & first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", & second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}