Exponential Problem with C Programming

This is a discussion on Exponential Problem with C Programming within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm new to C programming, taking a course for EE. This is the question: C does not have ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Exponential Problem with C Programming

    Hey guys, I'm new to C programming, taking a course for EE.

    This is the question: C does not have an exponentiation operator. Write a C program that allows the user to enter the base a and the exponent b and prints the value of (a^b). You may not use any library functions other than printf and scanf. Assume b is an integer.

    So I have my program but when you input a and b, it seems like it hang ups, no output. When you press enter many times, it doesn't do anything. Only when you input something (when you are not suppose), does it display the printf statement.

    Can anyone help me out? thanks

    here is the code:

    Code:
    #include <stdio.h>
    int main ()
    {
    	int b,i;
    	float a,c;
    
    	printf("\nEnter base value a: ");
    	scanf("\n%f", &a);
    	printf("\nEnter exponent value b: ");
    	scanf("\n%i\n", &b);
    
    	c=1;
    	i=1;	
    
    	while(i<=b)
    	{
    		c*=a;	
    		i++;
    	}
    	
    
    	printf("\nThe answer is %.2f", c);
    
    	return(0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The code
    Code:
    scanf("\n%i\n", &b);
    does not do what you think it does. Namely, the \n at the end means to throw away whitespace, but the program needs a non-whitespace character (later) to know when to stop throwing away the whitespace. So: don't put \n at the end of a scanf format string, unless you know positively there will be more input coming and want (for whatever reason) to make sure there's no whitespace in the input buffer. (Note: this will never happen.)

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    what do you mean by whitespace?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    whitespace = things that print "white" on a piece of paper. So: spaces, tabs, carriage returns, line feeds, form feeds, and the ever-popular vertical tab (\v). The \n at the beginning is alright; but whitespace is always skipped by scanf unless you're explicitly reading in characters with %c (or doing some extra-fancy things).

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If the base b is allowed to be negative, then you need to handle that case as well, by first computing the answer for the corresponding positive value, then taking the reciprocal. You can worry about that after getting it working for nonnegative values of b, though.

    Edit: Another way to handle it is to simultaneously take the reciprocal of a, and reverse the sign of b, which is probably easier.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You might want to look in the <math.h> header. There's a function called pow() that might prove very useful for what you're doing.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Note that more "standard form of the loop in use
    Code:
    i=1;
    while(i<=b)
    {
       ...
       i++;
    }
    is
    Code:
    for(i=1;i<=b;i++)
    {
    }
    Moreover - due to indexing specific of C such loops are generelly written as
    Code:
    for(i=0; i < b; i++)
    {
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    thanks for all your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

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