DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   arrays proplem (http://www.daniweb.com/forums/thread102743.html)

rajatC Dec 30th, 2007 5:19 am
Re: arrays proplem
 
for(int i=0 ; i<50 ; i++)
{cout<<a[i]<<" ";
if(i%10 == 0)
cout<<endl;
}


you had to use cout<<endl if i is divisble by 10 i.e. 10 elements has been printed..

rajatC Dec 30th, 2007 5:21 am
Re: arrays proplem
 
it will print 11 elements in first line and 10 in all others...
to be more accurate use...
for(int i=1 ; i<51 ; i++)
    {
           
            cout<<setw(3)<<a[i-1]<<" ";
            if(i%10 == 0)
            cout<<endl;
    }

invisal Dec 30th, 2007 6:10 am
Re: arrays proplem
 
Quote:

Originally Posted by rajatC (Post 500542)
for(int i=0 ; i<50 ; i++)
{cout<<a[i]<<" ";
if(i%10 == 0)
cout<<endl;
}


you had to use cout<<endl if i is divisble by 10 i.e. 10 elements has been printed..

It should be:
if( (i+1)%10 == 0)
Because i was started from 0

bis student Dec 30th, 2007 6:49 am
Re: arrays proplem
 
this is my answer ; is it right ?
# include <iostream>
#include <iomanip >
using namespace std ;
int main ()
{
       
int alpha[50];
for (int i=0;i<25;i++)
{
alpha[i]=i*i;
cout<<alpha[i]<<" ";
}
for(int i=25;i<50;i++)
{
alpha[i]=3*i;
cout<<alpha[i]<<" ";
}
for(int i=1 ; i<51 ; i++)    {                         
    {
           
            cout<<setw(3)<<alpha[i-1]<<" ";
            if( (i+1)%10 == 0)
            cout<<endl;
    }
   
}

return 0;
}

superjacent Dec 30th, 2007 6:52 am
Re: arrays proplem
 
This works if you don't mind the newline right from the get go.
if (i % 10 == 0)
  cout << endl;
cout << alpha[i] << "\t";

superjacent Dec 30th, 2007 7:06 am
Re: arrays proplem
 
Quote:

Originally Posted by bis student (Post 500564)
this is my answer ; is it right ?

I'd guess that you should be able to determine if it's right or not and I'm suspecting you're guessing it's not.

I'd suggest removing the
cout
statements from the
for
loops of the assignment statements, they just print 25 across the line, which is not what you want.

I removed those cout statements and your code is nearly there. The first line only prints 9 numbers, the rest print 10.

invisal Dec 30th, 2007 7:15 am
Re: arrays proplem
 
for(int i=1 ; i<51 ; i++)    {                           
    {
           
            cout<<setw(3)<<alpha[i-1]<<" ";
            if( (i+1)%10 == 0)
            cout<<endl;
  }
   
}
Double open brace and double close brace?

There are 2 ways to choose to print 10 numbers for each line.
for (int i=0; i<50; i++) {
  cout << alpha[i] << "  ";
    if ((i+1) % 10 == 0)
        cout << endl;
}
Or
for (int i=1; i<51; i++) {
  cout << alpha[i-1] << "  ";
    if (i % 10 == 0)
        cout << endl;
}
This one also work too
for (int i=0; i<50; ) {
  cout << alpha[i] << "  ";
    if (++i % 10 == 0)
        cout << endl;
}


All times are GMT -4. The time now is 7:46 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC