Purpose: To introduce you to programming in C in a Unis environment Assignment W
ID: 3906440 • Letter: P
Question
Purpose: To introduce you to programming in C in a Unis environment Assignment Write a program to: Ask the user to enter the number of dimensions of each vector (1-10) . Ask the user to enter the coordinates of the first vector Ask the user to enter the coordinates of the second vector Compute and print the distance between both vectors Examples: /nulti01 Dist Please enter the number of dimensions (1-10) -2 Please enter the number of dinensions (1-1): 11 Please enter the number of dinensions (1-10): 2 Please enter coordinate e of vector A: 1 Please enter coordinate 1 of vector A: -1 Please enter coordinate 8 of vector 8: -3 Please enter coordinate 1 of vector 8:1 The distance between vector A and vector B is 2.82843 Organization: Your program should have 4 functions: int getNumDins) This function asks for an integer between 1-10. It keeps asking while the number the user enters is outside of this range. It returns the entered number whon it is valid This function lets the user onter the arrayLen numbers of aray array with name arrayhae int anraylen) float computeDistance (float arraye, float areayl, This fanction computes and returns the distance betwem both vecors. (The formula for this is suqet sum-O.arayLen-1, (array oi-armayti)) int sain Given belowExplanation / Answer
int getNumDims(){
int n;
while(1){
printf("Please enter the number of dimensions (1-10):");
scanf("%d", &n);
if (n >= 1 || n <= 10)
return n;
}
}
void entervector(const char* arrayname, float *array, int arraylen){
int i;
for (i = 0; i<arraylen; i++){
printf("Please enter coordinate %d of vector %s :",i,arrayname);
scanf("%f",array[i]);
}
}
float computeDistance(float *array1, float *array2, int arraylen){
float sum = 0;
for (int i = 0; i<arraylen; i++){
sum = sum + (array1[i] - array2[i]) * (array1[i] - array2[i]);
}
return sqrt(sum);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.