-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;
}
4
  • Considering both those loops iterate over the same range you should be able to combine them. Oct 16, 2014 at 7:05
  • You could also be creative and tell the user to enter both indexes in 1 5 format. Then read the string. Oct 16, 2014 at 7:08
  • Is this some homework problem?
    – Mikhail
    Oct 16, 2014 at 7: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] Oct 16, 2014 at 7:58

2 Answers 2

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.

-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?

4
  • -1: wrapping every trivial output statement in a function just adds fluff to your program, clouding what happens. It would be better to rewrite the logic if repetition is a problem.
    – rubenvb
    Oct 16, 2014 at 7: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 Oct 16, 2014 at 7: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. Oct 16, 2014 at 7: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. Oct 16, 2014 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.