this code won't work

This is a discussion on this code won't work within the C Programming forums, part of the General Programming Boards category; good morning ; this programme is created to find x^y but it show every time an random number !!! could ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    this code won't work

    good morning ;
    this programme is created to find x^y
    but it show every time an random number !!!
    could you help me please
    and thank you so much ....
    Code:
    #include <stdio.h>
    int nbpos (int x)
    {
        int c,cpt ;
        do
        {
            x=x/10;
            c++;
        }
        while (x!=0);
    
        return c;
    }
    
    
    int puiss (int x , int y)
    {
        int i=1,s=1;
    printf ("\n--- x == %d --- s == %d---- y == %d\n",x,s,y);    
    /*
    for (i=1;i<y;i++)
        {
            s=s+1;
            
        }
    */
    return s;
    }
    
    main()
    {
    int x=10,y,e;
    
        scanf("%d",&y);
        printf (" %d  ********** \n",nbpos(y));
    
    
    e=nbpos(y);
    e=puiss (x,e);
    printf("////// %d \n",e);
    }
    Last edited by Salem; 03-23-2011 at 09:35 AM. Reason: Remove font size/colour abuse

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by med linux View Post
    but it show every time an random number !!!
    Code:
    #include <stdio.h>
    int nbpos (int x)
    {
        int c,cpt ;
        do
        {
            x=x/10;
            c++;
        }
        while (x!=0);
    
        return c;
    }
    Notice that c is not initialized to any particular value, so it's value is random.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by med linux View Post
    good morning ;
    this programme is created to find x^y
    but it show every time an random number !!!
    could you help me please
    and thank you so much ....
    Please don't use giant colored text... It makes you look like an idiot.

    Code:
    #include <stdio.h>
    int nbpos (int x)
    {
        int c,cpt ;
        do
        {
            x=x/10;
            c++;
        }
        while (x!=0);
    
        return c;
    }
    your integer c is used without initialization... so you're doing math on garbage.
    cpt is never used.
    Last edited by Salem; 03-23-2011 at 09:36 AM. Reason: s/5/1 for size

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    I'm assuming that x^y means x to the power of y? If so, this isn't even close.

    The function nbpos is returning the exponent part of the number when expressed in scientific notation, so 7 = 7E0 --> nbpos(7) = 0, and 286=2.86E2 --> nbpos(286) = 2.

    Currently, the puiss function is always returning 1. With the comments removed, puiss is returning y+1, and x isn't used. The y you're passing in is the return value of nbpos (e in main).

    What you should be doing to calculate x^y is going through a loop to multiply x by itself y times.

    EDIT: I didn't catch that c wasn't being initialized in nbpos. Still, even if it was, the above still stands.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    Please don't use giant colored text... It makes you look like an idiot.



    your integer c is used without initialization... so you're doing math on garbage.
    cpt is never used.
    the problem is in the color or in the color and the size ?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,897
    the problem is in the color or in the color and the size ?
    Yes

    Jim

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by med linux View Post
    the problem is in the color or in the color and the size ?
    Both...

    When I see giant red text the first thing that goes through my mind is "10 year old at the keyboard".

    In fact, it almost certainly has the opposite of the effect you intended...
    When I see crap like that I generally just go read something else.
    Last edited by CommonTater; 03-23-2011 at 09:16 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Fixed colour and size
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  2. Stuck making part of my code work for testing input
    By KevinP in forum C Programming
    Replies: 6
    Last Post: 01-25-2011, 08:52 AM
  3. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  4. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21