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);
}
|