English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i need it to find the kth digit and store it in result of the function ....my function should return the resulted value to the parameter result (call by reference)
#include
int kth_digit(int n,int k,int *result)
{
int i;
int temp=n;
for(i=0;i {
temp=temp/10;
result=&temp;
}

}
main()
{
int *result,y,m;
int c;
result=(int*)&c;
prinf("enter n:");
printf("enter k");
scanf("%d",&y);
scanf("%d",&m);
c=kth_digit(y,m,&result);
printf("digit number %d is %d",y,result%10);
}

2006-10-02 06:29:42 · 2 answers · asked by myheartsuzan 2 in Computers & Internet Programming & Design

2 answers

Where to start:

main():
----------
Things are done IN ORDER, so your program will print the line
"enter nenterk" and then wait for 2 input numbers, separated by a line-feed.

It should read:

prinf("enter n:"); /* request "n" value */
scanf("%d",&y); /* GET "n" value */
printf("enter k"); /* request "k" value */
scanf("%d",&m); /* GET "k" value */

Also, your kth_digit() return is confused. You are either returning the result in "result" or you are returning it as a value of the function. Remove the "c=" so that your function call looks like this:

kth_digit( y, m, &result );

And...would it be too much to use the variables "n" and "k" instead of "y" and "m". SCOPE is the concept that is missing here - you can re-use variable names within different functions because the compiler always uses the most local definition of the variable.

kth_digit()
-------------
You're supposed to de-reference your result variable, not get the pointer to the local temp variable.

result=&temp;

SHOULD READ:

*result=temp;

For efficiency sake, it should appear OUTSIDE the loop, although that is not a requirement. So the whole phrase should read...

for(i=0;i {
temp=temp/10;
}
*result=temp; /* no need to do this every time, as we only care about last result! */

Also, your requiremnt is to return the digit, isn't it?

That means that the MOD function should be done in kth_digit - i.e. that result line should read

*result=(temp%10);

Remove the MOD function from your "main()" printf() line.

Also, make the declaration "void" not "int." kth_digit() doesn't return anything itself, but uses the pointer you pass it to send back the information.

void kth_digit( int n,int k,int *result) /* a function that doesn't RETURN anything */

That's about it....

2006-10-02 06:49:45 · answer #1 · answered by jbtascam 5 · 0 0

On top of what the previous person mentioned kth_digit does not return anything. so c=kth_digit(y,m,&result) will not work

2006-10-02 14:30:08 · answer #2 · answered by Andrew R 1 · 0 0

fedest.com, questions and answers