Question

In: Computer Science

using c language: Add two vectors of doubles. Name this function vect_add(). Subtract two vectors of...

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

Solutions

Expert Solution

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


Related Solutions

Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator. For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as...
C++ For this assignment, you will write a C++ program to either add (A), subtract (S),...
C++ For this assignment, you will write a C++ program to either add (A), subtract (S), multiply (M), and (N), or (O) two matrices if possible. You will read in the dimensions (rows, then columns) of the first matrix, then read the first matrix, then the dimensions (rows then columns) of the second matrix, then the second matrix, then a character (A, S, M, N, O) to determine which operation they want to do. The program will then perform the...
MUST BE DONE IN C (NOT C++) In this task, using a function, we will add...
MUST BE DONE IN C (NOT C++) In this task, using a function, we will add a range of values of an array. The range will be determined by the user. For example, if I have the following array … 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells me to add from the 3rd element to the 6th element, my program would add the values 8.9, 4.6, 7.8 and 995.1. To do so, please follow...
Variable Register a $9 b $19 c $2 C Operators + add - subtract & bitwise...
Variable Register a $9 b $19 c $2 C Operators + add - subtract & bitwise and ! bitwise or ~ bitwise not Assume the variables, a, b and c are stored in the registers given above. Give a single MIPS assembly instruction from the MIPS Core Instruction Set that performs the equivalent operation for each of the following C statements. a = ~ ( b | c ); // MIPS equivalent: -------- c = b - 2; // MIPS...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
C++ Write a function that accepts an array of doubles and the array's size as arguments....
C++ Write a function that accepts an array of doubles and the array's size as arguments. The function should display the contents of the array to the screen.
6. Add using binary addition 26 and 56 45 and 65      7. Subtract using binary...
6. Add using binary addition 26 and 56 45 and 65      7. Subtract using binary two’s complement 23 from 45 34 from 65
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT