Printing 20 Numbers per Line

I have the following program and everything is working great but how would I get it to only print 20 numbers per line and after the 20 numbers skip to the next line to finish printing the numbers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const int SIZE=101;//0 to 100 set containing 101 elements
class IntegerSet
{
        int a[SIZE];
    public:
        IntegerSet();
        IntegerSet(int *, int);
        IntegerSet unionOfSets(IntegerSet s2);
        IntegerSet intersectionOfSets(IntegerSet s2);
        int isEqualTo(IntegerSet s2);
        void deleteElement(int n);
        void insertElement(int n);
        void printSet();
};
IntegerSet::IntegerSet()//default constructor creates empty set
{
    int i;
    for(i=0;i<SIZE;i++)
        a[i]=0;
}
IntegerSet::IntegerSet(int *ptr, int n)//initialize object using another array of integers
{
    int i;
    for(i=0;i<SIZE;i++)//Initialize the set first
        a[i]=0;
    for(i=0;i<n;i++)
        a[(ptr[i])]=1;
}
IntegerSet IntegerSet::unionOfSets(IntegerSet s2)
{
     IntegerSet s3;
     int i;
     for(i=0;i<SIZE;i++)
        if(a[i]==1 || s2.a[i]==1)
            s3.a[i]=1;
     return s3;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet s2)
{
     IntegerSet s3;
     int i;
     for(i=0;i<SIZE;i++)
        if(a[i]==1 && s2.a[i]==1)
            s3.a[i]=1;
     return s3;
}
int IntegerSet::isEqualTo(IntegerSet s2)
{
    int i;
    int check=1;//equal
    for(i=0;i<SIZE;i++)
     if(a[i]!=s2.a[i])
     {
        check=0;//not equal
        break;
     }
    return check;
}
void IntegerSet::deleteElement(int n)
{
    if (n<SIZE && n>=0) //check validity
        a[n]=-1;
}
void IntegerSet::insertElement(int n)
{
    if (n<SIZE && n>=0) //check validity
        a[n]=1;
}
void IntegerSet::printSet()
{
    int i, count=0;
    for(i=0;i<SIZE;i++)
        if(a[i]==1)
        {
            count++;
            cout<<i<<" ";
        }
    if(count==0)
        cout<<"---";
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "IntegerSet.h"

int main()
{
    // IntegerSet holds integers in the range 0..100
    const int maxInteger = 100;

    // Test default constructor --
    // Create an empty set s
    IntegerSet s;
    cout << "Initial empty set: " << endl;
    s.printSet();
    cout << endl;
     
    // Test insertElement function --
    // Insert powers of 5 into Integer set s
    for (int i = 0; i <= maxInteger; i += 5)
    {
        s.insertElement(i);
    }
    cout << "Inserted powers of 5: " << endl;
    s.printSet();
    cout << endl;

    // Test deleteElement Function -- 
    // Delete powers of 10 from IntegerSet s	
    for (int i = 0; i <= maxInteger; i+= 10)
    {
        s.deleteElement(i);
    }
    cout << "Deleted powers of 10: " << endl;
    s.printSet();
    cout << endl;

    // Create a set of even integers
    IntegerSet evenSet;
    for (int i = 0; i <= maxInteger; i+=2)
    {
        evenSet.insertElement(i);
    }
    cout << "Even Integers: " << endl;
    evenSet.printSet();
    cout << endl;

    // Create a set of odd integers
    IntegerSet oddSet;
    for (int i = 1; i <= maxInteger; i+=2)
    {
        oddSet.insertElement(i);
    }
    cout << "Odd Integers: " << endl;
    oddSet.printSet();
    cout << endl;

    // Test unionOfSets Function --
    // Print union of even and odd integers which
    // results in a set of all integers between 0..100
    s = evenSet.unionOfSets(oddSet);
    cout << "Union of Even and Odd Integers: " << endl;
    s.printSet();
    cout << endl;

    // Test intersectionOfSets Function --
    // Print intersection of even and odd integers which
    // results in an empty set
    s = evenSet.intersectionOfSets(oddSet);
    cout << "Intersection of Even and Odd Integers: " << endl;
    s.printSet();
    cout << endl;

    // Test isEqualTo Function --
    // Even integers should not be equal to odd integers.
    if (evenSet.isEqualTo(oddSet))
    {
        cout << "Even Integer Set is equal "
             << "to Odd Integer Set." << endl;
    }
    else
    {
        cout << "Even Integer Set is NOT equal "
             << "to Odd Integer Set." << endl;
    }

    // Test isEqualTo Function --
    // Set s, the intersection of even and odd integers,
    // should equal the empty set.
    IntegerSet emptySet;
    if (s.isEqualTo(emptySet))
    {
        cout << "Intersection of even and odd integers "
             << "is equal to the Empty Set." << endl;
    }
    else
    {
        cout << "Intersection of even and odd integers "
             << "is NOT equal to the Empty Set." << endl;
    }
    cout << endl;

    // Test constructor from array of integers --
    // Create a set of prime numbers between 0 to 100
    const int numPrimes = 25;
    int primes[numPrimes] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 
                             31, 37, 41, 43, 47, 53, 59, 61, 67, 
                             71, 73, 79, 83, 89, 97};
    IntegerSet primeSet(primes, numPrimes);
    cout << "Prime Integers: " << endl;
    primeSet.printSet();
    cout << endl;

	system("pause");
    return 0;
}
That's a lot of code to read.

But generally, keep track of how many numbers you've written (written++ after every write). Then:
if (written>=20) written = 0, cout << '\n';
Last edited on
Topic archived. No new replies allowed.