Thread: Raise to the power problem

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Raise to the power problem

    Hey guys

    I am doing a rather simple exercise but I am not permitted to use the pow() function to do this.

    I need to print of all the powers of 2(0) to 2(8) as in 2 x 2 x 2 x2 etc.

    I have tried using the base exponent method but I am getting eight zeros as output.

    This is what I have tried, can anyone see what I am doing wrong?

    Code:
    #include <stdio.h>
    
    int main ( void ) {
    	int i, 
    		base = 2,
    		res = 0,
    		exp = 0;
    
    	while ( exp <= 8 ) {
    		for ( i = 1; i <= exp; i++ ) 
    			res = res * base;
    
    			printf("%d\n", res);
    		exp++;
    	}
    		
    	getchar();
    
    	return 0;
    }
    Double Helix STL

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, 0*2*2*2*2*2*2*2*2 is zero, after all. Perhaps you don't want a 0 in the product?

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks tabstop such a simple mistake too. All I had to do was change res = 0 to res = 2 and zero it before the exp++.

    Cheers for pointing me in the right direction!
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Shifting left the number 1 by 1 is the same as multiplying by 2. So if x = 1 and you do x << 1, x becomes 2, which is the same as 2^1.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    This works as an unoptimized general case for most user defined types:

    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    
    namespace xtd {
        
    template < typename Type, typename Integer >
    Type&
    raise( Type& lhs, Integer rhs )
    {
            static Type
                    one = Type( ),
                    once = one++;      
            Type const
                    value = lhs;
            lhs = one;  
            while( rhs-- > zero )
                    lhs *= value;
            return lhs;      
    }
    
    template < typename Type, typename Integer >
    inline Type&
    operator ^=( Type& lhs, Integer rhs )
    {
            return raise( lhs, rhs );      
    }
    
    template < typename Type, typename Integer >
    Type
    raised( Type const& lhs, Integer const& rhs )
    {
            Type
                    tmp( lhs );
            return raise( tmp, rhs );      
    }
    
    template < typename Type, typename Integer >
    inline Type
    operator ^( Type const& lhs, Integer const& rhs )
    {
            return raised( lhs, rhs );      
    }
    
    } // namespace
    
    using namespace std;
    using namespace xtd;
    
    int
    main( int argc, char** argv )
    {            
            if( argc == 3 )
            {
                    cout << raised( strtod( argv[ 1 ], 0 ), atoi( argv[ 2 ] ) ) << endl;
                    return 0;
            }
            cout << "raise a number to some power - usage: '" << argv[ 0 ] << " float-value integer-exponent'" << endl;
            return 1;
    }
    [edit]
    Hmm...but what about negative exponents?
    [/edit]

    [edit]
    Is this it?

    Code:
    template < typename Type, typename Integer >
    Type&
    raise( Type& lhs, Integer rhs )
    {
            static Type
                    one = Type( ),
                    once = one++;         
    	Type
    		value;	
            if( rhs < Integer( ) )
    	{
    		rhs = -rhs;		
    		value = lhs ? one / lhs : lhs;
    	}
    	else
                    value = lhs;                
            lhs = one;
            while( rhs-- > Type( ) )
                    lhs *= value;
            return lhs;      
    }
    [/edit]
    Last edited by Sebastiani; 11-30-2008 at 08:30 PM. Reason: editing
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Fyi, Sebastiani's example is in C++, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    swgh:
    Can it be that you do not have had the pleasure of using a debugger?
    You ask quite simple problems--simple mistakes--that you should easily be able to solve on your own.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Fyi, Sebastiani's example is in C++, not C.

    Whoops, wrong forum.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. hmm i think i broke something (power supply)
    By MisterSako in forum Tech Board
    Replies: 1
    Last Post: 02-11-2006, 11:32 PM
  3. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM