# Thread: Printing an array in lines of 10.

1. ## 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.

2. 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;

Have a think about that.

3. 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.

4. 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;
}```

5. You only want a new line if i is a multiple of ten, which is to say, if i modulo ten is zero.

6. 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.

7. 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;
}```

8. Originally Posted by adrianxw
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

9. worked fine for me...shrug

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

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. Originally Posted by adrianxw
>>> Do you mean that your compiler won't work with this? What is your compiler?

Visual Studio.NET 2003 Professional. The error it gives is fairly self explanatory.
Thanks for the update. I haven't used .NET for anything. I know that just because something works on my system (and has worked on all C and C++ systems that I have used since 19..) that doesn't mean that it works for everything everywhere.

Regards,

Dave

Popular pages Recent additions