Sunday, July 12, 2009

C programming?

i need to create a function that accepts 3 parameters. (array of int, size of the array, a value to search). if the 3rd parameter is found in the array, the function will return the location or index. otherwise, it returns -1.





i created this code.. but i think there's something wrong. anyone can help?





#include%26lt;stdio.h%26gt;





#define MAX 10





typedef int arrInt[MAX];





int searchFunction(arrInt a, int MAX, int nNum)


{


int j=0, nfound=0;


if(MAX!=0)


{


for(j=0;j%26lt;max;j++)


{


if(a[j]==nNum)


nfound=j;


}


if(nfound==0)


printf("Not Found!");


}


else


printf("No Array!");


return nfound;


}





main()


{


arrInt a[MAX];


int i, nNum, nValues, nSearch;








for(i=0,i%26lt;MAX;i++)


{


printf("Enter a number:");


scanf("%d",%26amp;nValues);


a[i]=nValues;


}





printf("What number do you want to search?");


scanf("%d",%26amp;nNum);





nSearch=searchFunction(a,MAX,nNum);


}

C programming?
First mistake I can see:





int searchFunction(arrInt a, int MAX, int nNum)





In this function, the first parameter is typed as "arrInt". There is no such data type in C. An array is really just a pointer to an integer, so your definition should look something like this:





int searchFunction(int *a, int MAX, int nNum)


{


}





As for the contents of the function, here are other mistakes...





for(j=0;j%26lt;max;j++)





There is no variable called "max", the one you defined earlier is called "MAX".





This last part is horrible:





if(nfound==0)


printf("Not Found!");


}


else


printf("No Array!");


return nfound;


}





You don't use an opening bracket on the first if statement or the else statement. If you are going to use closing brackets, you NEED opening brackets. Also, if those brackets meant to close the if and else, then there are two other parts of your function that are not closed: one is the function itself, and the other is the very first if statement in the function.





On the other hand, if you didn't mean to close the if and else with brackets, then then these lines:





if(nfound==0)


printf("Not Found!");





Belong OUTSIDE the bracket that comes after it. Your code, running the way it is, will keep printing "Not Found!" on the screen over and over regardless of whether the integer is found or not, since the program is still searching. The only way it would not print that message is if the very first number in the array was the one you were searching for.





Also, if the above code had worked correctly, it would return 0 if the integer was not found. This is not what the assignment asked... it's supposed to return -1.
Reply:"arrInt a" is wrong, it should be "int a[]". Also, one of your function arguments is MAX. Why are you overriding the constant that you already defined? Instead of using MAX each time you call the function, remove that parameter altogether and just access MAX normally. It will then be treated as the global (constant) variable that it is. if the variable is called "MAX", then you must use it as "MAX" each and every time, and not call it as "max" or "Max". After you fix that, I will look over it again if I get the chance.
Reply:I ll first point ur mistakes,


1. you have opened an if loop but not closed


to check forMAX!=0


2. You have assigned MAX as 10


but why do u want to check for MAX!=0, its logically wrong


3. U r returning some value to main() and


also u r printing whether u found or


not in the funtion itself. So wat is the use of


returning values if u r printing there itself.


4. u r checking whether nfound==0,


but consider this logic if the


value to be find itself is zero, then ur logic goes wrong.


5. I think u dint specify the typedef properly. Sorry i am not


sure about that.





Here is the soln:-


u get the value of array normally, and declare as normal int array.


function fn_name(parameters)


{


for(i=0;i%26lt;MAX;i++)


{


if(arr[i]==searchElement)


{


printf("found");


return i;


}


}


return -1;


}








very simple logic, if u found u ll return index, otherwise after the loops get over, it ll automatically return -1. So u can check in the main function and print the message accordingly.

mint

No comments:

Post a Comment