Question: Linear Algebra API (C programming) Description: In this part of the assignment, you are to implement functions to initialize or set vectors and functions to
Linear Algebra API (C programming)
Description: In this part of the assignment, you are to implement functions to initialize or set vectors and functions to manipulate vectors for a Linear Algebra API.
Output: The output must be the same as in the example below. Notice that random numbers may be different on your computer. To make sure that your program produces the correct answer, iLeap uses a seed. Do not add any seed to your program.
Sample Output 1
Welcome to Linear Algebra Begin calculateFindVectorValues setRandomVector v1 14 14 9 27 20 27 setRandomVector v2 16 15 15 8 13 9 dotProductVectors = 1288 sumVector = 111 findMaxInVector = 27 findMinInVector = 9 Value 27 found in Vector v1 at index 3 Value -77 not found in Vector v1 End calculateFindVectorValues End Linear Algebra
--------------------------------------------------
CODE must use:
#include#include #include #define VECMAX (6) #define LOW (7) #define HIGH (27) #define SPECINDEX (3) #define WILLNOTBEFOUND (-77) #define NOTFOUND (-1) typedef int Item; typedef int Index; typedef Item Vector[VECMAX]; // prototypes for CSC 111 Vector API // functions for testing Vector API int main(void); void printVector(char* msg, Index len, const Vector v); void printValueFound(bool found, Item val, Item fidx); int prngInt(int iLow, int iHigh); float prngFloat(float fLow, float fHigh, float precision); void calculateFindVectorValues(void); // initialize or set vectors void setRandomVector(Index len, Vector v, Item low, Item high); // calculate for vectors and find values in vectors Item sumVector(Index len, const Vector v); Item dotProductVectors(Index len, const Vector v1, const Vector v2); Item findMaxInVector(Index len, const Vector v); Item findMinInVector(Index len, const Vector v); bool findValueInVector(Index len, const Vector v, Item val, Index* fidx); int main(void) { printf("Welcome to Linear Algebra "); calculateFindVectorValues(); printf("End Linear Algebra "); return EXIT_SUCCESS; }//main void calculateFindVectorValues(void){ printf("Begin calculateFindVectorValues "); Vector v1; Vector v2; setRandomVector(VECMAX, v1, LOW, HIGH); printVector("setRandomVector v1", VECMAX, v1); setRandomVector(VECMAX, v2, LOW, HIGH); printVector("setRandomVector v2", VECMAX, v2); Item dot = dotProductVectors(VECMAX, v1, v2); printf("dotProductVectors = %d ", dot); Item sum = sumVector(VECMAX, v1); printf("sumVector = %d ", sum); Item max = findMaxInVector(VECMAX, v1); printf("findMaxInVector = %d ", max); Item min = findMinInVector(VECMAX, v1); printf("findMinInVector = %d ", min); Index fidx = NOTFOUND; Item val = v1[SPECINDEX]; bool found = findValueInVector(VECMAX, v1, val, &fidx); printValueFound(found, val, fidx); fidx = NOTFOUND; val = WILLNOTBEFOUND; found = findValueInVector(VECMAX, v1, val, &fidx); printValueFound(found, val, fidx); printf("End calculateFindVectorValues "); }//calculateFindVectorValues void printValueFound(bool found, Item val, Item fidx) { if (found) printf("Value %d found in Vector v1 at index %d ", val, fidx); else printf("Value %d not found in Vector v1 ", val); }//printValueFound void printVector(char* msg, Index len, const Vector v) { // print all values of 'v' onto one console line printf("%s ", msg); for (Index k = 0; k < len; k++) printf("%d ", v[k]); printf(" "); }//printVector int prngInt(int iLow, int iHigh) { int mod = iHigh - iLow + 1; return rand() % mod + iLow; }//prngInt float prngFloat(float fLow, float fHigh, float precision) { int iLow = (int)(fLow*precision); int iHigh = (int)(fHigh*precision); int mod = iHigh - iLow + 1; int irn = rand()%mod + iLow; return ((float)(irn)) / precision; }//prngFloat void setRandomVector(Index len, Vector v, Item low, Item high){ // initialize each component of 'v' with a pseudo // random numbers in the range of 'low' to 'high' for (Index k=0; k
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
