Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C/C++, define an integer pointer in main(), and use this pointer to dynami

ID: 3822707 • Letter: U

Question

Using C/C++, define an integer pointer in main(), and use this pointer to dynamically allocate an integer array with 50 elements; assign the array with random numbers in range [0, 999]. Then, pass the array as parameter(s) to a function prime_count() which counts the number of the integers that are prime, and prints those integers, after the function call, print the "counter" result: finally, use delete to deal locate the array memories. Note that you need to define a Boolean function prime()which tells whether an integer is prime or not. This function will be called and used by prime_count() tell whether the numbers in the array are prime or not.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

int prime(int number)
{
    int j,flag = 0;
     for(j=2;j<number/2;j++) // check if a number is prime
        {
            if(number%j == 0)
            flag = 1;
        }
    return flag;      
}

int prime_count(int array[5])
{
    int i,count = 0;
     printf(" Prime numbers in array are :");
    for(i=0;i<50;i++)
    {
     
         if(prime(array[i])== 0)    //if an element of array is prime increment count
         {
             printf(" %d",array[i]);
       
            count++;
        }
    }
  
    return count;
}

int main(void)
{
int i,*ptr,array[50];
ptr = array;
ptr = (int*)malloc(50*sizeof(int)); //dynamically allocating memory

printf(" Elements of array : ");
for(i=0;i<50;i++)
{
    array[i] = rand()%(999-0+1)+0; //initialize array with random numbers in the range 0 to 999
    printf("%d ",array[i]);
}

printf(" Number of prime numbers = %d", prime_count(array));


free(ptr); //deallocate memory

   return 0;
}


Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote