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

Array Utilities (array utils.c) In this exercise you will write several function

ID: 3597066 • Letter: A

Question

Array Utilities (array utils.c)

In this exercise you will write several functions that operate on arrays. The individual

functions are detailed below.

(a) The dot product of two arrays are a = [a1; a2; a3; ::; an] and b = [b1; b2; b3; ::; bn] is

a b =

Xn

i=1

aibi = a1b1 + a2b2 + + anbn

Write a function that computes the dot product of the two arrays.

int dotProduct(const int *a, int size, const int *b, int size2);

5

(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);

(c) Write the following function.

void arrayDifference(const int *a, const int *b, int sizeOfA, int

sizeOfB);

This function dynamically creates an array and lls it with all integers that can

be found in a, but not in b. The values in the returned array should be unique;

that is, there should not be any duplicates. Furthermore, the size of the allocated

arrays should not waste memory.

(d) Write the following function.

void arrayIntersect(const int *a, const int *b, int sizeOfA, int

sizeOfB);

This function dynamically creates an array and lls it with all integers that can

be found in a and b. The values in the returned array should be unique; that is,

there should not be any duplicates. Furthermore, the size of the allocated arrays

should not waste memory.

Program ow pseudocode:

Start

Ask user for size of int arrays A & B (assume that both A and B has the

same size)

Dynamically create arrays A & B with size provided by the user

Prompt the user for entering values into A and B

Print menu

Prompt for menu choice

Run the chosen utility function or on invalid input prompt again

Print result of the chosen utility function and return to menu

Loop until exit

end program

Sample Output:

6

Enter size of array:

3

Enter values for array A:

2 2 4

Enter values for array B:

4 4 4

Main Menu

A: Find Dot Product

B: Find Number of Prime Integers In Array

C: Array A Difference With B

D: Array Intersection

E: Exit

B

Array A or B?

A

2 prime numbers in array A

...Print main menu again...

D

intersection array is: [4]

...Print main menu again...

C

difference array is: [2]

...Print main menu again...

E

Note: the line ...print main menu again... does not mean to print that literal

phrase, it means to print the menu again and is used here to save space.

Data Type Requirement: The array contents should be any valid integer and the

size should be any valid integer greater than 0.

Input Validation: Input can be any positive integer plus 0. You should account for

special nature of 0 and 1 when checking prime.

REMEMBER: This program uses DYNAMIC arrays.

You have to submit the following le containing the solution of this program

via handin: array utils.c

Here's what I have so far but it is wrong. I think I'm not supposed to use character char. This is for C programming.

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

int numPrime(int *a, int size){
int i,count = 0;
int found;
int j;

for(i=0; i<size; i++){
found = 0;
for (j=2; j<a[i]; j++){
if (a[i]%j == 0){
found = 1;
break;
}
}
if (found == 0)
count++;
}
return count;
}


int dotProduct(int *a, int m, int *b, int n){

int prod = 0;
int i;
if (m > n){
for (i = 0; i<n; i++){
prod = prod + a[i]*b[i];
}
}
if (m < n){
for (i = 0; i<m; i++){
prod = prod + a[i]*b[i];
}
}
else {
for (i = 0; i<m; i++){
prod = prod + a[i]*b[i];
}
}
return prod;
}

void arrayIntersect(int *a, int *b, int m, int n){

int *d;
int i,j;
int count = 0;

d = (int *)malloc(sizeof(int)*n);
for (i = 0; i<m; i++){
for(j=0; j<n; j++){
if (a[i] == b[j]){
d[count] = b[i];
count++;
break;
}
}
}
  
printf("Intersection array is [");
for (i = 0; i<count; i++){
printf("%d ",d[i]);
}
printf("] ");
}

void arrayDifference(int *a, int *b, int m, int n){
int *d;
int i,j;
int count = 0;
int found;

d = (int *)malloc(sizeof(int)*m);
for (i = 0; i<m; i++){
found = 0;
for(j=0; j<n; j++){
if (a[i] == b[j]){
found = 1;
break;
}
}
if (found == 0){
for (j=0; j<count; j++){
if (d[j] == a[i]){
found = 1;
break;
}

}
if (found == 0){
d[count] = a[i];
count++;
}
}
}

printf("Difference array is [");
for (i = 0; i<count; i++){
printf("%d ",d[i]);
}
printf("] ");

  
}


int main(){
  
char ch[2];
char ch1[2];
int *A;
int *B;
int *C;
int n,i;

printf("Enter size of array : ");
scanf("%d",&n);
A = (int *)malloc(sizeof(int)*n);
B = (int *)malloc(sizeof(int)*n);
  
if (n<1)
{printf ("Wrong size for array. Enter a positive number.Enter size of array:");
scanf("%d", &n);   }

printf("Enter A: ");
for (i = 0; i<n; i++){
scanf("%d",&A[i]);
}
printf("Enter B: ");
for (i = 0; i<n; i++){
scanf("%d",&B[i]);
}

while(1){

//menu of options

printf("A.Find Dot Product ");
printf("B.Find Number of Prime Integers In Array ");
printf("C.Array A Difefrence With B ");
printf("D.Array Intersection ");
printf("E.Exit ");

printf("Enter your choice (A,B,C,D,E):");
scanf("%c",ch);
  

  
if (ch[0] == 'E')
break;
if (ch[0] == 'A'){
printf("Dot product of A and B is %d ",dotProduct(A,n,B,n));
  
}
if (ch[0] == 'B'){
printf(" Array A or B: ");
scanf("%s",ch1);
if (ch1[0] == 'A'){
printf("%d prime numberes in array %c ",numPrime(A,n), 'A');
}
if (ch1[0] == 'B'){
printf("%d prime numberes in array %c ",numPrime(A,n), 'B');
}
  
}
if (ch[0] == 'C'){
arrayDifference(A,B,n,n);
  
}
if (ch[0] == 'D'){
arrayIntersect(A,B,n,n);
  
}

}
  
return 0;   /* indicate that program ended successfully */
}   /* end function main */

Explanation / Answer

Given below is the fixed code with output. You used %c for scanning menu choice instead of using %s in the scanf.

Also cleaned up some code . Changed code in dotproduct , difference , intersection

Output is shown . Please don't forget to rate the answer if it helped. Thank you

#include<stdio.h>
#include<stdlib.h>
int numPrime(int *a, int size){
int i,count = 0;
int isPrime;
int j;
for(i=0; i<size; i++){
isPrime = 1;
for (j=2; j<a[i]; j++){
if (a[i]%j == 0){
isPrime = 0;
break;
}
}
if (isPrime == 1)
count++;
}
return count;
}

int dotProduct(int *a, int m, int *b, int n){
int prod = 0;
int i;
int lastIndex ;
if(m <= n)
lastIndex = m;
else
lastIndex = n;
  
for (i = 0; i< lastIndex; i++){
prod = prod + a[i]*b[i];
}
return prod;
}
void arrayIntersect(int *a, int *b, int m, int n){
int *d;
int i,j;
int count = 0;
int found = 0;
d = (int *)malloc(sizeof(int)*n);
for (i = 0; i<m; i++){
found = 0;
//search in the second array
for(j=0; j<n; j++){
if(a[i] == b[i])
{
found = 1;
break;
}
}
  
if(found) //if eleent presnt in both arrays, check if already added to the intersection array
{
found = 0;
for(j = 0; j < count; j++)
{
if(d[j] == a[i])
{
found = 1;
break;
}
}
  
//if not already present, add it now
if(!found)
{
d[count++] = a[i];
}
}
}
  
printf("Intersection array is [");
for (i = 0; i<count; i++){
printf("%d ",d[i]);
}
printf("] ");
//free the dynamic array
free(d);
}
void arrayDifference(int *a, int *b, int m, int n){
int *d;
int i,j;
int count = 0;
int found = 0;
d = (int *)malloc(sizeof(int)*n);
for (i = 0; i<m; i++){
found = 0;
//search in the second array
for(j=0; j<n; j++){
if(a[i] == b[i])
{
found = 1;
break;
}
}
  
if(!found) //if eleent is not presnt in second array, we need to add it to difference array only if already not added to the difference array
{
found = 0;
for(j = 0; j < count; j++)
{
if(d[j] == a[i])
{
found = 1;
break;
}
}
  
//if not already present, add it now
if(!found)
{
d[count++] = a[i];
}
}
}
  
printf("Difference array is [");
for (i = 0; i<count; i++){
printf("%d ",d[i]);
}
printf("] ");
//free the dynamic array
free(d);
  
}

int main(){
  
char ch[2];
char ch1[2];
int *A;
int *B;
int *C;
int n,i;
  
printf("Enter size of array : ");
scanf("%d",&n);
A = (int *)malloc(sizeof(int)*n);
B = (int *)malloc(sizeof(int)*n);
  
if (n<1)
{printf ("Wrong size for array. Enter a positive number.Enter size of array:");
scanf("%d", &n); }
printf("Enter A: ");
for (i = 0; i<n; i++){
scanf("%d",&A[i]);
}
printf("Enter B: ");
for (i = 0; i<n; i++){
scanf("%d",&B[i]);
}
while(1){
  
//menu of options
printf(" A.Find Dot Product ");
printf("B.Find Number of Prime Integers In Array ");
printf("C.Array A Difefrence With B ");
printf("D.Array Intersection ");
printf("E.Exit ");
printf("Enter your choice (A,B,C,D,E):");
scanf("%s",ch);
  

  
  
if (ch[0] == 'E' || ch[0] == 'e')
break;
if (ch[0] == 'A' || ch[0] == 'a'){
printf("Dot product of A and B is %d ",dotProduct(A,n,B,n));
  
}
if (ch[0] == 'B' || ch[0] == 'b'){
printf(" Array A or B: ");
scanf("%s",ch1);
if (ch1[0] == 'A' || ch1[0] == 'a'){
printf("%d prime numberes in array %c ",numPrime(A,n), 'A');
}
if (ch1[0] == 'B' || ch1[0] == 'b'){
printf("%d prime numberes in array %c ",numPrime(B,n), 'B');
}
  
}
if (ch[0] == 'C' || ch[0] == 'c'){
arrayDifference(A,B,n,n);
  
}
if (ch[0] == 'D' || ch[0] == 'd'){
arrayIntersect(A,B,n,n);
  
}
  
}
  
return 0; /* indicate that program ended successfully */
} /* end function main */

output

Enter size of array : 3
Enter A:
2 2 4
Enter B:
4 4 4

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):a
Dot product of A and B is 32

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):b

Array A or B: a

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):^X

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):e
amoeba-2:Test2 raji$ gcc array_utils.c
amoeba-2:Test2 raji$ ./a.out
Enter size of array : 3
Enter A:
2 2 4
Enter B:
4 4 4

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):a
Dot product of A and B is 32

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):b

Array A or B: a
2 prime numberes in array A

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):b

Array A or B: b
0 prime numberes in array B

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):c
Difference array is [2 ]

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):d
Intersection array is [4 ]

A.Find Dot Product
B.Find Number of Prime Integers In Array
C.Array A Difefrence With B
D.Array Intersection
E.Exit
Enter your choice (A,B,C,D,E):e

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