formating output to new lines
Nov 30, 2010 at 2:08am UTC
The program i am working on is taking an array of 50 numbers and i need the output to display 10 numbers and then start a new line. What do i need to add to the cout statement to get it to print that way?
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 <conio.h>
#include <iomanip>
using namespace std;
void ArrayTest (int , double []);
double numbers[50];
int i;
void ArrayTest (int i, double numbers[])
{
for (i = 0; i < 50; i++)
{
if (i <= 24)
numbers[i] = i * i;
if (i >= 25)
numbers[i] = i / 2;
}
}
int main()
{
ArrayTest (i, numbers);
for (i = 0; i < 50; i++)
cout << numbers[i] << " " << '\n' ;
getch();
return 0;
}
Nov 30, 2010 at 3:46am UTC
Are you trying to display 5 rows with 10 numbers on each row? If so, you just have to alter your cout statement. I'm sure there is a way to do this more efficiently but this works.
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
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
void ArrayTest ();
double numbers[50];
int i;
void ArrayTest ()
{
for (i = 0; i < 50; i++)
{
if (i <= 24)
numbers[i] = i * i;
if (i >= 25)
numbers[i] = i / 2;
}
}
int main()
{
ArrayTest ();
for (i = 0; i < 10; i++)
cout << numbers[i] << " " ;
cout<< endl;
for (i = 10; i < 20; i++)
cout << numbers[i] << " " ;
cout<< endl;
for (i = 20; i < 30; i++)
cout << numbers[i] << " " ;
cout<< endl;
for (i = 30; i < 40; i++)
cout << numbers[i] << " " ;
cout<< endl;
for (i = 40; i < 50; i++)
cout << numbers[i] << " " ;
return 0;
}
Nov 30, 2010 at 4:08am UTC
Thanks! This solves the problem for now. I know there has to be a way to do this more efficiently, but this gets the job done.
Topic archived. No new replies allowed.