0% found this document useful (0 votes)
60 views22 pages

Devesh

The document contains C code for performing various operations on 1D and 2D arrays, including: 1) Functions to calculate the sum, average, minimum, maximum and count of elements greater than a threshold for a 1D array. 2) Functions to reverse a 1D array, add/subtract/multiply matrices, and calculate the row/column sums and averages for a 2D array. 3) Recursive functions to calculate the sum of numbers from 0 to n, convert between number bases, sum the digits of a number, and count the 1s in the binary representation of a number. 4) Binary search and polynomial evaluation using Horner's method on a 1D array.

Uploaded by

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

Devesh

The document contains C code for performing various operations on 1D and 2D arrays, including: 1) Functions to calculate the sum, average, minimum, maximum and count of elements greater than a threshold for a 1D array. 2) Functions to reverse a 1D array, add/subtract/multiply matrices, and calculate the row/column sums and averages for a 2D array. 3) Recursive functions to calculate the sum of numbers from 0 to n, convert between number bases, sum the digits of a number, and count the 1s in the binary representation of a number. 4) Binary search and polynomial evaluation using Horner's method on a 1D array.

Uploaded by

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

 Write the functions for following  operations on 1D array

 sum & average of elements


 min/max element in array
 reverse the array
 Count the elements which are greater than certain
threshold value
#include<stdio.h>
int sa(int a[],int n)
{
int i,sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("Sum is %d\n",sum);
printf("Average is %d\n",sum/n);
}
int sortt(int a[],int n)
{
int min,max,i,j;
max = a[0];
min = a[0];
for(i=1; i<n; i++)
{
if(a[i] > max)
{
max = a[i];
}
if(a[i] < min)
{
min = a[i];
}
}
printf("max is %d",max);
printf("\nmin is %d\n",min);
}
int rev(int a[], int n)
{
printf("Original array: \n");
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}

printf("\nArray in reverse order: \n");


for (int i = n-1; i >= 0; i--)
{
printf("%d\n ", a[i]);
}
}
int thres(int a[], int n)
{
int th;
printf("Enter the threshold value:\n");
scanf("%d", & th);
printf("Elements greater than threshold value are:\n");
for(int i=0; i<n;i++)
{
if(a[i]>th)
{
printf("\n%d",a[i]);
}
}
}
int main()
{
int i,a[10],n;
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sa(a,n);
sortt(a,n);
rev(a,n);
thres(a,n);

 Write the functions for following  operations on 2D array


 Addition of two matrices
 Trace of the matrix
#include<stdio.h>
int add(int a[2][2],int b[2][2])
{
int c[2][2];
printf("The sum of the matrices is:\n");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j] = a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
}

}
int trace(int a[2][2], int b[2][2])
{
int sum1=0, sum2=0;
for(int i=0;i<2;i++)
{
sum1=sum1+a[i][i];
}
printf("\nThe trace of first matrix is:%d\n",sum1);

for(int i=0;i<2;i++)
{
sum2=sum2+b[i][i];
}
printf("\nThe trace of second matrix is:%d\n",sum2);
}

int main()
{
int a[2][2],b[2][2],i,j,c[2][2];
printf("Enter the elements of first matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
add(a,b);
trace(a,b);
}

 Write the recursive functions for the following


 Sum of numbers in the range of 0 to n
 Number conversions - Binary vs Decimal, Octal vs
Decimal etc
 Sum of digits in given number
 No.of 1's in binary pattern of given number (Parity)

#include <stdio.h>
#include <math.h>

int sum(int a)
{
int sum=0;
for(int i=0;i<=a;i++)
{
sum+=i;
}
printf("The sum until %d is %d.\n",a,sum);
}
int convertDecimalToOctal(int decimalNumber);

int convertDecimalToOctal(int decimalNumber)


{
int octalNumber = 0, i = 1;

while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}
int convert(long long n);

int convert(long long n) {


int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
int getSum(int n)
{
int sum1 = 0;
while (n != 0)
{
sum1 = sum1 + n % 10;
n = n/10;
}
printf("The sum of digits is %d.\n",sum1);
}
int bit(int c)
{
int count=0;

while(c!=0)
{
if(c & 1 == 1)
{
count++;
}
c=c>>1;

}
printf("No of bits those are 1 in its binary representation: %d\n",count);
}

int main()
{
int decimalNumber,a,b;
unsigned int c;
printf("Enter the range\n");
scanf("%d",&a);
sum(a);
printf("Enter a decimal number: \n");
scanf("%d", &decimalNumber);

printf("%d in decimal = %d in octal\n", decimalNumber,


convertDecimalToOctal(decimalNumber));

long long n;
printf("\nEnter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal\n", n, convert(n));

printf("Enter a number\n");
scanf("%d",&b);
getSum(b);
printf("enter an integer to check number of 1 bits\n");
scanf("%d",&c);
bit(c);
return 0;
}

Binary search in 1D array


#include <stdio.h>
int bs(int key, int n,int array[])
{
int low,high,mid;
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.\n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.\n", key);
}

int main()
{
int i, n,key, 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", &key);
bs(key,n,array);
return 0;
}

Polynomial Evaluation , consider that coefficients are


provided in a 1D array. (Without using power function,
Hint:- Horner's method
#include<stdio.h>

int horner(int poly[], int le, int x)


{
int result = poly[0];
for (int i=1; i<le; i++)
{
result = result*x + poly[i];
}
printf("The value of polynomial is:%d",result);
}

int main()
{
int le,num,x,poly[30];
printf("Enter the order of polynomial:\n");
scanf("%d",&le);
printf("Enter the coefficients:\n");
for(int i=0;i<=le;i++)
{
scanf("%d",&poly[i]);
}
printf("Enter x value:\n");
scanf("%d",&x);
horner(poly, le, x);
return 0;
}

.
Find row wise sum/avg, column wise sum/avg in 2D arrays
#include <stdio.h>
int main()
{
int i, j, m, n;
int matrix[20][20];
int sumR, sumC;

printf("Enter number of rows : ");


scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
printf("Enter the matrix values:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("\nGiven Matrix:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\n");
for (i = 0; i < m; i++)
{
sumR = 0;
for (j = 0; j < n; j++)
{
sumR += matrix[i][j];
}
printf("Sum of row %d = %d\n", i + 1, sumR);
}

printf("\n");
for (i = 0; i < n; i++)
{
sumC = 0;
for (j = 0; j < m; j++)
{
sumC += matrix[j][i];
}
printf("Sum of column %d = %d\n", i + 1, sumC);
}
return 0;
}

MATRIX ADDITION
#include <stdio.h>
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}

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


{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}

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


{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}

return 0;
}
MATRIX SUBTRACTION.
#include <stdio.h>
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}

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


{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] – mat2[i][j];
}
}

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


{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}
return 0;
}

MATRIX MULTIPLICATION
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int mat1[10][10], mat2[10][10], mat3[10][10];
printf(“Enter number of rows and columns of mat1 matrix\n”);
scanf(“%d%d”, &m, &n);
printf(“Enter elements of matrix 1\n”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf(“%d”, &mat1[c][d]);
printf(“\nEnter number of rows and columns of mat2 matrix\n”);
scanf(“%d%d”, &p, &q);
if (n != p)
printf(“\nThe matrices can’t be multiplied with each other.\n”);
else
{
printf(“\nEnter elements of matrix2\n”);
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf(“%d”, &mat2[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + mat1[c][k]*mat2[k][d];
}
mat3[c][d] = sum;
sum = 0;
}
}
printf(“\nProduct of the matrices:\n”);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf(“%d\t”, mat3[c][d]);
printf(“\n”);
}
}
return 0;
}

TRACE OF THE MATRIX


#include<stdio.h>
int main()
{

int i, j, n, aj[10][10], sum = 0;

printf("\nEnter the number of rows (columns) of the matrix: \n\n");


scanf("%d", &n);

printf("\nEnter %d elements of the matrix: \n\n", n*n);

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


{
for(j = 0; j < n; j++)
{
scanf("%d", &aj[i][j]);
}
}
for(i = 0; i < n; i++)
{
sum = sum + aj[i][i];
}
printf("\n\nThe Trace of the given matrix is: %d", sum);
return 0;
}

TRANSPOSE OF A MATRIX
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i){

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


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

printf("\nTranspose of the matrix:\n");


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

GENERATE NULL MATRIX


#include <stdio.h>
int main() {
int a[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=0;
}
}
printf("The null matrix generated is :\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}
GENERATE IDENTITY MATRIX
#include <stdio.h>
int main() {
int a[10][10], r, c, i, j;
printf("Enter rows and columns:\n ");
scanf("%d %d", &r, &c);
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
a[i][j]=1;
}
else
{
a[i][j]=0;

}
}
}
printf("\nThe identity matrix generated is :\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}
IDENTITY MATRIX OR NOT
#include <stdio.h>
int main()
{
int arr1[10][10];
int r,c,i, j, yn =1;
printf("Input number of Rows and columns of the matrix :");
scanf("%d%d", &r,&c);
printf("Input elements of matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&arr1[i][j]);
}
}
printf("The matrix is :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c ;j++){
printf("% d\t",arr1[i][j]);
}
printf("\n");
}

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


{
for(j=0; j<c; j++)
{
if((arr1[i][i] != 1) || (( i != j) && (arr1[i][j] != 0)))
{
yn = 0;
break;
}
}
}

if(yn == 1 )
printf(" The matrix is an identity matrix.\n\n");
else
printf(" The matrix is not an identity matrix.\n\n");
}

NULL MATRIX OR NOT

#include <stdio.h>
int main() {
int a[10][10], r, c, i, j , flag=1;
printf("Enter rows and columns:\n ");
scanf("%d %d", &r, &c);
printf("Enter the matrix elements\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}

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


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

for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j] != 0)
{
flag = 0;
break;
}
}
}
if(flag==1)
printf("The given matrix is a null matrix.\n");
else
printf("The given matrix is not a null matrix.\n");
return 0;
}

DETERMINANT OF THE MATRIX


#include <stdio.h>
int main() {
int a[10][10], r, i, j ,det;
printf("Enter 3 x 3 matrix elements\n");
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}

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


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

for(i=0;i<3;i++)
{
det = det + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
}
printf("\nThe Determinant of the matrix is: %d\n\n",det);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy