-2

I am interested to see the way the computer processes the output of the cout command (i.e. how it goes through the various iterations for each row) in the following program. Is it possible to decrease the speed of the process?

#include <iomanip>
#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    for (int x=1; x <= 12; x++)
    {
        for (int y=1; y <= 12; y++)
            cout << setw(4) << x*y; cout << endl;
    }
}
developer
  • 283
  • 2
  • 8
  • 2
    [`std::this_thread::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for) – WhozCraig Nov 16 '19 at 07:18
  • 1
    If you attach a debugger, you can step through the code one line at a time. – Kai Nov 16 '19 at 07:33
  • @Kai thanks. Can you give a hint or reference on how? – developer Nov 16 '19 at 07:38
  • 1
    https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb https://stackoverflow.com/questions/8041614/how-to-debug-in-codeblocks/58050981#58050981 – Yunnosch Nov 16 '19 at 07:57
  • If you only want to peruse the output in peace, then why don't you redirect into a file and read that with your favorite editor? – Yunnosch Nov 16 '19 at 07:57
  • @Yunnosch Does that demonstrate how (i.e. the process) the ouput is actually printed out? – developer Nov 16 '19 at 08:13
  • I a buffer-and-flush environment I doubt that anything will. In that case you might want to use a debugger and watch "from the inside". I see your point now. Does the accepted answer give you that? You'd have to much more flushing within lines... – Yunnosch Nov 16 '19 at 08:40

1 Answers1

1
#include <iomanip>
#include <iostream>
#include <thread>       // std::this_thread::sleep_for

// using namespace std; // dont get used to using this

int main() {            // if you are not using the arguments, leave it like this
    for(int x = 1; x <= 12; x++) {
        for(int y = 1; y <= 12; y++) std::cout << std::setw(4) << x * y;
        std::cout << '\n'; // just some advice: replace all your "endl"s with '\n'
                           // until flushing is needed.

        // here's one way to make it slower that WhozCraig mentioned in a comment:
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thank you very much. That is what I needed. I only had to increase the milliseconds to 500 for my computer. – developer Nov 16 '19 at 07:37
  • @developer You are welcome, but - "for my computer" makes it sound like you have a totally different problem. No computer needs 500 ms sleeps really :-) – Ted Lyngmo Nov 16 '19 at 07:40
  • I thought maybe depending on the CPU speed the time in milliseconds had to be adjusted. – developer Nov 16 '19 at 07:41
  • @developer Indeed, if there's some synchronizing to be done haphazardly. What did this sleep solve? – Ted Lyngmo Nov 16 '19 at 07:43
  • Actually, I am beginning to learn coding in cpp. The book I am reading, Programming with C++ by John R. Hubbard, PhD gives us this example on page 69. I was not sure if I had fully understood the author, so I needed to see this in practice. I mean, I was not sure if each line of the multiplication table is printed horizontally, then the next line is started. Now I see, with your help, that I had understood correctly. – developer Nov 16 '19 at 07:48
  • @developer Aha, got it. By depickling stuff like this, you are going to get a good understanding. +1 If you had asked this question many years ago, the `endl` that I suggested that you should remove may have slowed down your program enough for you to never have asked this to begin with. – Ted Lyngmo Nov 16 '19 at 07:53
  • "I thought maybe depending on the CPU speed the time in milliseconds had to be adjusted." Why would you think that differently fast CPUs would take shorter or longer with sleeping for a specified number of milliseconds? It is different of course for "count to a million", but any accepted implementation of `sleep()` gets it right. Give or take a few milliseconds for Operating Systems which don't have decent timers. – Yunnosch Nov 16 '19 at 08:02
  • 1
    @ Ted Lyngmo Yes, absolutely! :) As much as I expect my computer to be fast in other areas, I would like it to slow down a bit in demonstrating the output of some code for me! – developer Nov 16 '19 at 08:05
  • 1
    You need to drop the blank between "@" and name. Otherwise the pining does not work. – Yunnosch Nov 16 '19 at 08:40
  • @Yunnosch Does developer get an automatic ping just for having been in this commenting section? I don't think so, but I could be wrong. – Ted Lyngmo Nov 16 '19 at 09:49
  • @TedLyngmo Close call. But I am convinced that as the asker, they get a ping. And sorry, as the answerer so do you and I did not mean you. But you obviously guessed. – Yunnosch Nov 16 '19 at 09:52
  • @Yunnosch There was no doubt :-) Cheers! – Ted Lyngmo Nov 16 '19 at 09:53