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

This is a discussion on Priting some of the for loop results (but not all of them) within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've encountered the following problem: Inside a for loop I calculate recursively the members of a mathematical series, ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

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