Home > Mobile >  How to display output in rows of five numbers?
How to display output in rows of five numbers?

Time:10-25

I'm new to programming and I have to display all the prime numbers that are the product of this code in rows of five. After too many hours of trying to find something online, this is what I came up with. This way, not even the prime numbers are being displayed in the end; only 1s all the way. I'd be happy to know what I did wrong or what I could change.

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

int main() {
    int n { 0 };
    cout << "Please enter an initial value n<2000 in order for the calculation to begin: " << endl;
    cin >> n;

    vector<bool> cygnus(n   1);               
    for (int m = 0; m <= n; m  ) {
        cygnus[m]=true;
    }

    for (int j = 2; j < n; j  ) {
        if (cygnus[j] == true) {
            for (int i = j   1; i <= n; i  ) {
                if (i % j == 0) {
                    cygnus[i] = false;
                }
            }
        }
    }
    
    int s = 0;
    for (auto value : cygnus) {
        if (value == true && s > 0) {
            for (int counter = s; counter  ; ) {
                if (counter % 5 == 0) {
                    cout << setw(3) << s << "  \n ";
                }
                
                if (counter % 5 != 0) {
                    cout << setw(3) << s << "  ";
                }
            }
        }

        s  ;
    }

    cout << endl;
    return 0;
}

CodePudding user response:

You are seriously over-complicating your output logic. Just have a counter variable declared (and initialized to zero) outside the for loop that does the output and then, every time you print a number, increment it. When that reaches the value of 5, print a newline and reset it to zero.

Some other points:

  1. As mentioned in the comments, your m <= n and i <= n will cause out-of-bounds accesses on the last iterations of the respective loops; you should just use the < operator, instead.

  2. You can speed up the program in many ways, using better prime-detection algorithms; however, one simple thing you can do is to terminate the inner for loop once you have found that the number is not prime, using a break statement.

  3. The STL containers (like std::vector) use the size_t type (not int) for their sizes and indexes. In the code below, I have changed all your int variables to this type; fortunately, that won't affect your algorithm.

Note also that 1 is not a prime number.

Here's a re-worked version of your code:

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

int main()
{
    size_t n{ 0 };
    cout << "Please enter an initial value n<2000 in order for the calculation to begin: " << endl;
    cin >> n;

    vector<bool>cygnus(n   1);
    for (size_t m = 0; m < n; m  ) { // m <= n will go out-of-bounds
        cygnus[m] = true;
    }

    for (size_t j = 2; j < n; j  ) {
        if (cygnus[j] == true) {
            for (size_t i = j   1; i < n; i  ) { // i <= n will go out-of-bounds
                if (i % j == 0) {
                    cygnus[i] = false;
                    break; // Once we have this, we can skip the rest of the inner loop!
                }
            }
        }
    }
    size_t s = 0;
    size_t counter = 0;
    for (auto value : cygnus) {
        if (value == true && s > 1) { // Note that 1 is NOT a prime number
            cout << setw(3) << s << "  ";
            if (  counter == 5) {
                cout << "\n ";
                counter = 0;
            }
        }
        s  ;
    }
    if (counter != 0) cout << "\n "; // Add newline for any partial last line.
    cout << endl;
    return 0;
}
  • Related