Thread: Some basic things

  1. #31
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also m is not initialized

    so first (and as result each next) time you multiply garbage on something, getting garbage as a result
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #32
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Nathalie View Post
    Code:
                    printf("%d to the power of %d is %d ",m,i,k);
    maybe
    Code:
                    printf("%d to the power of %d is %d ",k,i,m);

  3. #33
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Thanks razorlight, you are right about that. Now the code doesn't give any errors, but the result still isn't correct. Also, we need a code that works for negative numbers as well. Any help would be appreciated.

    Edit: Sorry vart and sloppy, I didn't see your replies. Checking now!

    Edit 2: How should m be initialized?

    Code:
    #include <stdio.h>
                    int power,base;
                    int result=1;
                    int count=0;
                    int main()
    
                    {
                    printf("base....:D");
                    scanf("&#37;d",&base);
                    printf("\npower......:D");
                    scanf("%d",&power);
                    for(count=0;count<power;count++)
                    {
                    base*count=result;
                    }
                    printf("%d to the power of %d = %d ",base,power,result);
                    return 0;
                    }
    Last edited by Nathalie; 11-11-2007 at 06:23 AM.

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    count should = 1 to start the for loop, not 0. There is no zero column of digits in a number base.

    If you want to work with negative int's as well, just make your datatypes unsigned int's.

  5. #35
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Nathalie View Post
    Edit 2: How should m be initialized?

    Code:
                    base*count=result;
    why this? it was corrected before...
    Code:
                    result *= base; // that is  result = result * base
    m (that now is result ) should be initialised to 1. So before loop:
    Code:
                    result =1;

  6. #36
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Adak View Post
    count should = 1 to start the for loop, not 0. There is no zero column of digits in a number base.

    If you want to work with negative int's as well, just make your datatypes unsigned int's.
    both do not seem correct to me...

  7. #37
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    the base may be < or > 0, no problem.

    if power is < 0 you should change sign, something like
    exp = - power
    and after the loop (where you use exp instead of power)
    result = 1/ result

  8. #38
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Now the program works for anything but negative power numbers. To change them, as you said, you probably need an if-structure or so? Would it be possible to write it down? I'm not really sure where to start.

    Code:
    #include <stdio.h>
                    unsigned int power,base;
                    int result=1;
                    int count=1;
                    int main()
    
                    {
                    printf("base....:D");
                    scanf("%d",&base);
                    printf("\n power......:D");
                    scanf("%d",&power);
                    for(count=0;count<power;count++)
                    {
                    result *= base;
                    }
                    printf("%d to the power of %d = %d ",base,power,result);
                    return 0;
                    }

  9. #39
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    read comments:
    Code:
                    unsigned int power,base; // delete "unsigned" here. 
                    int result=1;
                    int count=1; // it is completely useless to put =1 here
                                          // because count is initialized in for loop
    The variables above have to go inside main as told before.

  10. #40
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Quote Originally Posted by sloppy View Post
    read comments:
    Code:
                    unsigned int power,base; // delete "unsigned" here. 
                    int result=1;
                    int count=1; // it is completely useless to put =1 here
                                          // because count is initialized in for loop
    The variables above have to go inside main as told before.
    Thanks for noting that, however this doesn't change anything to the negative powers. If you could go into that briefly, it'd be nice.

  11. #41
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Nathalie View Post
    this doesn't change anything to the negative powers.
    it does change. An unsigned int cannot be negative. Maybe to simplify your problem you can leave power unsigned and base signed.

    the problem is (assuming "power" is an integer) that result=base^(-power) is not an integer. Example:

    10^(-2)= 1/(10^2)=0.01 that is not an integer.

    To store non-integer values in a variable you must declare it as double (or float).
    But doing that requires a little change to printf (because &#37;d is for integers, not for double)
    As you see there is a bit to write sooo:
    If you could go into that briefly, it'd be nice.
    It 'd be really nice if you tried to write something before... I am as lazy as you :-D

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As many have noted before, it's a good practice to properly indent your code. It looks all sloppy and messy. After each brace, indent with a few spaces or a tab. Code will look so much cleaner and easier to read.

  13. #43
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well i should have read the previous posts of this thread, but been lazy today. From last few post i can understand u are trying to find the power of a number. Why not use pow function from math header?

    ssharish

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From first post:
    - A program that calculates powers (ex. 3^4 when 3 and 4 are given by the user) without using pow(a,b).
    I was thinking of writing something like A*((B-1)* "*A"), which would probably work in VB but how do you write this correctly in C?
    As to why... no clue. Negative powers aren't really the easiest to deal with...

  15. #45
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Okay, we've been trying to make it work with negative powers for awhile now but still no success. However maybe it is something in the right direction and you can give a hint after looking at the code.

    For the if <0, count<power is not correct I think, because the count will never be a negative number at all. But when I make it -power, it gives a floating point exception and when we tried some other things, it even crashed. Any ideas?

    Code:
     #include <stdio.h>
    
                    int main()
                    {
                    int power,base;
                    float result=1;
                    int count=1;
    
    
                    printf("base....:D");
                    scanf("%d",&base);
                    printf("\npower......:D");
                    scanf("%d",&power);
    
                    if (power>0)
                            {
                            for(count=0;count<power;count++)
                                    {
                                    result *= base;
                                    }
                            }
    
                    if (power<0)
                            {
                            for(count=0;count<power;count++)
                                    {
                                    result *= -base;
                                    }
                            }
    
                    printf("%d to the power of %d = %.2f ",base,power,result);
                    return 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