/*this program calulates the occurence of a number given by the user at runtime in an array*/
#include<stdio.h>
#include<string.h>
int counter=0; // for counting number of occurences
int i=0; // for iteration
int count(int fnumber,int farray[],int flength)
{
if(i == flength) // checks if all the elements of the array have been scanned
return(1); // i dont know why i have written i inside return().. seriously..
if(farray[i] == fnumber) // checks if the array element at i matches with the required number
counter++; // if yes then increments the counter
return(fnumber,farray[i+1],flength); // recursively calls itself again with farray[i+1]
}
int main()
{
int array[100]; // for storing elements
int length; // for storing the length of the array or number of elements in the array
int i; // for iterative purpose
int number; //number to be compared
int occur; //number of occurences or the result
printf("How many elements do you want to enter?\n");
scanf("%d",&length);
printf("Enter the numbers\n");
printf("\n");
for(i=0;i<length;i++)
{
scanf("%d",&array[i]); // saves the elements into the array
}
printf("Enter the number you want to look for\n");
scanf("%d",&number);
occur=count(number,array,length); // function called
printf("The number has %d occurences\n",occur);
return(0);
}
Question
LeviS LoVEr
what cud be wrong with this one ?
/*this program calulates the occurence of a number given by the user at runtime in an array*/ #include<stdio.h> #include<string.h> int counter=0; // for counting number of occurences int i=0; // for iteration int count(int fnumber,int farray[],int flength) { if(i == flength) // checks if all the elements of the array have been scanned return(1); // i dont know why i have written i inside return().. seriously.. if(farray[i] == fnumber) // checks if the array element at i matches with the required number counter++; // if yes then increments the counter return(fnumber,farray[i+1],flength); // recursively calls itself again with farray[i+1] } int main() { int array[100]; // for storing elements int length; // for storing the length of the array or number of elements in the array int i; // for iterative purpose int number; //number to be compared int occur; //number of occurences or the result printf("How many elements do you want to enter?\n"); scanf("%d",&length); printf("Enter the numbers\n"); printf("\n"); for(i=0;i<length;i++) { scanf("%d",&array[i]); // saves the elements into the array } printf("Enter the number you want to look for\n"); scanf("%d",&number); occur=count(number,array,length); // function called printf("The number has %d occurences\n",occur); return(0); }Link to comment
Share on other sites
12 answers to this question
Recommended Posts