944,007 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4083
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 30th, 2007
0

Re: arrays proplem

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 7
Junior Poster
rajatC is offline Offline
109 posts
since Dec 2007
Dec 30th, 2007
0

Re: arrays proplem

it will print 11 elements in first line and 10 in all others...
to be more accurate use...
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 14
Solved Threads: 7
Junior Poster
rajatC is offline Offline
109 posts
since Dec 2007
Dec 30th, 2007
0

Re: arrays proplem

Click to Expand / Collapse  Quote originally posted by rajatC ...
c++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. if( (i+1)%10 == 0)
Because i was started from 0
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Dec 30th, 2007
0

Re: arrays proplem

this is my answer ; is it right ?
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Dec 30th, 2007
0

Re: arrays proplem

This works if you don't mind the newline right from the get go.
cpp Syntax (Toggle Plain Text)
  1. if (i % 10 == 0)
  2. cout << endl;
  3. cout << alpha[i] << "\t";
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 30th, 2007
0

Re: arrays proplem

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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 30th, 2007
0

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.
C++ Syntax (Toggle Plain Text)
  1. for (int i=0; i<50; i++) {
  2. cout << alpha[i] << " ";
  3. if ((i+1) % 10 == 0)
  4. cout << endl;
  5. }
Or
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. for (int i=0; i<50; ) {
  2. cout << alpha[i] << " ";
  3. if (++i % 10 == 0)
  4. cout << endl;
  5. }
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: direct access to hdd
Next Thread in C++ Forum Timeline: how to choos a path





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC