Thread: Math formula (js -->C++)

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    to tell you the truth, i dont have the slightess clue what a for loop is. I only know Ifs and Whiles and elifs and n00b stuff like that... could som1 plz tell me how to make it so u input the lvl number instead of it just outputting a bunch of lvls

  2. #17
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A for-loop repeats a piece of code a set amount of times. Read the tutorials @ http://faq.cprogramming.com/cgi-bin/smartfaq.cgi for indepth explanations.
    Code:
    int Exp = 0;
    
    for(X = 1; X < L; X++)
    {
       Exp += (X + (int)(300.0 * pow(2, ((double)X / 7.0))));
    }
    
    Exp /= 4;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    but i dont want to loop it... i just want to do it once

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    i've tried for hours to make it work without a for loop but whenever i do the results dont come out right...

  5. #20
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Code:
    ...
    for (lvl = 1; lvl <= maxlevel; outputnum = Math.floor(points / 4), lvl++) {
    	diff = Math.floor(lvl + 300 * Math.pow(2, lvl / 7));
    	points += diff;
    	...
    }
    This code is performing the operations
    outputnum = Math.floor(points / 4)
    diff = Math.floor(lvl + 300 * Math.pow(2, lvl / 7));
    points += diff;
    several times: once for level 1, and once more for every level up to and including maxlevel. So, what you want to do looks something like this in C...


    Code:
    float func2(float maxlevel)  //is maxlevel an int or unsigned int instead, perhaps?  you probably don't need a float
    {
    ...
    for (float lvl=0; lvl <= maxlevel; lvl++)  //probably don't need a float for lvl either
    {
            diff = floor(lvl + 300 * pow(2, lvl / 7));
            points += diff;
    }
    return(floor(points/4);)  //just returning the value of outputnum instead of using a variable
    Away.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    this is what i tried:

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<math.h>
    
    
    
    
    float func2(float maxlevel)//is maxlevel an int or unsigned int instead, perhaps?  you probably don't need a float
    {
        float diff,points,x;
        for (float lvl=0; lvl <= maxlevel; lvl++)  //probably don't need a float for lvl either
        {
            diff = floor(lvl + 300 * pow(2, lvl / 7));
            points += diff;
            x=floor(points/4);
            if(lvl == maxlevel)
            {
                    cout <<"\n"<< x;
                    getch();
            }
            
            //just returning the value of outputnum instead of using a variable
        }
        
        
    }
    
    int main()	
    {
        int maxlevel = 1;
        cout << "Enter max lvl: ";
        cin >> maxlevel;
        func2(maxlevel);
    }
    and it didnt work
    i tried typing in 52... and it outputted: 136,669
    it should have come out as 123,660

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  3. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM