arrays proplem

Thread Solved
Reply

Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: arrays proplem

 
0
  #11
Dec 30th, 2007
  1. for(int i=0 ; i<50 ; i++)
  2. {cout<<a[i]<<" ";
  3. if(i%10 == 0)
  4. cout<<endl;
  5. }


you had to use cout<<endl if i is divisble by 10 i.e. 10 elements has been printed..
Last edited by rajatC; Dec 30th, 2007 at 5:19 am.
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: arrays proplem

 
0
  #12
Dec 30th, 2007
it will print 11 elements in first line and 10 in all others...
to be more accurate use...
  1. for(int i=1 ; i<51 ; i++)
  2. {
  3.  
  4. cout<<setw(3)<<a[i-1]<<" ";
  5. if(i%10 == 0)
  6. cout<<endl;
  7. }
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 463
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: arrays proplem

 
0
  #13
Dec 30th, 2007
Originally Posted by rajatC View Post
  1. for(int i=0 ; i<50 ; i++)
  2. {cout<<a[i]<<" ";
  3. if(i%10 == 0)
  4. cout<<endl;
  5. }


you had to use cout<<endl if i is divisble by 10 i.e. 10 elements has been printed..
It should be:
  1. if( (i+1)%10 == 0)
Because i was started from 0
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bis student is an unknown quantity at this point 
Solved Threads: 0
bis student's Avatar
bis student bis student is offline Offline
Newbie Poster

Re: arrays proplem

 
0
  #14
Dec 30th, 2007
this is my answer ; is it right ?
  1. # include <iostream>
  2. #include <iomanip >
  3. using namespace std ;
  4. int main ()
  5. {
  6.  
  7. int alpha[50];
  8. for (int i=0;i<25;i++)
  9. {
  10. alpha[i]=i*i;
  11. cout<<alpha[i]<<" ";
  12. }
  13. for(int i=25;i<50;i++)
  14. {
  15. alpha[i]=3*i;
  16. cout<<alpha[i]<<" ";
  17. }
  18. for(int i=1 ; i<51 ; i++) {
  19. {
  20.  
  21. cout<<setw(3)<<alpha[i-1]<<" ";
  22. if( (i+1)%10 == 0)
  23. cout<<endl;
  24. }
  25.  
  26. }
  27.  
  28. return 0;
  29. }
there are always hope
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: arrays proplem

 
0
  #15
Dec 30th, 2007
This works if you don't mind the newline right from the get go.
  1. if (i % 10 == 0)
  2. cout << endl;
  3. cout << alpha[i] << "\t";
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: arrays proplem

 
0
  #16
Dec 30th, 2007
Originally Posted by bis student View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 463
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: arrays proplem

 
0
  #17
Dec 30th, 2007
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.
  1. for (int i=0; i<50; i++) {
  2. cout << alpha[i] << " ";
  3. if ((i+1) % 10 == 0)
  4. cout << endl;
  5. }
Or
  1. for (int i=1; i<51; i++) {
  2. cout << alpha[i-1] << " ";
  3. if (i % 10 == 0)
  4. cout << endl;
  5. }
This one also work too
  1. for (int i=0; i<50; ) {
  2. cout << alpha[i] << " ";
  3. if (++i % 10 == 0)
  4. cout << endl;
  5. }
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC