Printing 5 values per line

When I print this array, how can I print 5 values per line?

1
2
3
4
  void printarray(int q,double numb[]) { //This function prints array
    for(int i=0;i<q;i++) {
        cout << numb[i] << endl;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

int main()
{
    int myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                        15, 16, 17, 18, 19, 20};
    size_t i{};
    while (i < sizeof(myArray)/sizeof(myArray[0]))
    {
        size_t j{};
        while (j < 5)
        {
            std::cout << std::left << std::setw(5) << myArray[i];
            ++i;
            ++j;
        }
        std::cout << "\n";
    }
}
How can I do it without using <iomanip> ?
iomanip just formats the output to look 'nice' and it is not C++11 in case you can't use +11 and beyond:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    int myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                        15, 16, 17, 18, 19, 20};
    size_t i{};
    while (i < sizeof(myArray)/sizeof(myArray[0]))
    {
        size_t j{};
        while (j < 5)
        {
            std::cout << myArray[i] << " ";
            ++i;
            ++j;
        }
        std::cout << "\n";
    }
}
gunnerfunner, your code assumes that the array size is a multiple of 5.
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
#include <iostream>
#include <iomanip>

using std::cout;

void printarray(size_t q,double numb[]) //This function prints array
{
    if (q == 0) return;		// nothing to print
    
    cout << numb[0];
    // i is the index into numb[]. perLine is the number printed on the current line
    for (size_t i=1, perLine=1; i<q; ++i, ++perLine) {
	// print a newline or a space before the current number.
	if (perLine == 5) {
	    perLine = 0;	// also reset perLine
	    cout << '\n';
	} else {
	    cout << ' ';
	}

	// print the number
	cout << numb[i];
    }
}


int main()
{
    double myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14.7,
		     15, 16, 17, 18, 19, 20};
    printarray(sizeof(myArray)/sizeof(myArray[0]), myArray);
}




1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

void printarray( size_t q, double a[], int cols )
{
   for ( size_t i = 1; i <= q; i++ ) cout << a[i-1] << ( i != q && i % cols ? '\t' : '\n' );
}

int main()
{
   double myArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 };
   printarray( sizeof( myArray ) / sizeof( myArray[0] ), myArray, 5 );
}

Last edited on
jonnin - thanks, updated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

constexpr auto lineSize = 5;

int main()
{
    int myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                        15, 16, 17};
    size_t i{};
    while (i < sizeof(myArray)/sizeof(myArray[0]))
    {
        size_t j{};
        while (i < sizeof(myArray)/sizeof(myArray[0]) && j < lineSize)
        {
            std::cout << myArray[i];
            ++i;
            ++j;
            std::cout << ((i % lineSize == 0) ? "\n" : "\t");}
    }
}
This is the entire program, maybe it will help clarify the solution. I'm slightly confused about some of the above solution.


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
/*          


   The main program will call a series of functions to process a set of data.
   One function will read data into an array.  A second function will print the
   values stored in an array in a neat format. Another function will find the
   average of the values stored in an array.  A fourth function will construct
   a new array from an existing array.
*/

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

void readdata(int n, double first[]) {  //This function inputs data from a file
    ifstream infile("data.txt");
    double temp;
    for(int i=0;i<n;i++) {
        infile >> temp;
        first[i] = temp;
    }
}


void printarray(int q,double numb[]) { //This function prints array
    for(int i=0;i<q;i++) {
        cout << numb[i] << endl;
    }
}

double findaverage(int k,double p[]) {  //This function finds an average of array
    double sum = 0;
    for(int i=0;i<k;i++) {
        sum+=(p[i]);
    }
    double average = sum/k;
    return average;
}

void howfaraway(int m,double average,double r[],double s[]) { //Determines how far an element
    for(int i=0;i<m;i++) {                                   //is from the average
        s[i] = average-r[i];
    }
}

int main() {

    cout << "Eugene Sokoletsky Assignment No.5" << endl;

    int size;
    cin >> size;
    double first[size];
    readdata(size,first);
    cout << "Here is the original array" << endl;
    printarray(size,first);
    double average = findaverage(size,first);
    cout << "The average is " << average << endl;
    double second[size];
    howfaraway(size,average,first,second);
    cout << "Here is the new array" << endl;
    printarray(size,second);
    cout << "The average is " << findaverage(size,second) << endl;
    return 0;
}
This is an error in C++ (though it is allowed in C99); a conforming compiler would generate an error:
1
2
3
    int size;
    std::cin >> size;
    double first[size]; // *** error in C++ (size is not a constant known at compile time) 

http://coliru.stacked-crooked.com/a/c4108a8ab423bd8f
(note: in its default mode, the GNU compiler is non-conforming wrt this)

Something along these lines, perhaps:
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
/*
   The main program will call a series of functions to process a set of data.
   One function will read data into an array.  A second function will print the
   values stored in an array in a neat format. Another function will find the
   average of the values stored in an array.  A fourth function will construct
   a new array from an existing array.
*/

#include <fstream>
#include <iostream>

// return the number of items read (up to a max of maxsz )
int readdata( double arr[], int maxsz ) {  //This function inputs data from a file

    std::ifstream infile("data.txt");
    int cnt = 0 ; // number of items read
    while( cnt < maxsz && infile >> arr[cnt] ) ++cnt ;
    return cnt ;
}

void printarray( const double numb[], int sz ) { //This function prints array

    const int N = 5 ; // values per line

    for( int i = 0 ; i < sz ; ++i )
    {
        std::cout << numb[i] << ' ' ;
        if( i%N == (N-1) ) std::cout << '\n' ; // put a new line after every N values
    }

    if( sz%N != 0 ) std::cout << '\n' ; // put a final new line if required
}

double findaverage( const double numb[], int sz ) {  //This function finds an average of array

    double sum = 0;
    for( int i=0; i<sz; ++i ) sum += numb[i] ;

    const double average = sum/sz ; // NaN if sz == 0
    return average;
}

void howfaraway( const double srce[], double dest[], int sz, double average ) { //Determines how far an element

    for( int i=0; i<sz; ++i ) dest[i] = average - srce[i] ;  //is from the average
}

int main() {

    std::cout << "Eugene Sokoletsky Assignment No.5\n" ;

    const int MAX_SIZE = 1024 ;
    double first[MAX_SIZE];

    const int actual_size = readdata( first, MAX_SIZE );
    std::cout << "Here is the original array\n" ;
    printarray( first, actual_size );

    const double average = findaverage( first, actual_size );
    std::cout << "The average is " << average << '\n';

    double second[MAX_SIZE];
    howfaraway( first, second, actual_size, average );
    std::cout << "Here is the new array\n" ;
    printarray( first, actual_size );
}
Last edited on
Topic archived. No new replies allowed.