Question

Q3) Write a function that takes two arrays and their size as inputs, and calculates their...

Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows:

N−1
c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1),

i=0

where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1) and D = (3,4,2,0,2) is c2 = 36.

You must use the following function prototype:

                    int innerProduct(int A[], int B[], int size);

Note: You must write your function prototype in a header file named libvector.h and you must write your function definition in a source file named libvector.c. We are providing you the main source file vector.c, which you can use to test your function. Do not change anything in vector.c.

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

int main()
{
int* array1;
int* array2;
int n, c, result;

printf("Enter number of elements:\n");
scanf("%d", &n);

/* dynamic memory allocation */
array1 = (int*) malloc(n * sizeof(int));
array2 = (int*) malloc(n * sizeof(int));

printf("Enter the %d elements of vector A:\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array1[c]);

printf("Enter the %d elements of vector B:\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array2[c]);

result = innerProduct(array1, array2, n);

printf("The inner product of vectors A and B is: %d.\n", result);

return 0;
}

Homework Answers

Answer #1

Put this code into "libvector.h":

int innerProduct(int A[], int B[], int size){
   int i, sum=0;
   for(i=0;i<size;i++){
   sum+=A[i]*B[i];
   }
   return(sum);
}

vector.c file:

libvector.h file:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
Expand the code below to display both the value of the loop variable and this value...
Expand the code below to display both the value of the loop variable and this value squared. Put the pairs of values on a new line. #include <stdio.h> #include <stdlib.h> int main(void) { int i; int a; int b; do { printf("Please enter the lower value: "); scanf("%d", &a); printf("Please enter the upper value: "); scanf("%d", &b); if (a>b) printf("The upper value must be greater than the lower value\n\n"); } while (a>b); for(i=a; i<=b; i++) printf("%d ", i); return 0;...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");     printf("1-Insurence Plan Subscription\n");     printf("2-Claim Processing\n");     printf("3-Accounts Information\n");     printf("4-Searching Functionalities\n");     printf("5-Exit\n");     scanf("%d", &choice);   } while (choice > 5 || choice < 1);   return choice; void subscribe() system("cls");   victims[userCount].id = userCount + 1;   printf("Enter age: ");   scanf("%d", &victims[userCount].age);   printf("\n\n%-25sHealth Insurence Plan\n\n", " ");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000,150000,200000);   printf("|%-30s|%-20d|%-20d|%-20d|\n",...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line argument and it is -hw you should simply print hello world and then exit(0). Otherwise, the program should provide a function named mm_alloc. This is the function prototype: void *mm_alloc(int num_bytes_to_allocate) mm_alloc function should obtain space for the user from a block which you obtain via mmap. You should obtain the block on the first invocation and the block size should be some number...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT