Newbie help with expontential program

This is a discussion on Newbie help with expontential program within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> 2 3 int main(void) 4 { 5 float base, exp, result; 6 7 8 9 printf("Enter base\n"); ...

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

    Red face Newbie help with expontential program

    Code:
    
     #include <stdio.h>
      2 
      3 int main(void)
      4 {
      5 float base, exp, result;
      6 
      7 
      8 
      9 printf("Enter base\n");
     10 scanf("%f",&base);
     11 printf("Enter exponent\n");
     12 scanf("%f",&exp);
     13 base=0;
     14 for (exp = -1000, exp <= 1000, exp++){
     15  base = exp;
     16  result = base * exp;
     17 }
     18 printf("Result is %f\n",result);
     19 return 0;
     20 }
    ~
    warning: value computed is not used
    exp.c:14: warning: value computed is not used
    exp.c:14: error: expected ; before ) token
    exp.c:14: error: expected expression before ) token

    Thanks guys.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need two semicolons to sepearate the statements [that is one semicolon per statement] in a for-loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    My book doesn't have semi colons after this example for loop. What the heck? I will try that.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BSmith4740 View Post
    My book doesn't have semi colons after this example for loop. What the heck? I will try that.
    Not after - IN the statements of the for-loop itself, e.g.:

    Code:
    for (exp = -1000; exp <= 1000; exp++)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Code:
    #include <stdio.h>
      2    
      3    int main(void)
      4    {
      5   float base, exp, result;
      6         
      7 printf("Enter base\n");
      8  scanf("%f",&base);
      9  printf("Enter exponent\n");
     10  scanf("%f",&exp);
     11  
     12  for (exp = 0; exp < 1000; exp++)
     13  {
     14   
     15  result = base * exp;
     16  }
     17  printf("Result is %f\n",result);
     18  return 0;
     19  }
     20 
    ~

    A base of four and exponent of four gives me
    3996

    What the heck?

  6. #6
    Registered User
    Join Date
    Jun 2007
    Location
    Rome, NY
    Posts
    24
    Follow the programs logic; you are reading a value into exp and then initializing exp to 0 inside the loop.

    Among some other things, take note that you are assigning a new value to result each time. You want to keep multiplying the base number exp - 1 times.

    Good luck.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I thought you had to ininitalize the variable in the first part of the for statement?

  8. #8
    Registered User
    Join Date
    Jun 2007
    Location
    Rome, NY
    Posts
    24
    Yes, but the fact that you initialize the float exp to 0 after reading in the input from the user.

    Code:
    /* stores a float typed in by the user in exp */
    scanf("&#37;f", &exp); 
    
    /* iterates a loop 1000 times, but this makes the value you typed in as the exponent completely useless. You are overwriting its value with 0. */
    for (exp = 0; exp < 1000; exp++)
    What you want to do is read in exp, and use it as a bound in the loop. You are using it as the index. Use another variable as the index. You seemingly pulled the number 1000 out of thin air.

    Remember what you need to do: you need to multiply the base number exp times.

    i.e. 2^3 = 2 * 2 * 2 = 8

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Thumbs down

    I don't understand what you mean by index.

    What the heck is a nan?

    Code:
    #include <stdio.h>
      2 
      3    int main(void)
      4    {
      5   float base, exp, result, sum;
      6 
      7 printf("Enter base\n");
      8  scanf("%f",&base);
      9  printf("Enter exponent\n");
     10  scanf("%f",&exp);
     11 
     12  for (exp = -100; exp < 100; exp++)
     13  
     14  {
     15  sum *= exp;
     16  result = base * sum;
     17  }
     18  printf("Result is %f\n",result);
     19  return 0;
     20  }
     21 
    ~
    Enter base
    2
    Enter exponent
    2
    Result is nan

  10. #10
    Registered User
    Join Date
    Jun 2007
    Location
    Rome, NY
    Posts
    24
    Maybe you should look back at some unrelated examples of for loops and such. When i say index I mean the variable you are using to increment the loop. There are a number of things wrong, but notably you are declaring a float sum. Then before giving a value to sum you are storing in it the value of sum * exp.

    For example:
    Code:
    float sum;  /* sum has a 'garbage' value, you don't know whats there */
    ...
    sum *= exp; /* you are taking that garbage value and multiplying it by some number, this is surely not what yo want */
    You need to know that simply declaring a variable does not give it a value. Supposing the loop was setup correctly, you would want something like this:

    Code:
    float sum = base;
    
    ...
    /* inside the for loop */
    sum *= base;
    Also, don't forget that x ^ 0 is 1.
    Last edited by BooBoo808; 06-10-2008 at 06:11 PM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > What the heck is a nan?
    NaN is Not A Number.
    Which, considering sum is uninitialised at the point you do
    sum *= exp;

    Other special floats you may see are INF (infinites - really big), IND (indefinites)


    Oh, and can you stop numbering your posts with line numbers. We can't copy the code and try it without lots of code.

    If you need to point out specific lines, then use a comment.
    Eg.
    Code:
    for (exp = -100; exp < 100; exp++) /* line 12 */
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Do yourself a favor: READ your code to yourself or post it here so you know what it does. Then write out in words what the logic should be.
    Compare the two, fix the logic, translate to code again.
    Part of programming is writing the correct syntax and part is to use the correct logic.
    The first is now fine, the second is not fine.
    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.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Smile I am a little confused.

    Guys,
    I have been working on this for two days. I think I need to say
    base = exp somewhere but if I do it after the scanf it going to override the scanf will it not. If I do it before the scanf, the scanf will override the values. Needless to say I am thoroughly confused.



    Code:
    #include <stdio.h>
      2 
      3    int main(void)
      4    {
      5   float base, exp, product, a;
      6   double result;
      7 
      8 
      9 
     10 
     11 printf("Enter base\n");
     12  scanf("%f",&base);
     13  printf("Enter exponent\n");
     14  scanf("%f",&exp);
     15 
     16  printf("The exponent is %f\n",exp);
     17  printf("The base is %f\n",base);
     18  result = 0;
     19 product = 1;
     20  for( a = 1; a <= exp; a++)
     21  {
     22  product *= exp;
     23  printf("The product is %f\n",product);
     24 
     25 
     26  printf("The variable A is equal to %f\n",a);
     27 
     28 
     29 
     30  result = base *  product;
     31  printf("Result is %f\n",result);
     32  if ( 0 == product - base)
     33  return base;
     34 
     35  }
     36 
     37  return 0;
     38  }
    The exponent is 3.000000
    The base is 5.000000
    The product is 3.000000
    The variable A is equal to 1.000000
    Result is 15.000000
    The product is 9.000000
    The variable A is equal to 2.000000
    Result is 45.000000
    The product is 27.000000
    The variable A is equal to 3.000000
    Result is 135.000000

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Think for a minute about multiplying 5^3. You need 5 * 5 * 5. There are no multilplying threes by itself. There is no multiplying 5's by 3's. The only use of the three is to count how many multiplies there are. The only thing that should be multiplying is 5. No 3. Just 5.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Tabstop I have realized that I am missing it. C does not understand ^. It would be nice if it did.

    I want to say base = exp
    for statement
    base*=exp

    What am I missing?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie looking for hints on first audio program...
    By BrownB in forum Game Programming
    Replies: 2
    Last Post: 07-13-2005, 07:25 AM
  2. Newbie program - improvements?
    By -JM in forum C++ Programming
    Replies: 9
    Last Post: 06-26-2005, 06:53 PM
  3. newbie needs help with C++ program
    By cyba in forum C++ Programming
    Replies: 9
    Last Post: 06-25-2004, 02:41 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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