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

Unit II Progrms to Upload

Uploaded by

Nan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit II Progrms to Upload

Uploaded by

Nan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

// print the first element of the array

printf("%d", mark[0]);

// print the third element of the array

printf("%d", mark[2]);

// Program to find the average of n numbers using arrays

#include <stdio.h>

int main()

int marks[10], i, n;

float average,sum = 0.0;

printf("Enter number of elements: ");

scanf("%d", &n);

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

printf("Enter number%d: ",i+1);

scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable

sum += marks[i];

average = sum/n;

printf(“Sum =%f\n”,sum);

printf("Average = %.2f", average);

return 0;

//Inserting an Element into the Array

#include <stdio.h>

int main()

int array[100], position, i, n, value;

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

scanf("%d", &n);

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

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

scanf("%d", &array[i]);
printf("Enter the location where you wish to insert an element\n");

scanf("%d", &position);

printf("Enter the value to insert\n");

scanf("%d", &value);

for (i = n - 1; i >= position - 1; i--)

array[i+1] = array[i];

array[position-1] = value;

printf("Resultant array is\n");

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

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

return 0;

Remove element from array C program


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

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


scanf("%d", &n);

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


for (i = 0; i < n; i++)
scanf("%d", &array[i]);

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


scanf("%d", &position);

if (position >= n+1)


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

printf("Resultant array:\n");

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


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

return 0;
}

Find the Largest Element in an array

#include <stdio.h>

int main()

int i, n;

float arr[100];

printf("Enter the number of elements (1 to 100): ");


scanf("%d", &n);

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

printf("Enter number%d: ", i + 1);

scanf("%f", &arr[i]);

// storing the largest number to arr[0]

for (i = 1; i < n; i++)

if (arr[0] < arr[i])

arr[0] = arr[i];

printf("Largest element = %.2f", arr[0]);

return 0;

}
Read n number of values in an array and display it in reverse
order

#include <stdio.h>

void main()

int i,n,a[100];

printf("Input the number of elements to store in the array :");

scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);

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

printf("element - %d : ",i+1);

scanf("%d",&a[i]);

printf("\nThe values store into the array are : \n");

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

printf("%d",a[i]);

}
printf("\n\nThe values store into the array in reverse are :\n");

for(i=n-1;i>=0;i--)

printf("% d",a[i]);

printf("\n\n");

Copy the elements of one array into another array


#include <stdio.h>

void main()

int arr1[100], arr2[100];

int i, n;

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

printf("element - %d : ",i+1);
scanf("%d",&arr1[i]);

/* Copy elements of first array into second array.*/

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

arr2[i] = arr1[i];

/* Prints the elements of first array */

printf("\nThe elements stored in the first array are :\n");

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

printf("% d", arr1[i]);

/* Prints the elements copied into the second array. */

printf("\n\nThe elements copied into the second array are


:\n");

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

printf("% d", arr2[i]);


}

printf("\n\n");

Find the maximum and minimum element in an array

#include <stdio.h>

void main()

int arr1[100];

int i, mx, mn, n;

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

printf("element - %d : ",i);

scanf("%d",&arr1[i]);

mx = arr1[0];
mn = arr1[0];

for(i=1; i<n; i++)

if(arr1[i]>mx)

mx = arr1[i];

if(arr1[i]<mn)

mn = arr1[i];

printf("Maximum element is : %d\n", mx);

printf("Minimum element is : %d\n\n", mn);

Sort elements of array in ascending order(BUBBLE


SORT)
#include <stdio.h>

void main()
{

int arr1[100];

int n, i, j, tmp;

printf("Input the size of array : ");

scanf("%d", &n);

printf("Input %d elements in the array :\n",n);

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

printf("element - %d : ",i);

scanf("%d",&arr1[i]);

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

for(j=i+1; j<n; j++)

if(arr1[j] <arr1[i])

tmp = arr1[i];

arr1[i] = arr1[j];
arr1[j] = tmp;

printf("\nElements of array in sorted ascending order:\n");

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

printf("%d ", arr1[i]);

printf("\n\n");

Linear search program in C

#include <stdio.h>

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

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


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);


for (i = 0; i < n; i++)
scanf("%d", &array[i]);

printf("Enter a number to search\n");


scanf("%d", &search);

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


{
if (array[i] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);

return 0;
}

Binary search program in C


#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 (i = 0; i < n; i++)


scanf("%d", &array[i]);
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;
}
2D ARRAYS

ACCESSING 2D ARRAY ELEMENTS

#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++) a[0][0] a[0][1] a[0]2]
{ a[1][0] a[1][1] a[1][2]
for (j=0;j<3;j++) a[2][0] a[2][1] a[2][2]
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}

MATRIX ADDITION

#include <stdio.h>

void main()
{

int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;

printf("\n\nAddition of two Matrices :\n");

printf("------------------------------\n");

printf("Input the size of the square matrix (less than 5): ");

scanf("%d", &n);

/* Stored values into the array*/

printf("Input elements in the first matrix :\n");

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

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

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&arr1[i][j]);

printf("Input elements in the second matrix :\n");

for(i=0;i<n;i++)
{

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

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&brr1[i][j]);

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

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

printf("\n");

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

printf("%d\t",arr1[i][j]);

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

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

printf("\n");

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

/* calculate the sum of the matrix */

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

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

crr1[i][j]=arr1[i][j]+brr1[i][j];

printf("\nThe Addition of two matrix is : \n");

for(i=0;i<n;i++){

printf("\n");

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

printf("%d\t",crr1[i][j]);

printf("\n\n");

TRANSPOSE OF A MATRIX

#include <stdio.h>

void main()

int arr1[50][50],brr1[50][50],i,j,r,c;
printf("\n\nTranspose of a Matrix :\n");

printf("---------------------------\n");

printf("\nInput the rows and columns of the matrix : ");

scanf("%d %d",&r,&c);

printf("Input elements in the first matrix :\n");

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

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

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&arr1[i][j]);

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

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

printf("\n");

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

printf("%d\t",arr1[i][j]);
}

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

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

brr1[j][i]=arr1[i][j];

printf("\n\nThe transpose of a matrix is : ");

for(i=0;i<c;i++){

printf("\n");

for(j=0;j<r;j++){

printf("%d\t",brr1[i][j]);

printf("\n\n");

}
//Print the number of positive and negative values present in
the array

#include<stdio.h>

int main()

int Size, i, a[10];

int Positive_Count = 0, Negative_Count = 0;

printf("\n Please Enter the Size of an Array : ");

scanf("%d", &Size);

printf("\nPlease Enter the Array Elements\n");

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

scanf("%d", &a[i]);

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

if(a[i] >= 0)

Positive_Count++;

}
else

Negative_Count++;

printf(“\n Total Number of Positive Numbers in this Array


= %d “, Positive_Count);

printf(“\n Total Number of Negative Numbers in this Array


= %d “, Negative_Count);

return 0;

Diagonal matrix
Any given square matrix where all the elements are zero except for the elements that
are present diagonally is called a diagonal matrix.

Eg) 11 0 0
0 22 0
0 0 44

#include <stdio.h>

#define MAXROWS 10

#define MAXCOLS 10

int main()
{
int i, j, row, col, flag = 0;
int matrix[MAXROWS][MAXCOLS];
/* get the number of rows from the user */
printf("Enter the number of rows:");
scanf("%d", &row);
/* get the number of columns from the user */
printf("Enter the number of columns:");
scanf("%d", &col);
/* Boundary Check */
if (row > MAXROWS || row < 0 || col > MAXCOLS ||
col < 0)

{
printf("Boundary Level Exceeded!!\n");
return 0;

/* get the entries for the input matrix */

printf("\nEnter the matrix entries:\n");

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

for (j = 0; j < col; j++)

scanf("%d", &matrix[i][j]);
}
}

/* checking for diagonal matrix */

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

for (j = 0; j < col; j++)

if (i != j && matrix[i][j] != 0)

flag = 1;

goto end;

end:

/* printing the result */

if (flag)

printf("Given Matrix is not a diagonal matrix!!\n");

}
else

printf("Given Matrix is a diagonal matrix!!\n");

return 0;

//MATRIX MULTIPLICATION

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first
matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++)

{
for (d = 0; d < q; d++)

{
for (k = 0; k < p; k++)

{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++)

{
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}

You might also like