Thread: Priting some of the for loop results (but not all of them)

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Priting some of the for loop results (but not all of them)

    Hey guys,
    I've encountered the following problem:
    Inside a for loop I calculate recursively the members of a mathematical series, so that the result of each new calculation is dependant on the previouos calculation. I need to print (in one step) the calculation results for n = 1, 10, 100 and 1000 only, without printing all the calculations of the loop. How do I do that - I'm a beginner so I only know how to print each and every step of the calculation.

    Thanks,
    N.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    If your result is given by x ...and the index by n..
    Change
    "cout<<x;"
    to
    "if(n==10||n==100||n==1000)cout<<x;"


    Btw... you should post the code... as something smells wrong with your "recursion inside for loop" logic.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    I'm trying to answer forever, but something goes wrong..Firsy=t of all, we're not allowed to use if yet. Here is the partia code:

    insert
    Code:
     
    int main(int argc, char* argv[])
    {
    float Acurr = 1, Aest;
    float i, j;
    int k, l = 0;
      for (i = 1, j = 2, k = 0; j < 10, k < 1001; i = Acurr, j++, k++){
      //l++;
      Acurr = i + sqrt(i) ;
      Aest = (pow(j,2))/4 ;
         cout << "for n =  " << k + 1  << endl;
         cout << "An as calculated by the series is:  " << Acurr << endl;
         cout << "An as calculated by the estimate is:  "  << Aest << endl;
         cout << "The absolute difference between the two calculations is:  " << abs(Acurr - Aest);
         cout << endl;
         //}
    }
    system ("pause");
            return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nikky
    Firsy=t of all, we're not allowed to use if yet.
    Yet? It is weird for a student to be taught loops before if statements. I suggest that you check with your teacher; perhaps your teacher merely wants you to use a loop rather than a gigantic series of if statements, but an if statement within the loop body is permissible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two comparable loop but different results...help please
    By spottedzebra in forum C Programming
    Replies: 1
    Last Post: 09-09-2010, 11:43 PM
  2. ADC #2 Results
    By Prelude in forum Contests Board
    Replies: 20
    Last Post: 08-11-2003, 06:59 AM
  3. ADC #1 Results
    By Prelude in forum Contests Board
    Replies: 26
    Last Post: 08-06-2003, 03:57 AM
  4. I got my Results..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 06-29-2003, 10:46 AM
  5. Borland c++ quick reports priting
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2002, 07:22 PM