Printing an array in lines of 10.

This is a discussion on Printing an array in lines of 10. within the C++ Programming forums, part of the General Programming Boards category; I've tried several ways to do this but I either copy one variable 10 times or they all go on ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    6

    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. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    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. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You only want a new line if i is a multiple of ten, which is to say, if i modulo ten is zero.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    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. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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
    Last edited by Dave Evans; 11-16-2004 at 03:34 PM.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    worked fine for me...shrug

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> 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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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 subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Frustated with mess table printing
    By benedicttobias in forum C Programming
    Replies: 6
    Last Post: 04-16-2009, 06:50 PM
  3. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  4. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  5. Replies: 1
    Last Post: 06-07-2006, 10:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21