DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: Array sorting

  1. #1
    Garu Guest

    Array sorting


    This is long winded but..
    # include<iostream>
    using std::cout;
    using std::endl;
    # include<iomanip>
    using std::setw;
    #include <cstdlib>
    #include <ctime>
    int main()
    {
    const int arraysize = 100;
    srand (time(0));

    for (int i = 1; i <=15; i++){
    cout << setw(10) << 1 + rand() % 100 << endl;
    if (i% 99 == 0)
    cout << endl;
    }
    return 0;
    }

    This program is to create a function the uses the random number generator
    to populate a 5X3 array with random numbers in the range of 1 to 100, and
    display array in 5 rows by 3 columns
    My question(s): How to I display as 5X3 array using setw etc..

    How do you populate an 100 number array, and work it into the if statement?
    Do I have to use int n[array size] = {1,2,3...and write all 100 numbers,
    or is their a shorter method?
    Am I even close with what I got?






  2. #2
    jonnin Guest

    Re: Array sorting


    You are close. If the loop counter+1 % columns == 0 then write end of line.
    here, if you assume filled array with loop counter from 0, we get
    0 1 2
    3 4 5
    6 7 8
    9 10 11
    12 13 14

    watch out for 0, 0 % anything == 0 ...

    fill each element with a number in a loop.

    for(x = 0 ... 99)
    array[x] = (x or rand() or whatever)





    "Garu" <gchambers12@home.ca> wrote:
    >
    >This is long winded but..
    ># include<iostream>
    >using std::cout;
    >using std::endl;
    ># include<iomanip>
    >using std::setw;
    >#include <cstdlib>
    >#include <ctime>
    >int main()
    >{
    > const int arraysize = 100;
    >srand (time(0));
    >
    >for (int i = 1; i <=15; i++){
    > cout << setw(10) << 1 + rand() % 100 << endl;
    > if (i% 99 == 0)
    > cout << endl;
    >}
    >return 0;
    >}
    >
    >This program is to create a function the uses the random number generator
    >to populate a 5X3 array with random numbers in the range of 1 to 100, and
    >display array in 5 rows by 3 columns
    > My question(s): How to I display as 5X3 array using setw etc..
    >
    > How do you populate an 100 number array, and work it into the if statement?
    > Do I have to use int n[array size] = {1,2,3...and write all 100 numbers,
    >or is their a shorter method?
    > Am I even close with what I got?
    >
    >
    >
    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links