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
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
int numPrime(int *a, int size){ int i,count = 0; int found; int j;
for(i=0; i int dotProduct(int *a, int m, int *b, int n){ int prod = 0; int i; if (m > n){ for (i = 0; i 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 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 printf("Difference array is ["); for (i = 0; i } 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 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 */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
