Thread: Prime Numbers and Array...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    Prime Numbers and Array...

    Code:
    #include"stdafx.h"
    #include<stdlib.h>
    #include<math.h>
    
    
    int main() {
    
    int int_array[10];
    int i, product = 1, sum = 0;
    
    for (i = 0; i < 10; i++) {
    	int_array[i] = (rand() * 251)/RAND_MAX;
    	printf("int_array[%d] has a value of %d.\n", i, int_array[i]);
    }
    
    for (i = 0; i < 10; i++) {
    	sum = sum + int_array[i];
    }
    printf("The sum of the 10 integers is %d\n");
    
    
    for (i = 0; i < 10; i++) {
    	product = product * int_array[i];
    }
    printf("The product of the 10 integers is %d\n");
    
    printf("The numbers that are divisible by two are\n");
    for (i = 0; i < 11; i++) {
    	if ((int_array[i]%2)==0)
    printf ("%d\n", int_array[i]);
    }
    
    printf("The numbers that are divisible by three are\n");
    for (i = 0; i < 11; i++) {
    	if ((int_array[i]%3)==0)
    printf ("%d\n", int_array[i]);
    }
    
    printf("The numbers that are prime are\n");
    for (i = 0; i < 11; i++) {
    	int x = sqrt (int_array[i]);
    	for 
    		//divide it by 2 - x, if the remainder is ever 0, its not prime.
    	
    }
    How would I do the prime number section? I'm totally lost. :S
    Last edited by Deux; 12-17-2004 at 02:05 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What part specificly are you having problems with? We're not here to simply hand out code. You might want to consider searching the forums. There are numerous posts on finding prime numbers if that's what you're stuck on.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    I dont understand y people cannot help others who had problems. this is discussion forum anyway.. help people as much as u can, until u can. Anyway Deux, here's sample how to determine the prime number. maybe it is not efficient, but at least you have basic idea how to calculate the prime number.

    Code:
    for(i=0; i<10; i++) {
       printf("int_array[%d] = %d is ", i, int_array[i]);
       if(!prime(int_array[i]))
          printf("prime number\n");
       else
          printf("not prime number\n");
    }
    :
    :
    :
    
    int prime(int a) {
       int i;
       for(i=2; i<a; i++) {
          if(a%i == 0)
             return 1;
       }
       return 0;
    }
    rgds,

    Triple_X, MY

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't understand why people complain about us not just handing out code and then post their own horrible code.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    y dont u post yur excellent code then... ;-)

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Because like quzah I don't know exactly what they want.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    to Thantos: seems like you are an expert in this forum. Can u help me out. You can read my post here. Any suggestion? Thanks ;-)

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Calculating whether a number is prime is simple really. Just a few things to keep in mind.

    Given the number in question is n and then value to check against the modulus is m.

    Simply assume you already know 1 and 2 are prime. This way you can begin the loop which checks the modulus of n % m == 0 with 3 as the initial value of m. In this manner, you can increment m by 2 since no even numbers other than 2 are prime. This results in fewer calculations to be performed.

    Also, m never needs to be checked any higher than the square root of n. Any calculations where m is greater than the squareroot of n are just redundant.
    Last edited by Scribbler; 12-20-2004 at 11:46 AM.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    If I'm looking at it right, isn't there a problem in the for loop with i < 10? That would only fill the array 9 times instead of 10?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. It is starting at zero, just like array indexes. Funny, that. At any rate, count from zero to nine on your fingers and get back to me.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Really didnt need to be a smartass about it. So then my next question is, why use i<11 then?

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    My little program for getting primes (enjoy):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct OOOOO0OOO0OO{
      struct OOOOO0OOO0OO *OO00O0O0O0O0;
      struct OOOOO0OOO0OO *O0O0O0O0O0O0;
      int O00O0O0O00O0;
    } OOOOO0OOO0OO;
    
    OOOOO0OOO0OO* OO0O0O0O000O (OOOOO0OOO0OO *OXOO0O00O0O0, int O00O0O0O00O0);
    void O000000000OO (OOOOO0OOO0OO *O0X0000OOO0O, char O0O0O00O0O0O);
    void OOOOOOOOOO00(OOOOO0OOO0OO *O0X0000OOO0O);
    
    int O00O00O0O0O0(int OOOOOOOO0000, OOOOO0OOO0OO *OOO0OO0OO0OO);
    
    int main(void)
    {
      OOOOO0OOO0OO *O0X0000OOO0O = NULL, *OXOO0O00O0O0 = NULL;
      int Ox0OO00O00O0=0;
      OXOO0O00O0O0 = OO0O0O0O000O(OXOO0O00O0O0, 2);
      O0X0000OOO0O = OXOO0O00O0O0;
      OXOO0O00O0O0 = OO0O0O0O000O(OXOO0O00O0O0, 3);
    
      for (Ox0OO00O00O0=4; Ox0OO00O00O0 < 10000; Ox0OO00O00O0++)
        if ( O00O00O0O0O0(Ox0OO00O00O0, O0X0000OOO0O) )
          OXOO0O00O0O0=OO0O0O0O000O(OXOO0O00O0O0, Ox0OO00O00O0);
    
      O000000000OO(O0X0000OOO0O, '\n');
      OOOOOOOOOO00(O0X0000OOO0O);
      return 0;
    }
    
    OOOOO0OOO0OO* OO0O0O0O000O(OOOOO0OOO0OO *OXOO0O00O0O0, int O00O0O0O00O0)
    {
      OOOOO0OOO0OO *O0000OOOOO00 = malloc(sizeof(OOOOO0OOO0OO));
      if ( O0000OOOOO00 == NULL )
        return NULL;
    
      O0000OOOOO00->O00O0O0O00O0 = O00O0O0O00O0;
      if ( OXOO0O00O0O0 != NULL)
      {
        OXOO0O00O0O0->O0O0O0O0O0O0 = O0000OOOOO00;
        O0000OOOOO00->OO00O0O0O0O0 = OXOO0O00O0O0;
      }
      else
      {
        O0000OOOOO00->OO00O0O0O0O0 = NULL;
      }
      O0000OOOOO00->O0O0O0O0O0O0 = NULL;
    
      return O0000OOOOO00;
    }
    
    void O000000000OO(OOOOO0OOO0OO *O0X0000OOO0O, char O0O0O00O0O0O)
    {
      while ( O0X0000OOO0O != NULL )
      {
        printf("%d%c", O0X0000OOO0O->O00O0O0O00O0, O0O0O00O0O0O);
        O0X0000OOO0O=O0X0000OOO0O->O0O0O0O0O0O0;
      }
    }
    
    void OOOOOOOOOO00(OOOOO0OOO0OO *O0X0000OOO0O)
    {
      while ( O0X0000OOO0O != NULL )
      {
        OOOOO0OOO0OO *O0000OOOOO00 = O0X0000OOO0O;
        O0X0000OOO0O = O0X0000OOO0O->O0O0O0O0O0O0;
        free(O0000OOOOO00);
      }
    }
    
    int O00O00O0O0O0(int OOOOOOOO0000, OOOOO0OOO0OO *OOO0OO0OO0OO)
    {
      while ( OOO0OO0OO0OO != NULL && (OOO0OO0OO0OO->O00O0O0O00O0 * OOO0OO0OO0OO->O00O0O0O00O0) <= OOOOOOOO0000)
        if ( OOOOOOOO0000 % OOO0OO0OO0OO->O00O0O0O00O0 == 0 )
          return 0;
        else
          OOO0OO0OO0OO = OOO0OO0OO0OO->O0O0O0O0O0O0;
    
      return 1;
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Closed - the OP has never returned, and all semblance of "on topic" is gone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  3. HELP:::MIX numbers in array
    By ypramesh in forum C Programming
    Replies: 9
    Last Post: 03-30-2006, 07:47 PM
  4. More Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 3
    Last Post: 02-21-2003, 10:06 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM