In: Computer Science
using c language:
Add two vectors of doubles. Name this function vect_add(). Subtract two vectors of doubles. Name this function vect_sub(). Multiplies element by element two vectors. Name this function vect_prod(). Compute and return the dot product of two vectors. Name this function vect_dot_prod(). The dot product operation is the result of taking two vectors(single dimension arrays) and multiplying corresponding element by element and then adding up the sum of all the products. For example, if I have 2 vectors of length 3 that have the following values {l, 2, 3} and {3, 2, l} the dot product of these two vectors is: 1*3+2*2+3*1 = 10 Compute and return the mean of a single vector. Name this function vect_mean(). Compute and return the median of a single vector. Name this function vect_median(). First sort the array. If the length of the sorted array is odd, then the median is the middle element. If the length is odd, the median is the average of the middle two elements. Compute and return the maximum element of a vector. Name this function vect_max(). Compute and return the minimum element of a vector. Name this function vect_min(). Write a function that reverses the elements in a given array. This function will change the original array, passed by reference. Name this function vect_reverse().
C CODE:
#include <stdio.h> #include <stdlib.h> int* vect_add(int a[], int b[], int n){ int *c = (int*)malloc(sizeof(int) * n); for(int i = 0; i < n; i++) c[i] = a[i] + b[i]; return c; } int* vect_sub(int a[], int b[], int n){ int *c = (int*)malloc(sizeof(int) * n); for(int i = 0; i < n; i++) c[i] = a[i] - b[i]; return c; } int* vect_prod(int a[], int b[], int n){ int *c = (int*)malloc(sizeof(int) * n); for(int i = 0; i < n; i++) c[i] = a[i] * b[i]; return c; } int vect_dot_prod(int a[], int b[], int n){ int dot = 0; for(int i = 0; i < n; i++) dot += a[i] * b[i]; return dot; } float vect_mean(int a[], int n){ float sum = 0; for(int i = 0; i < n; i++) sum += a[i]; return sum/n; } float vect_median(int a[], int n){ // Sort array for(int i = 0; i < n - 1; i++){ for(int j = 0; j < n - i - 1; j++){ if(a[j] > a[j + 1]){ // Swap adjacent elements int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } if(n % 2 == 1)//If length of array is odd return a[n/2]; else return (a[n/2] + a[n/2 - 1])/2.0f; } int vect_max(int a[], int n){ float max = a[0]; for(int i = 1; i < n; i++) if(a[i] > max) max = a[i]; return max; } int vect_min(int a[], int n){ float min = a[0]; for(int i = 1; i < n; i++) if(a[i] < min) min = a[i]; return min; } void vect_reverse(int a[], int n){ int l = 0, r = n - 1; while(l < r){ //Swap a[l] with a[r] int temp = a[l]; a[l] = a[r]; a[r] = a[l]; l++; r--; } } int main(){ int a[] = {1, 2, 3}; int b[] = {3, 2, 1}; int c[] = {5, 6, 7, 8}; printf("Dot product = %d\n", vect_dot_prod(a, b, 3)); printf("Mean of vector a = %f\n", vect_mean(a, 3)); printf("Mean of vector b = %f\n", vect_mean(b, 3)); printf("Mean of vector c = %f\n", vect_mean(c, 4)); printf("Median of vector a = %f\n", vect_median(a, 3)); printf("Median of vector b = %f\n", vect_median(b, 3)); printf("Median of vector c = %f\n", vect_median(c, 4)); printf("Max of vector a = %d\n", vect_max(a, 3)); printf("Max of vector b = %d\n", vect_max(b, 3)); printf("Max of vector c = %d\n", vect_max(c, 4)); printf("Min of vector a = %d\n", vect_min(a, 3)); printf("Min of vector b = %d\n", vect_min(b, 3)); printf("Min of vector c = %d\n", vect_min(c, 4)); return 0; }
SAMPLE OUTPUT:
FOR ANY HELP JUST DROP A COMMENT