I need help with this C programming assignment Instructions Using C programming.
ID: 3601223 • Letter: I
Question
I need help with this C programming assignment
Instructions
Using C programming.
You will need to write the following functions:
printArray - takes an array and an integer representing the size of the array as input and prints the array to the console
findInArray - takes an array and two integer as input, one for array size and the other as a search value, and searches the array for the integer. It returns the index of the integer if found, and -1 if it is not found. Do not worry about duplicates, just return the first one you find.
getFrequency - takes an array and an integer representing the size of the array as input. This array counts the how often a number occurs in the input array - the frequency. Created an array of 21 integers called frequency. Starting with the first index of the frequency array, count the number of times that index exists in the input array. Store that number in the frequency array and move to the next index and repeat. When you are done, PRINT the frequency array.FOR EXAMPLE - {2, 1, 1, 2, 1, 0, 2, ,0 ,1, 0, 2, 2} => {3, 5, 4}
The input array has 3 zeros, 4 ones and 5 twos
You will write a program that does the following
declare an array of 100 integers
declare an integer to represent the size of the array
initials the array with 100 random integers between 0 and 20 (inclusively)
call the print array function with the array as input
prompt the user for an integer use your array and the integer to call the findInArray function. When the function returns, print the results. Repeat this 10 times.
call the getFrequncy function with your array as input. In the frequency function print the resulting array with the print array function.
In the frequency function, use a loop to calculate and print the following
the sum of the array
the average of the array
Explanation / Answer
NOTE:ANY issue during execution drop a comment.i will rectify it.
give a postive feed back by giving thumbsup when satisfied with the answer
#include<stdio.h>/*for standard input output operations we use this header file*/
#include<stdlib.h>/*for standard library functions we use this header file*/
void printArray(int a[],int size)/* to print the given array */
{
int j;
for(j=0;j<size;j++)/*to go through the given array till the end of the array based on size*/
{
printf("%d ",a[j]);/*to print elements*/
}
printf(" ");/**/
}
int findInArray(int a[],int size,int key)/**/
{
int j;
for(j=0;j<size;j++)/*to repeat till the end of the array */
{
if(key==a[j])/**/
return (j+1);/*if element found retun j+1
because array indexing starts from 0th location .
so we add 1 to the index when returning*/
}
return -1;/*if elements not found then return -1*/
}
void getFrequency(int a[],int size)/*to generate a frequenct arrat*/
{
int frequencyarray[21],j=0,k,sum=0;
float d;
for(j=0;j<21;j++)/*to repeat the loop for 21 times*/
frequencyarray[j]=0;/*intialize all elements of the frequency array with zero*/
for(j=0;j<size;j++)/*to go through the end of the array*/
{
k=a[j];/*to get the array element at j th location*/
frequencyarray[k]++;/*to increment the if the index of frequency array is in input array passed*/
}
printf("Frequency Array: ");
printArray(frequencyarray,21);/*to call printArray to print the frequency*/
for(j=0;j<21;j++)/**/
{
sum=sum+ frequencyarray[j];/*to calculate sum*/
}
printf("sum of frequnecies %d ",sum);
d=(float)sum/21;/*to calculate average*/
printf("avg of frequnecies %f ",d);
}
int main() {
int a[100],i,size=100,result,key;
for(i=0;i<size;i++)/*to run for 100 times to generate 100 random elememts*/
{
a[i] = rand() % 20;/*to generate the integers between 0 to 20 inclusive*/
}
printf("Randomly Generated Array ");
printArray(a,size);/*call the printArray function to print the random array*/
for(i=0;i<10;i++)/*to repeat the following process for 10 times*/
{
printf("enter the key to search in the array ");
scanf("%d",&key);/*reading the search key form user*/
result=findInArray(a,size,key);/*calling the findInArray for searching the element*/
if(result>0)/*check result contains greater than zero or not*/
{
printf("Key found at %d ", (result+1));/*display the key location*/
}else
{
printf("Key not found ");
}
}
getFrequency(a,size);/*calling the getFrequency funcion */
}