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

C language /* This driver program should be used to test your functions defined

ID: 3852657 • Letter: C

Question

C language

/*
This driver program should be used to test your functions defined in the following file:
array_utils.c

Please note that, this driver program is meant for testing your program locally.

In the command prompt, type "gcc -o arrayTestDriver arrayTestDriver.c array_utils.c" to
create the driver program. Then type "./arrayTestDriver" to see the output.

After successful local testing, you MUST test your program (array_utils.c) via webgrader.

Also, don't submit this driver program in webhandin.
*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "array_utils.h"


int main(){
      
   int arr1[] = {1,2,3,3,3,3,3,4,5};
   int arr2[] = {2,6,8,11,12,14,15,16};
   int arr3[] = {3,8,11,12,14,23,32,33,35};
   int arr4[] = {4,8,9,17,23,23,25,29,31};
   int arr5[] = {2,4,7,8,9,11,13};
   int arr6[] = {3,8,9,11,13};

//test case 1
   int result1 = dotProduct(arr4,3,arr5,3);
   int result2 = numPrime(arr3,9);
   int *result3 = arrayDifference(arr1,arr2,5,5);
   int *result4 = arrayIntersect(arr2,arr3,5,5);
//test case 2  
   int result5 = dotProduct(arr1,3,arr2,3);
   int result6 = numPrime(arr5,4);
   int *result7 = arrayDifference(arr4,arr5,5,5);
   int *result8 = arrayIntersect(arr4,arr5,5,5);
//test case 3  
   int result9 = dotProduct(arr2,3,arr3,3);
   int result10 = numPrime(arr6,5);
   int *result11 = arrayDifference(arr5,arr6,3,3);
   int *result12 = arrayIntersect(arr2,arr3,5,5);
  
  
       int i = 0;
       printf(" Test Case 1:");
          
   for ( i = 0; i < 1;i ++){  
       printf(" dotProduct %d",result1);
       printf(" numPrime %d",result2);
       printf(" arrayDifference %d",result3[i]);
       printf(" arrayIntersect %d",result4[i]);
       } printf(" ");
      
   printf(" Test Case 2: ");  
  
       printf("dotProduct ");
   for ( i = 0; i < 1;i ++){
       printf("%d ",result5);          
   }
  
       printf("numPrime ");
   for ( i = 0; i < 1;i ++){
       printf("%d ",result6);          
   }
       printf("arrayDifference ");
   for ( i = 0; i < 2;i ++){
       printf("%d ",result7[i]);
              
   }
       printf("arrayIntersect ");  
      
   for ( i = 0; i < 2;i ++){
       printf("%d ",result8[i]);
              
   }
  
       printf(" Test Case 3: ");  
  
       printf("dotProduct ");
   for ( i = 0; i < 1;i ++){
       printf("%d ",result9);          
   }
  
       printf("numPrime ");
   for ( i = 0; i < 1;i ++){
       printf("%d ",result10);          
   }
       printf("arrayDifference ");
   for ( i = 0; i < 2;i ++){
       printf("%d ",result11[i]);
              
   }
       printf("arrayIntersect ");  
      
   for ( i = 0; i < 2;i ++){
       printf("%d ",result12[i]);
              
   }
  
              
      
      
      
      
   return 0;
}

3. In this exercise vou will write several functions that operate on arravs You will place all the function prototypes in a header file named array_utils.h with their definitions in a source file named array_utils.c. You should test your functions by writing a driver program, but you need not hand it in. (a) The dot product of two arrays are a a1, a2, ag, .., an and b-[bi, b2, b3,.., bn] is Write a function that computes the dot product of the two arrays int dotProduct(const int *a, int size, const int *b, int size2); (b) Write a function that takes an integer array and returns the number of integers that are prime in the array int numPrime(const int *a, int size);

Explanation / Answer

int dotProduct(const int*a,const int*b,int size2)
{
int result = 0,i;
for ( i = 0; i < size2; i++)
result += a[i]*b[i];
return result;
}
int numPrime(const int*a,int size2)
{int total=0,counter,i,j;
for( i=0;i<size2;i++)
{
counter=0;
for( j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
counter=1;
break;
}
}
if(counter==0)
{
total++;
}
}
return total;
}

int* arrayIntersect(const int*a,const int*b,int sizeOfA,int sizeOfB)
{   
int arrayReturn[sizeOfA+ sizeOfB];
int count = 0,i,j;

for( i = 0; i < sizeOfA; i++)
{
for( j = 0; j < sizeOfB; j++)
{
if(a[i]==b[j])
{
count = count + 1;
arrayReturn[count] = a[i];
}
}
}
return arrayReturn;
}
  
int* arrayDifference(const int*a,const int*b,int sizeOfA,int sizeOfB)
{   
int arrayReturn[sizeOfA+ sizeOfB];
int count = 0,i,j;

for( i = 0; i < sizeOfA; i++)
{
for( j = 0; j < sizeOfB; j++)
{
if(a[i]==b[j])
{
count = count + 1;
arrayReturn[count] = a[i];
}
}
}
return arrayReturn;
}