#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Gold Coast, Australia
    Posts
    5
    Rep Power
    0

    listing numbers in columns


    i need to list numbers in a table 10 colums wide. It is not working properly.... Any clues?

    #include <stdio.h>

    #define PRIME_MAX 10000
    #define NUM_MAX 9

    int main() {
    int i;
    int j;
    bool prime[PRIME_MAX];
    int num[NUM_MAX];
    int counter;
    prime[PRIME_MAX] = i;
    num[NUM_MAX] = j;

    printf("\n Prime numbers from 1 to %d\n", PRIME_MAX);
    printf("\n");

    counter = 0;

    for (i = 0; i < PRIME_MAX; i++) {
    prime[i] = true;
    for (j = 2; j < NUM_MAX; j++) {
    if ((j < i) && (i % j == 0)) {
    prime[i] = false;
    }
    }
    if (prime[i] == true) {
    printf("%6d%c", i, (i + 1) % 10 == 0 ? '\n' : ' ');
    counter = counter + 1;
    }
    }

    printf("\n");
    printf("Total prime numbers less than %d: %d\n", PRIME_MAX, counter);
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    Not that I wanted to do your work for you, but...

    #include <stdio.h>

    #define PRIME_MAX 10000
    #define NUM_MAX 9

    int main() {
    bool prime;

    printf("\n Prime numbers from 1 to %d\n\n", PRIME_MAX);

    int total = 0;

    for (int i = 0; i < PRIME_MAX; i++) {
    prime = true;
    for (int j = i-1; j > 1; j--) {
    if (i % j == 0) {
    prime = false;
    }
    }
    if (prime == true) {
    printf("%6d%c", i, (total + 1) % 10 == 0 ? '\n' : ' ');
    total++;
    }
    }

    printf("\n");
    printf("Total prime numbers less than %d: %d\n", PRIME_MAX, total);
    }

    Some things to note... your column was testing the current "i" not the total primes that you have output. Also, unless I'm mistaken (which I might be since it's early and I'm very tired) prime numbers are only divisible by 1 and themselves... so that check had to be modified.

    Anyway, I hope this helps.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Gold Coast, Australia
    Posts
    5
    Rep Power
    0
    thanks, that worked beautifully.

IMN logo majestic logo threadwatch logo seochat tools logo