Thread: Problem with a mathematical formula

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Angry Problem with a mathematical formula

    Hi, I'm trying to implement this formula :

    http://olimpiadi.ing.unipi.it/oliFor...d23ebfa3f5.gif (the B at the end means that it is expressed in Bytes)

    Let say that in my code
    length = a
    maxLength = k


    what's wrong?? I keep having 0.0 and if I debug I don't understand why I'm having 0.0 suddenly after having a positive number in the 2 variables..

    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
      int maxLength = 6;
      int length = 21;
      
      double Mb = (((((pow (length, maxLength + 1)) *
    		  (length * maxLength - maxLength - 1) + length) /
    		 (pow ((length - 1), 2))) / 1024) /1024);
    		 
      double Gb = Mb / 1024;
      
      printf("The whole generated traffic will be of %.1f Gb (%.1f Mb).\n\n",Gb,Mb);
      system("PAUSE");  
      return 0;
    }
    Is it the implementation bugged? Or I've done something stupid? Or both?

    thanx
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i think you did something stupid...

    try %0.1f before anything else
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <math.h>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    d'ho!!!!!! it was math.h
    thanx guys
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    .................................................. .........*blink*.................................. ............

    how did it compile?.......and what difference would it make (assuming it actually did compile)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by misplaced
    how did it compile?.......and what difference would it make (assuming it actually did compile)
    Code:
    Warning W8065 test.c 7: Call to function 'pow' with no prototype in function main
    Warning W8065 test.c 9: Call to function 'pow' with no prototype in function main
    Warning W8065 test.c 14: Call to function 'system' with no prototype in function main
    Warning W8057 test.c 16: Parameter 'argc' is never used in function main
    Warning W8057 test.c 16: Parameter 'argv' is never used in function main
    Default int return for an unprototyped function.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    my devcpp and gcc didnt say a word about that, it was for this that I was confused!
    This forum is the best one I've ever seen. Great ppl, great coders

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    gcc 3.4.3

    Code:
    C:\temp>gcc err.cpp -o err.exe
    err.cpp: In function `int main(int, char**)':
    err.cpp:7: error: `pow' undeclared (first use this function)
    err.cpp:7: error: (Each undeclared identifier is reported only once for each fun
    ction it appears in.)
    err.cpp:14: error: `system' undeclared (first use this function)
    dev-cpp
    Code:
    g++.exe -c main.cpp -o main.o -I"C:/COMPILERS/BLOODSHED/lib/gcc/mingw32/3.4.2/include"  -I"C:/COMPILERS/BLOODSHED/include/c++/3.4.2/backward"  -I"C:/COMPILERS/BLOODSHED/include/c++/3.4.2/mingw32"  -I"C:/COMPILERS/BLOODSHED/include/c++/3.4.2"  -I"C:/COMPILERS/BLOODSHED/include"   
    
    main.cpp: In function `int main()':
    main.cpp:10: error: `pow' undeclared (first use this function)
    main.cpp:10: error: (Each undeclared identifier is reported only once for each function it appears in.)
    main.cpp:18: error: `system' undeclared (first use this function)
    
    Execution terminated
    Last edited by misplaced; 04-29-2005 at 09:20 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    it looks like u compiled a cpp file, i compiled a c file
    This forum is the best one I've ever seen. Great ppl, great coders

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    good eye.......

    no errors with .c
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    that's bad, I mean... why the hell should it happen? as far as you guys know, is there a way to extends the controls that the C compiler does?
    This forum is the best one I've ever seen. Great ppl, great coders

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    sure!

    try
    gcc file.c -o file.exe -Wall

    (-Wall - warn all)

    google gcc command line switches
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  13. #13
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    works with borland.

    C:\borland\bcc55\bin>bcc32 mathform1.c
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    mathform1.c:
    Warning W8065 mathform1.c 17: Call to function 'system' with no prototype in fun
    ction main
    Warning W8057 mathform1.c 19: Parameter 'argc' is never used in function main
    Warning W8057 mathform1.c 19: Parameter 'argv' is never used in function main
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

    C:\borland\bcc55\bin>mathform1
    The whole generated traffic will be of 0.5 Gb (511.0 Mb).

    Press any key to continue . . .

    C:\borland\bcc55\bin>

    Code:
    /* mathform1.c */
    
    #include<math.h>
    #include<stdio.h>
    
    int main (int argc, char *argv[]) {
      int maxLength = 6;
      int length = 21;
      
      double Mb = (((((pow (length, maxLength + 1)) *
    		  (length * maxLength - maxLength - 1) + length) /
    		 (pow ((length - 1), 2))) / 1024) /1024);
    		 
      double Gb = Mb / 1024;
      
      printf("The whole generated traffic will be of %.1f Gb (%.1f Mb).\n\n",Gb,Mb);
      system("PAUSE");  
      return 0;
    }
    you do not need the " (int argc, char *argv[]) "

    int main() should work fine.
    Last edited by kryptkat; 04-29-2005 at 05:40 PM.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    (int argc, char *argv[]) is typically automatically created when you create a project in Dev-c++....it's not worth the extra keystrokes to delete it, so most people leave it. i see nothing wrong with it (other than it taking up a few extra byte... oh no)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with mathematical problem in C
    By feelseez in forum C Programming
    Replies: 3
    Last Post: 09-10-2006, 11:44 AM
  2. Mathematical, nautical steering problem
    By Warlax in forum Game Programming
    Replies: 6
    Last Post: 06-16-2006, 06:01 PM
  3. Mathematical problem
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2006, 02:38 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM