using arrays to sort numbers

This is a discussion on using arrays to sort numbers within the C++ Programming forums, part of the General Programming Boards category; i'm having problems with writing a program using arrays. i have to get the program to print out all the ...

  1. #1
    Unregistered
    Guest

    Question using arrays to sort numbers

    i'm having problems with writing a program using arrays. i have to get the program to print out all the prime numbers up to 500. I'm using if/else statements and for loops but all i'm getting in return is 0 when i run the program. What am i doing wrong.

    Here's a sample of my work:
    for(digit=2; digit<=500; digit++)
    {
    if(digit*2 != digit)
    PrimeNum[digit]
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    it doesn't look like your doing it right cause you aren't assigning anything to the array. You will probably have to use a nested loop. Probably something of this sort


    int array = 500;
    for(int x = 0; x <= 500; x++)
    {
    for(int y = 0; y <= 500; y++)
    {

    do stuff here to figure out if it is prime then assign it to the array such as

    array[x] = prime number;

    then have a counter count up x everytime it finds a prime number
    }

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 05:03 AM
  2. Replies: 4
    Last Post: 04-19-2005, 09:05 PM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. Bubble Sort with Arrays
    By 3kgt in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2003, 02:48 AM
  5. arrays and random numbers
    By bruceness in forum C Programming
    Replies: 3
    Last Post: 10-23-2002, 10:36 PM

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