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

Solution:: 1.write A Function That Returns The Number of Times A Value (Key) Appears in Array

The document contains code to solve two problems: 1) Write a function that returns the number of times a value appears in an array. It takes the array, size, and key as parameters and uses a for loop to count matches. 2) Write a program that computes the sum of a specific column in a 2D array. It takes row and column counts as input, initializes a 2D array, inputs elements, prints the array, takes a column number, and uses a for loop to sum that column.
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)
27 views

Solution:: 1.write A Function That Returns The Number of Times A Value (Key) Appears in Array

The document contains code to solve two problems: 1) Write a function that returns the number of times a value appears in an array. It takes the array, size, and key as parameters and uses a for loop to count matches. 2) Write a program that computes the sum of a specific column in a 2D array. It takes row and column counts as input, initializes a 2D array, inputs elements, prints the array, takes a column number, and uses a for loop to sum that column.
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/ 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