Question

In: Computer Science

C Programming: Problem 1: Write a function that returns the index of the largest value stored...

C Programming:

Problem 1:

Write a function that returns the index of the largest value stored in an array-of- double. Test the function in a simple program

Problem 2:
Write a function that returns the difference between the largest and smallest elements of an array-of-double. Test the function in a simple program

Solutions

Expert Solution

1.

Program:

#include <stdio.h>

int max_index(double arr[], int n);

int main(void) {

int n,index; // variable Declaration

double x[50];

printf("Enter number of elements in the array: ");

scanf("%d",&n); // Accept the number

printf("Enter array elements: \n");

for(int i=0; i<n; i++) // loop to accept array elements

scanf("%lf",&x[i]); // accept array elements

index = max_index(x, n); // calling function

printf("Max element index is : %d\n",index); // print max index

return 0;

}

int max_index(double arr[], int n){

int index; // variable Declaration

double max = arr[0]; // variable Declaration

for(int i=0; i<n; i++) { // loop to find max element index

if(max<arr[i]) {

max=arr[i];

index = i; // index contain max element index

}

}

return index; // return max index

}

Output:

2.

Program:

#include <stdio.h>

double diffLargeSmall(double arr[], int n);

int main(void) {

int n; // variable Declaration

double x[50];

printf("Enter number of elements in the array: ");

scanf("%d",&n); // Accept the number

printf("Enter array elements: \n");

for(int i=0; i<n; i++) // loop to accept array elements

scanf("%lf",&x[i]); // accept array elements

double difference = diffLargeSmall(x, n); // calling function

printf("Max element index is : %.2lf\n",difference); // print max index

return 0;

}

double diffLargeSmall(double arr[], int n){

double large = arr[0]; // variable Declaration

double small = arr[0]; // variable Declaration

for(int i=1; i<n; i++) { // loop to find largest and smallest

if(large<arr[i]) {

large=arr[i]; // calculate large

}

if(small>arr[i]) {

small=arr[i]; // calculate small

}

}

return large-small; // return difference of large and small

}

Output:

​​​​​​

Any doubts leave a comment

Please Rate My Answer


Related Solutions

In C++, write a function that returns the average of all the values stored in an...
In C++, write a function that returns the average of all the values stored in an integer array (called arr) of size n. The return type of the function should be double. Test this function in the main program.
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
1) Write a function equation(A, B) that returns a value C where A + B =...
1) Write a function equation(A, B) that returns a value C where A + B = C a. Write the equation function: b: Write the output of your function for a few values: 2) Write a non-recursive function called fact(n) that computes n! Try computing fact(n) for large values. Can you find the maximum value for n your program will compute correctly? a) Write your fact function: b) Write the output of your function for a few values: 3) Write...
in C programming language, write and test a function that writes and returns through an output...
in C programming language, write and test a function that writes and returns through an output parameter the longest common suffix of two words. (e.g. The longest common suffix of "destination" and "procrastination" is "stination", of "globally" and "internally" is "ally, and of "glove" and "dove" is the empty string)
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the...
Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the given child_index. A None value should be returned if the child has no parent. Notes: This function is obviously for a 3-heap! The values in the heap list start at index 1 - that is the root of the heap is at index 1 in the heap list. This is for Python
Write a function intLog of type Integer -> Integer that returns the exponent of the largest...
Write a function intLog of type Integer -> Integer that returns the exponent of the largest power of 2 less than its integer argument. This needs to be a haskell function
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT