# Printing an array in lines of 10.

• 11-16-2004
furiousferret
Printing an array in lines of 10.
I've tried several ways to do this but I either copy one variable 10 times or they all go on the same line. How do I display an array of 50 with 10 per line?

Here is the code I already have

Code:

```#include <iostream> #include <cmath> using namespace std; const int arraySize = 51; int main() {         double alphaArray[arraySize];         for (int i=1; i < 51; i++)         {             alphaArray[i]= i;      // Store a number in each array element                                 if( i < 25)                 {                         cout << sqrt(i) << ' ' <<endl;                 }                 else                 {                         cout << i * 3 << endl;                 }         }         return 0; }```
any help would be appreciated.
• 11-16-2004
What you need to do is only output the endl when you want the line to end, so

cout << number << " ";

ten times then

cout << endl;

• 11-16-2004
furiousferret
I've been doing that with

cout << alphaArray[0] << sqrt(i) << ' ' ;
cout << alphaArray[1] << sqrt(i) << ' ' ;
cout << alphaArray[2] << sqrt(i) << ' ' ;
cout << alphaArray[3] << sqrt(i) << ' ' ;
cout << alphaArray[4] << sqrt(i) << ' ' ;

and it returns in garble, I've also tried to do a loop for it but no go.
• 11-16-2004
hk_mp5kpdw
Code:

```#include <iostream> #include <cmath> using namespace std; const int arraySize = 51; int main() {     double alphaArray[arraySize];     for (int i=1; i < 51; i++) // Do you really mean to start at 1 and not 0?     {         alphaArray[i]= i;      // Store a number in each array element                         if( i < 25)         {             cout << sqrt(i) << ' ';         }         else         {             cout << i*3 << ' ';         }         if( ... ) // Figure out what to put in place of the ... (hint: it involves "i")             cout << endl;     }     return 0; }```
• 11-16-2004
You only want a new line if i is a multiple of ten, which is to say, if i modulo ten is zero.
• 11-16-2004
Another question is does your compiler allow a call to sqrt() with an integer argument? Mine doesn't, it has several overloaded functions, but not an integer version.
• 11-16-2004
furiousferret
got it to work, the winning code is:

Code:

```#include <iostream> #include <cmath> using namespace std; const int arraySize = 51; int main() {         double alphaArray[arraySize];         for (int i=1; i < 51; i++)         {             alphaArray[i]= i;      // Store a number in each array element                                 if( i < 25)                 {                         cout<< sqrt(i) << " | " ;                                         if(i % 10 == 0)                                         {                                                 cout << endl;                                         }                 }                 else                 {                         cout << i * 3 << " | ";                                         if(i % 10 == 0)                                         {                                                 cout << endl;                                         }                 }                         }         return 0; }```
• 11-16-2004
Dave Evans
Quote:

Another question is does your compiler allow a call to sqrt() with an integer argument? Mine doesn't, it has several overloaded functions, but not an integer version.

Do you mean that your compiler won't work with this? What is your compiler?

Code:

```#include <iostream> #include <cmath> using std::cout; using std::endl; int main() {   cout << "sqrt(2) = " <<  sqrt(2) << endl;   return 0; }```
The argument will be converted to double, since the prototype for sqrt() in math.h has a double argument.

(It works for me.)

Regards,

Dave
• 11-16-2004
furiousferret
worked fine for me...shrug
• 11-16-2004
>>> Do you mean that your compiler won't work with this? What is your compiler?

Quote:

Compiling...
xrap2.cpp
c:\crap2\xrap2.cpp(40) : error C2668: 'sqrt' : ambiguous call to overloaded function
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(626): could be 'long double sqrt(long double)'
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(578): or 'float sqrt(float)'
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(200): or 'double sqrt(double)'
while trying to match the argument list '(int)'

Build log was saved at "file://c:\crap2\Debug\BuildLog.htm"
crap2 - 1 error(s), 0 warning(s)

---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped

Visual Studio.NET 2003 Professional. The error it gives is fairly self explanatory.
• 11-16-2004
Dave Evans
Quote: