i need help related to pointers

i am new to pointers and i solved a problem factrl in easy section without help of pointers after learning a little bit i tried to solve it using pointers it seems to run fine on my system but when i am submitting it is saying wrong answer please help me

#include<stdio.h>
#include<math.h>
void zeros(int*);
int noi(int*);

int n,t,i;

int main()
{

    scanf("%d",&t);
    for(i=0;i<t;i++)
    {

    scanf("%d",&n);

    zeros(&n);

    }
    return 0;


}

void zeros(int *n)
{
    int j=1,i,sum=0;
    i=noi(&n);
    for(j=1;j<=i;j++)
    {
        sum=sum+*n/pow(5,j);

    }
    printf("%d\n",sum);

}
int noi(int *x)
{
    int i=1;
    while(*x/pow(5,i)>=1){
        i++;
    }

    return i-1;
}

and please guide me how can i improve the efficiency of this program following the same logic

When I’m coding in C++ I prefer when compiler reports all warnings

>g++-4 -Wall pointers.cpp -o pointers.exe
pointers.cpp: In function 'void zeros(int*)':
pointers.cpp:28:13: error: cannot convert 'int**' to 'int*' for argument '1' to 'int noi(int*)'

Maybe that helps you :wink:

thanks alot i got what you are saying
but even after changing my code to this

#include<stdio.h>
#include<math.h>
void zeros(int*);
int noi(int);

int n,t,i;

int main()
{

    scanf("%d",&t);
    for(i=0;i<t;i++)
    {

    scanf("%d",&n);

    zeros(&n);

    }
    return 0;


}

void zeros(int *n)
{
    int j=1,i,sum=0;
    i=noi(n);
    for(j=1;j<=i;j++)
    {
        sum=sum+*n/pow(5,j);

    }
    printf("%d\n",sum);

}
int noi(int x)
{
    int i=1;
    while(x/pow(5,i)>=1){
        i++;
    }

    return i-1;
}

it is still saying wrong answer

earlier i also used to think that i should not worry about time solution is important
but after submitting this problem i compared the time of my problem and others problem there was a big difference so i just wanted to know that if i am following the right approach or not
thanks

There is no need to use the pow function at all! Instead try this:

function numofzeroes :
    while N is not less than 5
          N=N/5
          sum=sum+ N
    print sum

i am really sorry but i am unable to get what you are saying and also i don’t know c++

C++ and C are almost same (especially when you are not using stl).

I simply wanted to say, that you have

int noi(int *x)

but n in void zeros(int *n) is already int*, when you are calling noi, you use & and you shouldn’t, because you are passing int** to noi().

Now I tried to compile with C compiler and I got same warning

>gcc-4 -Wall pointers.c -o pointersc.exe
pointers.c: In function 'zeros':
pointers.c:28:5: warning: passing argument 1 of 'noi' from incompatible pointer type
pointers.c:4:5: note: expected 'int *' but argument is of type 'int **'

similar error again

>gcc-4 -Wall pointers.c -o pointersc.exe
pointers.c: In function 'zeros':
pointers.c:27:5: warning: passing argument 1 of 'noi' makes integer from pointer without a cast
pointers.c:4:5: note: expected 'int' but argument is of type 'int *'

now, you are passing address to noi(), instead of 1 change you did 2

i got the mistake and i corrected it and successfully submitted it
and everything is fine
but i thought that using pointers would take less time but instead earlier without use of pointers it was taking 0.90 seconds and with use of pointers it is taking 0.93 seconds
please guide me

In this case there is not so big difference, because if you store one int to stack (when value is copied) or one int (when address is passed) it is the same. It will be better when you do that with arrays - instead of array copying you pass just address. My advice is: “do not care much about running time”. It depends on server load, so you will get different times when you try multiple times…

If you want to make a big difference in the execution time, you need to know what is taking the most time.

In your program it seems that pow(5,.) and floating point calculations may take the most time. These are done multiple times per test case. You can create a power5 array (of ints) that is filled before the first test case.

Then try to rewrite your program to see what the result is…