Thread: Array of integers - Print out how many primes are in there

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    3

    Array of integers - Print out how many primes are in there

    I'm working on an assignment and I've been stuck on this question for a while now.

    Basically, whats happening is that I have to feed my program an array of 20 integers and when it encounters a -1 in the array, it has to stop and output how many prime integers it read in the array.

    This is my code for the prime integer counter
    Code:
    int count_primes() {
    
    int countPrimes = 0;
        for (int p = 0; p < counter; p++) {
            if (inputTaken[p] % countPrimes == 0) {
                break;
            }
        if (inputTaken[p] == countPrimes) {
             countPrimes++;
             }
        }
        return countPrimes;
    }
    and it doesn't work at all and I'm honestly clueless as to how I can fix this.
    Some help would really be appreciated.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    At line #5 in the code you posted, you divide by zero. You need to make a function that checks whether a number is prime or not, and only if it returns True should you count that number.
    Last edited by GReaper; 11-02-2017 at 10:19 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    3
    Quote Originally Posted by GReaper View Post
    At line #5 in the code you posted, you divide by zero. You need to make a function that checks whether a number is prime or not, and only if it returns True should you count that number.
    Would this work?
    Code:
        int is_prime(int num) {        int i = 0;
            int num = 0;
            for (i = 2; i < num; i++) {
                  if (num % i == 0 && i != num) return 0;
        }
        return 1;
            }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    never mind it was almost there. So I remove what I had.
    Last edited by userxbw; 11-02-2017 at 12:25 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    modded FutureDeveloper, forgot 1 is not prime:

    screw prime numbers... People still use those? lol
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getPrimed(int arr[], int num)
    {
       if (  arr[num] == 1)
            return 0;
        
        for (int i = 2; i < arr[num]; i++) 
        { 
            if (  (arr[num] % i == 0 && i != arr[num] ) ) return 0;
        }
    return 1;
    }
    
    
    int main()
    {
       
        int primeCount = 0;
        
        int array_nums[ ] = { 
            1,   2,   3,   4,   5,   6,   7,   8,   9,  10,
            11,  12,  13,  14,  15,  16,  17,  18,  19,  20,
            21,  22,  23,  24,  25,  26,  27,  28,  29,  30,
            31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
            41,  42,  43,  44,  45,  46,  47,  48,  49,  50,
            51,  52,  53,  54,  55,  56,  57,  58,  59,  60,
            61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
            71,  72,  73,  74,  75,  76,  77,  78,  79,  80,
            81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
            91,  92,  93,  94,  95,  96,  97,  98,  99, 100 };
            
        int prime_numbers [ ] = {
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
            43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
            101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
            151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
            199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
            263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 
            317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
            383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 
            443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 
            503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
            577, 587, 593, 599, 601, 607, 613, 617, 619, 631,
            641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
            701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
            769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
            839, 853, 857, 859, 863, 877, 881, 883, 887, 907,
            911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
            983, 991, 997 };
        
        for ( int a = 0; a < size_of_array; a++)
        {
              
                // code goes here
                      
        }
        printf("some total of something %d\n", varTotal);
         
     
        return 0;
    }
    Last edited by userxbw; 11-02-2017 at 01:41 PM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    OMG, userxbw, please stop handing out complete solutions to assignments!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-13-2013, 05:42 PM
  2. Replies: 14
    Last Post: 10-07-2012, 06:43 PM
  3. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  4. Print all even or odd integers
    By Guti14 in forum C++ Programming
    Replies: 13
    Last Post: 08-13-2003, 06:46 PM
  5. Replies: 6
    Last Post: 11-04-2002, 05:11 PM

Tags for this Thread