Question

Implementing the function method using c, some sample codes are showed below: typedef struct{ double *vector;...

Implementing the function method using c, some sample codes are showed below:

typedef struct{
double *vector;
int count;
int length;
}Vector;

Vector create_vector(int length){
Vector vec;
vec.vector = (int*)malloc(sizeof(int)*length);
if (vec.vector == NULL){
vec.length = 0;
vec.count =0;
return vec;
}vec.count =0;
vec.length = length;
}

/*
Calculate and return the variance of the elements in
a vector.
*/
double var(Vector vec) {
}

/*
Calculate and return the standard deviation of the
elements in a vector.
*/
double stdv(Vector vec) {
}

/*
Perform the calculation of the dot product of two vectors,
where dbl += v1[i] * v2[i] and return the resulting
double.
*/
double dot(Vector v1, Vector v2) {
}

/*
Perform an element by element comparison of two vectors.
If for every i, v1[i] == v2[i], and the count is equal
return 1, otherwise zero.
*/
int equals(Vector v1, Vector v2) {
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

please find the code, do comment if any syntax mismatch. i will be happy to help. thank you.

code:

typedef struct{
double *vector;
int count;
int length;
}Vector;
Vector create_vector(int length){
Vector vec;
vec.vector = (int*)malloc(sizeof(int)*length);
if (vec.vector == NULL){
vec.length = 0;
vec.count =0;
return vec;
}vec.count =0;
vec.length = length;
}
/*
Calculate and return the variance of the elements in
a vector.
*/
double var(Vector vec) {
int i,n;
double sum,sum1,average;
n= vec.length;
for (i = 0; i < n; i++)
{
scanf("%f", &vec[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + vec[i];
}
average = sum / (float)n;
/* Compute variance */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((vec[i] - average), 2);
}
variance = sum1 / (float)n;
return var;
}
/*
Calculate and return the standard deviation of the
elements in a vector.
*/
double stdv(Vector vec) {
double var = variance (vec);
return =sqrt(var);

}
/*
Perform the calculation of the dot product of two vectors,
where dbl += v1[i] * v2[i] and return the resulting
double.
*/
double dot(Vector v1, Vector v2) {
double result = 0.0;
int n = v1.length;
for (int i = 0; i < n; i++)
result += v1[i]*v2[i];
return result;
}
/*
Perform an element by element comparison of two vectors.
If for every i, v1[i] == v2[i], and the count is equal
return 1, otherwise zero.
*/
int equals(Vector v1, Vector v2) {
int n = v1.length;
int checker = 1;
for (int i = 0; i < n; i++)
{
if(v1[i] != v2[i])
{
checker=0;
break;
}
}
return checker;
  
}

Add a comment
Know the answer?
Add Answer to:
Implementing the function method using c, some sample codes are showed below: typedef struct{ double *vector;...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

    C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct nodet *makeAnode(int val) { struct nodet *box; box = malloc(sizeof(struct nodet) ); box->data = val; box->link = NULL; return box; } void printList(struct nodet *L) { struct nodet = *mov; mov = L; while(mov != NULL) { printf("%d ", mov->data); mov = mov->link; } printf("\n"); } // THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST. int listLen(struct nodet **L) { int...

  • Expected OUTPUT: Codes Failed at Test D,E,F. Needs to be fixed: public class Vector<T> { private...

    Expected OUTPUT: Codes Failed at Test D,E,F. Needs to be fixed: public class Vector<T> { private const int DEFAULT_CAPACITY = 10; private T[] data; public int Count { get; private set; } = 0; public int Capacity { get { return data.Length; } }    public Vector(int capacity) { data = new T[capacity]; }    public Vector() : this(DEFAULT_CAPACITY) { } public T this[int index] { get { if (index >= Count || index < 0) throw new IndexOutOfRangeException(); return...

  • 2) Consider the following C function: ----------------------------------------------------------------------------- int vdiv(double * a, double * b, double *c,...

    2) Consider the following C function: ----------------------------------------------------------------------------- int vdiv(double * a, double * b, double *c, int siz) {// divide vectors element-wise: c=a/b; // replaces divide by almost 0 with 1e99 and returns count int result=0; int i=0; 1: while(i<siz) 2: { if( abs(b[i])<1e-10 ){ // close to 0 3: c[i]=1e99; 4: result++; 5: }else{ 6: c[i]=a[i]/b[i]; 7: } 8: i++; 9: } 10: return result; } -------------------------------------------------------------------------- Executable lines are numbered. -Find all basic blocks, -draw control flow graph...

  • IrmaMoves.h : #include "IrmaMoves.h" typedef struct Move { Irma irma;            // an instance of Irma L...

    IrmaMoves.h : #include "IrmaMoves.h" typedef struct Move { Irma irma;            // an instance of Irma L Location from_loc; // location where Irma is moving from Location current_loc; // location where Irma is passing over Location to_loc; // location where Irma is moving to } Move; typedef struct Location { char col; // the square's column ('a' through 'h') int row; // the square's row (0 through 7) } Location; typedef struct Irma { int ws; // wind speed (MPH) int...

  • How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity =...

    How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity = 8 0 5 10 15 20 Capacity = 16 0 5 10 15 20 25 30 35 40 Capacity = 32 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125...

  • Improve the code below by creating an addBefore method. Instead of adding an element to the...

    Improve the code below by creating an addBefore method. Instead of adding an element to the end, addBefore adds it to the beginning, at position 0, causing all existing elements to move forward (their position increased by 1). However, like add, your addBefore method must also guarantee that only O(?) time is needed to perform n addBefores and adds. To accomplish this, as with add, most calls to addBefore must execute very quickly, in O(1) constant time, meaning that you...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • Using c 3 File Input & Data Processing Reading data from a file is often done in order to pro...

    using c 3 File Input & Data Processing Reading data from a file is often done in order to process and aggregate it to get ad- ditional results. In this activity you will read in data from a file containing win/loss data from the 2011 Major League Baseball season. Specifically, the file data/mlb_nl_2011.txt contains data about each National League team. Each line contains a team name fol- lowed by the number of wins and number of losses during the 2011...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT