Question

This program should be in C. It should have adequate comments. Write a function called difference...

This program should be in C. It should have adequate comments. Write a function called difference () that returns the difference between the largest and smallest elements of an array-of-long. Use only pointers to iterate the array. Test the function in a simple program.

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

Here is code:

int difference(int *p)
{
int max,min;
// initial adding the starting values
max = *p;
min = *p;
// loops from array strating to end of array where the end of array end with null
while(*p)
{
   // if current value is greater then max
if(max < *p)
max = *p;
// if current value is lesser then min
if(min > *p)
min = *p;
*p++; // next value
}
return max - min;
}

Sample code to test:

#include <stdio.h>

int difference(int *p)
{
int max,min;
// initial adding the starting values
max = *p;
min = *p;
// loops from array strating to end of array where the end of array end with null
while(*p)
{
   // if current value is greater then max
if(max < *p)
max = *p;
// if current value is lesser then min
if(min > *p)
min = *p;
*p++; // next value
}
return max - min;
}
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9};
int diff = difference(array);
printf("The difference is %d",diff);
return 0;
}

Output:

T he diff erence 15

Add a comment
Know the answer?
Add Answer to:
This program should be in C. It should have adequate comments. Write a function called difference...
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
  • c++ 2. Write a CH function called range that returns the difference between the largest and...

    c++ 2. Write a CH function called range that returns the difference between the largest and smallest elements in an array.

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

  • Your task is to write the initDeck() function, which will be called at the start of...

    Your task is to write the initDeck() function, which will be called at the start of the program to initialize all of the cards data structures. The data structure definitions and a basic main program will be made available for you to use to develop your code.   The initDeck() function: /* Function: initDeck() * Purpose: iterate through entire deck and set to default values * Accepts: array of Cards (the deck) * Returns: void */ void initDeck(struct Card deck[]) {...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • #Python 1.Write a function called difference() that accepts a list of numbers from the user and...

    #Python 1.Write a function called difference() that accepts a list of numbers from the user and returns the difference between the largest and the smallest numbers in the list as follows: >>> difference() Please enter a list: [100, 102, 106, -10] 116 >>> difference() Please enter a list: [1,2,3,4,5,6,7,8,9,10] 9 >>>

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • Need help with these, especially number 4. C++ program L. WIite the function Write a function...

    Need help with these, especially number 4. C++ program L. WIite the function Write a function mpgcalculator that accepts pointers to three arrays. The firstis an array of miles, the second is an array of gallons, the third will hold mile-per-gallon. It has no return value. 4. a. b. c. d. write the prototype call this function with these three arrays: miles, gallons, mpg. write the function use only array notation 5. Do #4, using only array with offset notation....

  • Please write program in C++ Write a function called randCount with no parameters. The function should...

    Please write program in C++ Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function. please write the program in C++

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