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

C - Lab-Assign3

The document contains code for two functions: 1) A function that returns the number of times a search key appears in an array. It takes the array, size and key as parameters. 2) A function that calculates the sum of a specific column in a 2D array. It takes the 2D array as input and the column number from the user and returns the sum.
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)
28 views

C - Lab-Assign3

The document contains code for two functions: 1) A function that returns the number of times a search key appears in an array. It takes the array, size and key as parameters. 2) A function that calculates the sum of a specific column in a 2D array. It takes the 2D array as input and the column number from the user and returns the sum.
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/ 5

1.Write a function that returns the number of times a value(key) appears in array.

Solution:

#include<stdio.h>
int countSearchKey(int arr[],int s,int k);
int main()
{
int i,k,s,q;
printf("Enter The Size of Array:");
scanf(" %d",&s);
int arr[s];
printf("Enter The Elements:\t");
for(i=1;i<=s;i++)
{ scanf(" %d",&arr[i]); }
for(i=1;i<=s;i++)
{printf("%d\t",arr[i]);}
printf("\nSearch Key:");
scanf("%d",&k);
q=countSearchKey(arr,s,k);
printf("The Key Appears %d times in the array.",q);
return 0;
}
int countSearchKey(int arr[],int s,int k)
{
int i,keyCount=0;
for(i=1;i<=s;i++)
{
if(arr[i]==k)
{keyCount+=1;}
}
return keyCount;
}
2.Write a program that computes the sum of a specific column (provided by the user as input) in a 2D
array.
Solution:
#include<stdio.h>
int main()
{
int i,j,sum=0,r,c,q;
printf("Enter Number of Rows:\n");
scanf("%d",&r);
printf("Enter Number of Columns:\n");
scanf("%d",&c);
int a[r][c];
printf("Enter Elements:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Your 2D Array is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{printf("%5d",a[i][j]);}
printf("\n");
}
printf("Which Column Do You Want to Sum:\n");
scanf("%d",&q);
for(i=0;i<r;i++)
{
sum=sum+a[i][q-1];
}
printf("The Sum for Column %d Is %d",q,sum);
return 0;

}
CSE 115 LAB ASSIGNMENT-03

Name: Mritunjoy Chakrobarty


ID:1911943042
Section:07

You might also like