1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Another basic c programe programe

  1. Nov 6, 2009 #1
    1. The problem statement, all variables and given/known data
    http://u1.imgupload.co.uk/1257465600/6baa_prime1.jpg

    how can i write a C programe that can give a output exactly the same as the pic above?
    2. Relevant equations
    above.


    3. The attempt at a solution

    PHP:
            printf("The prime numbers less than or equal to %d are:\n", number);
            int pos = 0;
            int pcount = 0;
            while(primes[pos] != -1)
               {
               if(primes[pos] != -2)
                   {
                   printf("\t%d\n", primes[pos]);
                   pcount++;
                   }
               pos++;
               }
            printf("The total count of prime numbers is: %d\n", pcount);
    I have tried my best to attempt the required programe and it is shown above.
    but when i try to run it in codeblock it shows nothing.
    is there any critia code i missed or some fautal error in my attempt?
    thx for help.
     
  2. jcsd
  3. Nov 6, 2009 #2

    Mark44

    Staff: Mentor

    The code you show uses your primes array, but doesn't show how you have initialized this array. Somewhere in your code you are going to need to put the values 2, 3, 5, 7, 11, 13, and 17 in your array. Do you know how to tell if a number is prime?
     
  4. Nov 6, 2009 #3
    i dont know how to express in c program and i only know it should be something related to the code "while...loop..ect.."

    poor me
     
  5. Nov 6, 2009 #4

    Mark44

    Staff: Mentor

    That's not going to help you any. I'm not asking for C code. I'm asking for your basic algorithm for determining which of the numbers from 2 through, say 20, are prime. After you figure out your algorithm, then you can write an implementation using C.
     
  6. Nov 6, 2009 #5
    i know wt u mean.i just revise some concept about prime number and my attempt is listed below:

    PHP:
    #include <stdio.h>
    #include <math.h>

    #define N 20

    int main ()
       
    {
        int i, j, s;
        int is_prime[N]; /* if is_prime[i] == 0, then i isn't prime */

    /* initially, all are presumed prime */

        printf ("The prime numbers less than of equal to %d are:\n", N);

        for (i=0; i<N; i++) is_prime[i] = 1;

    /* find the limit of the numbers to count up to */

        s = (int) sqrt (N) + 1;

    /* for each i in 2..sqrt(N), mark out multiples of i */

        for (i=2; i<s; i++) {

    /* mark out 2i, 3i, 4i, ... */

        for (j=2*i; j<N; j+=i) is_prime[j] = 0;
    }

    /* print out the numbers that are left */

        for (i=2; i<N; i++)
        if (is_prime[i]) printf ("%d\n", i);
        printf("There total count of prime numbers is: %d\n",  );  
    }
    output:
    http://u1.imgupload.co.uk/1257552000/bb4b_2009_11_07_114949.png

    http://u1.imgupload.co.uk/1257552000/e2f9_prime11.jpg
    but how can i make the yellow region happen?
    printf("There total count of prime numbers is: %d\n" ,?? ) <-- what shoud i put in "??" ?
    and how can i make the each line had some space at the beginning??
     
  7. Nov 7, 2009 #6

    Mark44

    Staff: Mentor

    Try this. I have defined a new variable, prime_count. It should go up near the top with the other variable declarations.
    Code (Text):

    int i, j, s;
    int is_prime[N];
    int prime_count = 0;
    ...
    for (i=2; i<N; i++)
    {
        if (is_prime[i]) printf ("\t%d\n", i);
        prime_count++;
    }

    printf("The total count of prime numbers is: %d\n",  prime_count);  
     
    There is also a new line in your for loop, that increments prime_count each time a prime is found in the array (that is, each time an element in the array has a value of 1).

    I have also added a '\t' control character in your first printf() statement. This is the TAB character. When it is printed, it moves the cursor over to the first tab position.
     
  8. Nov 7, 2009 #7
    sorry..why seems not work?
    i saved the name file as xxx.c and doesnt work.
    and i tried to save in .cpp and it works.

    your suggested code is C++ code?
     
    Last edited: Nov 7, 2009
  9. Nov 7, 2009 #8

    Mark44

    Staff: Mentor

    No, my suggested code was straight C, and would work as either C or C++. I think I remember that one difference between C and C++ is that in C, variables have to be declared before any executable statements, so if you put the line int prime_count = 0; in the wrong place, the revised code might not compile as C but would compile as C++. That's all I can think of.

    Your code should look like the following.
    Code (Text):

    #include <stdio.h>
    #include <math.h>

    #define N 20

    int main ()
    {
        int i, j, s;
        int is_prime[N]; /* if is_prime[i] == 0, then i isn't prime */
        int prime_count = 0;

        /* initially, all are presumed prime */

        printf ("The prime numbers less than of equal to %d are:\n", N);

        for (i=0; i<N; i++) is_prime[i] = 1;

        /* find the limit of the numbers to count up to */

        s = (int) sqrt (N) + 1;

        /* for each i in 2..sqrt(N), mark out multiples of i */

        for (i=2; i<s; i++) {

            /* mark out 2i, 3i, 4i, ... */

            for (j=2*i; j<N; j+=i) is_prime[j] = 0;
        }

        /* print out the numbers that are left */

        for (i=2; i<N; i++)
        {
            if (is_prime[i]) printf ("\t%d\n", i);
            prime_count++;
        }

        printf("The total count of prime numbers is: %d\n",  prime_count);
    }  
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Another basic c programe programe
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)

Loading...