• 0

[C] this program isnt giving the correct output..


Question

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

  • 0

I was actually wondering why you were using recursion but anyway after another look at the function I found some mistakes. Here's a fixed version for you:

int count(int fnumber,int farray[],int flength)
{	
	if(i == flength)					  // checks if all the elements of the array have been scanned
		return(counter);			 // 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
	i++;
	return(fnumber,farray,flength);	 // recursively calls itself again with farray[i+1]
}

Edited by mrvc
Link to comment
Share on other sites

  • 0

It still doesn't work even with setting i to 0 after the for-loop and using the fixed function? (The function was meant to return the counter rather than 1 in my post I forgot to correct it when I edited)

If you want to be familiar with the idea of recursion here's an example:

int count(int fnumber,int farray[],int flength)
{  
	int occurred = 0;  
	if(flength-1 >= 0)
	{
		if(farray[flength-1] == fnumber)
			occurred++;
		return occurred + (fnumber,farray,flength-1);
	}
	return occurred;
}

Link to comment
Share on other sites

  • 0
It still doesn't work even with setting i to 0 after the for-loop and using the fixed function? (The function was meant to return the counter rather than 1 in my post I forgot to correct it when I edited)

it didnt work..

hey i wanna know that is the logic correct??

int count(int fnumber,int farray[],int flength)
{  
	int occurred = 0;  
	if(flength-1 >= 0)
	{
		if(farray[flength-1] == fnumber)
			occurred++;
		return occurred + (fnumber,farray,flength-1);


	}
	return occurred;
}

what is this + for in return occured + (fnumber,farray,flength-1) ..?

one more thing that wont 'occured' be initialised ZERO eveytime the recursive function calls?

Link to comment
Share on other sites

  • 0

The logic looks fine...

What is the output of the program now?

^Did you try running that? The logic goes like this if farray has 5, 2, 3, 2, 6, fnumber is 2 and flength is 5:

count(2, farray, 5): return 0 + count(2, farray, 4): return 1 + count(2, farray, 3): return 0 + count(2, farray, 2): return 1 + count(2, farray, 1): return 0 + count(2, farray, 0): return 0.

The end result would be: return 0 + 1 + 0 + 1 + 0 + 0 = 2.

Edited by mrvc
Link to comment
Share on other sites

  • 0

in your program ::

what is this '+' for in return occured + (fnumber,farray,flength-1) ..? in line number 8

one more thing that wont 'occured' be initialised ZERO eveytime the recursive function calls?

Link to comment
Share on other sites

  • 0
it didnt work..

hey i wanna know that is the logic correct??

what is this + for in return occured + (fnumber,farray,flength-1) ..?

one more thing that wont 'occured' be initialised ZERO eveytime the recursive function calls?

he probably meant occured + count(funmbuer,farray,flength-1)

(didn't check rest of the code)

Link to comment
Share on other sites

  • 0
he probably meant occured + count(funmbuer,farray,flength-1)

(didn't check rest of the code)

lol that's what I meant. In fact that's the mistake in the first post too!

Link to comment
Share on other sites

  • 0
in your program ::

what is this '+' for in return occured + (fnumber,farray,flength-1) ..? in line number 8

The '+' is for adding an occurrence (if fnumber matches to the given number from the farray) to the subsequent call of the count function. (See the logic I showed above)

one more thing that wont 'occured' be initialised ZERO eveytime the recursive function calls?

Well each call gets its own occurred variable actually.

Anyway try changing return(fnumber,farray... to return count(fnumber,farray... , it should do it.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.