Thread: Some basic things

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here's how I made it work:

    UPDATE: Safer code that accounts 0 below.

    Doesn't work with non-even numbers (ie 0.5, etc).
    Last edited by Elysia; 11-11-2007 at 10:31 AM.

  2. #47
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Thanks a lot for writing down the full code. However I had my friend try it and it gives errors. Could it be that it contains C++?

  3. #48
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well this is the other way of doing it, may be if its interesting to you

    Code:
        if( power < 0 )
            printf("Result &#37;f", ( 1.0 / pow(base, ( power * -1 )))); 
        else
            printf("Resilt %f", pow(base, power));
    ssharish

  4. #49
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Nathalie View Post
    Thanks a lot for writing down the full code. However I had my friend try it and it gives errors. Could it be that it contains C++?
    Yes, cin and cout are C++, the rest is C-compatible, I believe.

    Quote Originally Posted by ssharish2005 View Post
    well this is the other way of doing it, may be if its interesting to you

    Code:
        if( power < 0 )
            printf("Result %f", ( 1.0 / pow(base, ( power * -1 )))); 
        else
            printf("Resilt %f", pow(base, power));
    ssharish
    pow wasn't to be used or we wouldn't be dicussing this.

  5. #50
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    another thing you may want to consider is the case that num=0 (because you cannot do 0^(-3))

  6. #51
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    0 * X = 0.
    0 / X = 0.
    So there's no worry I think.

  7. #52
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Elysia View Post
    0 * X = 0.
    0 / X = 0.
    So there's no worry I think.
    reading your code above, with num=0 and pow<0 say pow= -3 you get:
    Code:
    else if (pow < 0)  // -3<0 
    	{
    		onum = num;      //  onum = 0
    		for (; pow < 1; pow++)
    		{
    			num /= onum;  // num = 0  / 0  <---- this is the problem
    		}
    	}

  8. #53
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sloppy View Post
    reading your code above, with num=0 and pow<0 say pow= -3 you get:
    Code:
    else if (pow < 0)  // -3<0 
    	{
    		onum = num;      //  onum = 0
    		for (; pow < 1; pow++)
    		{
    			num /= onum;  // num = 0  / 0  <---- this is the problem
    		}
    	}
    You're right.
    Alright. New code:
    UPDATE: New code below.
    Last edited by Elysia; 11-11-2007 at 11:11 AM.

  9. #54
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Thanks to all of you for your efforts! Eventually we got it to work with this code, which doesn't contain any C++. (The words are in Turkish now though, don't ask. I believe us is power). Thanks again!

    Code:
    #include <stdio.h>
    
                    int main()
                    {
                    int us=0;
                    int taban=0;
                    float sonuc=1;
                    int sayac=1;
    
    
                    printf("Ussu alinacak sayiyi girin....:D");
                    scanf("%d",&taban);
                    printf("\nUssu giriniz......:D");
                    scanf("%d",&us);
    
                    if (us>0)
                            {
                            for(sayac=1;sayac<=us;sayac++)
                                    {
                                    sonuc *= taban;
                                    }
                            }
    
    
                    else
                            {
                            for(sayac=1;sayac<=-us;sayac++)
                                    {
                                    sonuc *= (1 / (float)taban);
                                    }
                            }
                    printf("%d uzeri %d = %.2f dir",taban,us,sonuc);
    
    
    
                    return 0;
                    }

  10. #55
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Nathalie View Post
    Thanks to all of you for your efforts! Eventually we got it to work with this code, which doesn't contain any C++. (The words are in Turkish now though, don't ask. I believe us is power). Thanks again!
    Look at my code above. Don't forget to check for power == 0 (anything raised to 0 is 1) and number == 0 (division by 0 is illegal). Also, you have a bug, I realize.

    Here's a condition:
    number = 10
    power = 3
    Result should be 1000, right?
    Let's check them loops:

    First loop: number = number (10) * number (10) = 100
    Second loop: number = number (100) * number(100) = 10.000

  11. #56
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Just a bit change, considering that 0^(a) has sense for a > 0 (and it's not 0)
    Code:
    int main()
    {
    	double num;
    	double onum;
    	double pow;
    	cout << "Enter number: ";
    	cin >> num;
    	cout << "Enter power: ";
    	cin >> pow;
    	if (num == 0)
    	{
                    if (pow>0)
                    {
                            cout << "Ehi! I cannot do that! " << endl;
                            return 1;
                    }        
    		// this is useless: num = 0;
    	}
    	else
    	{
    		onum = num;
    		if (pow > 0)
    		{
    			for (; pow > 1; pow--) 
    			{
    				num *= onum;
    			}
    		}
    		else if (pow == 0) 
    		{
    			num = 1;
    		}
    		else if (pow < 0)
    		{
    			for (; pow < 1; pow++)
    			{
    				num /= onum;
    			}
    		}
    	}
    	cout << "Result is " << num << endl;
    	return 0;
    }

  12. #57
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    0^X = 0. What are you implying?

  13. #58
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Elysia View Post
    0^X = 0. What are you implying?
    :-o

    0^(-3) = 1/(0^3)= 1/0 = indeterminate
    0^0 = indeterminate

  14. #59
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Depends on your calculator:

    Windows Calc says:
    0^0 = 1 (Which is false)
    0^(-3) = Invalid

    Powercalc says:
    0^0 = Invalid
    0^(-3) = 0

    My ti calc says:
    0^0 = Invalid
    0^(-3) = Invalid

    Still, the code doesn't check for division by 0.

  15. #60
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main()
    {
    	double num;
    	double onum;
    	double pow;
    	cout << "Enter number: ";
    	cin >> num;
    	cout << "Enter power: ";
    	cin >> pow;
    	if (num == 0)
    	{
    		if (pow <= 0)
    		{
    			cout << "Invalid number / power combination.\n" << num << " ^ " << pow << ": Invalid operation!\n";
    			return 1;
    		}
    		num = 0;
    	}
    	else
    	{
    		onum = num;
    		if (pow > 0)
    		{
    			for (; pow > 1; pow--) 
    			{
    				num *= onum;
    			}
    		}
    		else if (pow == 0) 
    		{
    			num = 1;
    		}
    		else if (pow < 0)
    		{
    			for (; pow < 1; pow++)
    			{
    				num /= onum;
    			}
    		}
    	}
    	cout << "Result is " << num << endl;
    	return 0;
    }
    This takes into consideration powers of 0 and negative when number is 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob with basic q's
    By SimplyComplex in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2006, 01:17 PM
  2. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  5. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 06:03 PM