Thread: Plz help with this loop problem,Thanks!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Plz help with this loop problem,Thanks!

    Hi,
    Im pretty much a beginner. Encountered this problem while practising
    Really appreciate it if you could help=).

    Im trying to compute (x^k) using a seperate function.

    Code:
      1 #include<stdio.h>
      2 #include<math.h>
      3 
      4 int main(void)
      5 
      6 
      7 {
      8     double x;
      9     int k;
     10     double power(double x,int k);
     11 
     12     printf("enter any real no.:\n ");
     13     scanf("%lf", &x);
     14     printf("enter any non-negative no.:\n");
     15     scanf("%i", &k);
     16 
     17     printf("result=%.3f\n",power(x,k));
     18     return 0;
     19 
     20 
     21  }
     22 
     23 
     24  double power(double x, int k)
     25  {
     26      double result;
     27      int loop;
     28      for (loop=1; loop<=k; loop++)
     29          result *= x;
     30          return result;
     31  }
    while executing, there is no error msg generated,
    However, the output is really huge and obviously wrong.
    Have spent the whole afternoon trying to figure out=(.

    Please advise

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    result is not initialized, so it contains random garbage that's multiplied loop times.
    If you're using Windows, I would suggest using Visual Studio since it automatically catches these types of errors.
    Also up your compiler warnings. Any compiler would happily throw a warning about this.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    problem solved!
    thanks Elysia=)

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hai your code would be...

    Following code with Little modification:-

    Code:
    #include<stdio.h>
      #include<math.h>
       
       int main(void)
       
       
       {
           double x;
           int k;
          double power(double x,int k);
      
          printf("enter any real no.:\n ");
          scanf("%lf", &x);
          printf("enter any non-negative no.:\n");
          scanf("%i", &k);
      
          printf("result=%.3f\n",power(x,k));
          return 0;
      
      
       }
      
      
       double power(double x, int k)
       {
           double result=1;
           int loop;
           for (loop=1; loop<=k; loop++)
               result *= x;
               return result;
       }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what a strange indentation you have...

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
    	double x;
    	int k;
    	double power(double x,int k);
    
    	printf("enter any real no.:\n ");
    	scanf("&#37;lf", &x);
    	printf("enter any non-negative no.:\n");
    	scanf("%i", &k);
    
    	printf("result=%.3f\n", power(x,k));
    	return 0;
    }
    
    
    double power(double x, int k)
    {
    	double result=1.0;
    	int loop;
    	for (loop=1; loop<=k; loop++)
    	{
    		result *= x;
    	}
    	return 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM