write the following C PROGRAM In this exercise you will write several functions
ID: 3594424 • Letter: W
Question
write the following C PROGRAM
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 (dot) b = 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);
(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 fills 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 fills 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 flow 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:
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.
Explanation / Answer
#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);
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){
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')
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.