-1

I want to reduce the number of "cout"s used in this program..So far I have 9 "cout"s in my program... First I had 11 "cout"s,but I could reduced to 9 by placing them inside a method. But still the over 6..

#include <iostream>
#include <iomanip>
using namespace std;

static void Endline(){
     cout << endl;
}

void printHeader(int start, int end) {
     cout << setw(4) << " ";
     for (int firstrow = start; firstrow <= end; firstrow++) {
        cout << setw(3) << firstrow << " ";
     }
     // cout << endl;
     Endline();

     // Second line, lead space then proper number of dashes
     cout << setw(4) << " ";
     for (int secondrow = start; secondrow <= end; secondrow++) {
        cout << "****";
     }
     //cout << endl;
     Endline();
}

int main() {
     int start;
     int end;

     // Collect start and end of listing
     cout << "Enter a start index (integer): " << endl;
     cin >> start;

     cout << "Enter an end index (integer): " << endl;
     cin >> end;

     for (int i = start; i <= end; i++) {
        // Executes once to print header
        if (i == start) {
            printHeader(start, end);
        }

        // Now loop through columns
        for (int j = start; j <= end; j++) {
            // Once per row create first column heading
            if (j == start) {
                cout << setw(3)<<  i << "|";
            }

            // Multiply row and column
            cout << setw(3) << (i * j) << " ";
        }

        // End row
        //cout << endl;
        Endline();
      }

     return 0;
}
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Considering both those loops iterate over the same range you should be able to combine them. – ChiefTwoPencils Oct 16 '14 at 07:05
  • You could also be creative and tell the user to enter both indexes in `1 5` format. Then read the string. – ChiefTwoPencils Oct 16 '14 at 07:08
  • Is this some homework problem? – Mikhail Oct 16 '14 at 07:48
  • Sounds like programming golf (write this code in as few lines as possible, use the least number of function X, etc), and just like golf as a sport, it's utterly useless in real life... Concentrate on the logic of your code, write it clearly and logically, keep it dry (Don't Repeat Yourself). [I could rewrite your code such that it uses `cout` once, but it would be of absolutely no benefit, and you'd be passing around a `ostream&` everywhere] – Mats Petersson Oct 16 '14 at 07:58

2 Answers2

0

If you stream the whole table into a std::stringstream and cout the result, you could reduce the number of couts to 3:

#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

void printHeader(stringstream& result, int start, int end) {
    result<< setw(4) << " ";
    for (int firstrow = start; firstrow <= end; firstrow++) {
        result<< setw(3) << firstrow << " ";
    }
    result<< endl;

    // Second line, lead space then proper number of dashes
    result << setw(4) << " ";
    for (int secondrow = start; secondrow <= end; secondrow++) {
        result << "****";
    }
    result << endl;
}

int main() {
    int start;
    int end;

    // Collect start and end of listing
    cout << "Enter a start index (integer): " << endl;
    cin >> start;

    cout << "Enter an end index (integer): " << endl;
    cin >> end;

    stringstream result;

    for (int i = start; i <= end; i++) {
        // Executes once to print header
        if (i == start) {
            printHeader(result, start, end);
        }

        // Now loop through columns
        for (int j = start; j <= end; j++) {
            // Once per row create first column heading
            if (j == start) {
                result<< setw(3)<<  i << "|";
            }

            // Multiply row and column
            result << setw(3) << (i * j) << " ";
        }

        // End row
        result<< endl;
    }
    cout << result.str();
    return 0;
}

But: Why do you actually want to reduce the couts? According to this question cout seems to run with good performance.

Community
  • 1
  • 1
Stephan
  • 4,187
  • 1
  • 21
  • 33
-1

What you want to do is reduce the amount of repetition in your code. This is something you always want to do when you program.

So, everytime you see two blocks of code that look alike (sometime bescause they come from a copy-paste), you should search for a way to write them as one function.

 cout << setw(3)<<  i << "|";

and

cout << setw(3) << (i * j) << " ";

and

cout << setw(3) << firstrow << " ";

are nearly the same. You can write them as a function taking as parameter the number to print, and the end character.

Similarly,

 cout << setw(4) << " ";

appears twice,

and

 cout << "Enter a start index (integer): " << endl;

appears twice also. You can wrap both in a function that you will call twice, like you did for the endl character.

You can thus remove 4 couts, this is enough right?

Ernest_Galbrun
  • 2,514
  • 3
  • 21
  • 33
  • -1: wrapping every trivial output statement in a function just adds [fluff](http://www.urbandictionary.com/define.php?term=fluff&defid=1829015) to your program, clouding what happens. It would be better to rewrite the logic if repetition is a problem. – rubenvb Oct 16 '14 at 07:33
  • 1
    the irony is that `setw` already is a function: `/* unspecified */ setw(int n)` which is being passed to `operator<<(std::ostream, ...)`, so there is nothing to gain from wrapping it even further – TemplateRex Oct 16 '14 at 07:35
  • I would say if you suggest to someone to use a function to output you also explain how it's not nice to bind someone to a stream; tell them to pass it to the function. – ChiefTwoPencils Oct 16 '14 at 07:45
  • Well, this question looks like a class problem to me, and given what I am told I have the feeling that wraping calls to cout into functions was what was expected. I don't think OP really needs to have 6 and no more calls to cout in his program. I think so especially since that is what was done (by the teacher,as a clue?) with endl printing. – Ernest_Galbrun Oct 16 '14 at 11:51