Question

C programming Write a function that goes through all elements of an array of size n,...

C programming

Write a function that goes through all elements of an array of size n, and output the minimum and maximum values using pointers.

Since C functions can only return one value, you must use pointers to output the minimum and maximum values from the function. You are not expected to print the values on the screen.

Note: you will be assessed on the functionality and the coding style.

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

#include <stdio.h>

//function to find the max and minimum using pointer
void minMax(int arr[], int n, int *min, int *max)
{
//initialize the pointer variable with array first element
*min = *max = arr[0];
  
//find the minimum and maximum
for(int i=1; i<n; i++)
{
if(*min>arr[i])
*min = arr[i];
  
if(*max<arr[i])
*max = arr[i];
}
}

int main()
{
//array declaration and initialization
int a[5] = {10, 25, 15, 9, 50};
  
//variable declaration
int min, max;
  
//function calling
minMax(a, 5, &min, &max);

//display minimum and maximum value
printf("\n Min = %d", min);
printf("\n Max = %d", max);

return 0;
}

OUTPUT:

Add a comment
Answer #2

#include <stdio.h>

//function to find the max and minimum using pointer
void minMax(int arr[], int n, int *min, int *max)
{
//initialize the pointer variable with array first element
*min = *max = arr[0];
  
//find the minimum and maximum
for(int i=1; i<n; i++)
{
if(*min>arr[i])
*min = arr[i];
  
if(*max<arr[i])
*max = arr[i];
}
}

int main()
{
//array declaration and initialization
int a[5] = {10, 25, 15, 9, 50};
  
//variable declaration
int min, max;
  
//function calling
minMax(a, 5, &min, &max);

//display minimum and maximum value
printf("\n Min = %d", min);
printf("\n Max = %d", max);

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
C programming Write a function that goes through all elements of an array of size n,...
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 programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use...

    C programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use of pointers), to pass the array to a function, named summation In main: define array, pass array, print out the array and the results on screen In function summation: take array from main and sum the array using the formula below: 10 fO 2 246 NOTE: you must use call-by-reference (the pointer) to call functions, as required; otherwise, no credits will be given.

  • I need help writing these functions in C programming. Write a C function that checks if...

    I need help writing these functions in C programming. Write a C function that checks if an array is sorted or not using an iterative approach. Write a C function that checks if an array is sorted or not using a recursive approach Write a C function to reverse the elements of an array. Note you are not allowed to use an 1. additional array to do that Write a C function to find the sum of the elements of...

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • C programming Write a function that accepts an array of integers as an input, and output...

    C programming Write a function that accepts an array of integers as an input, and output the sum of all values and the multiplication of all values. e.g. Suppose that the array contained the following values: 1 2 3 -4 5. The function should calculate and output the sum of values (i.e. 1+2+3+(-4)+5=7) and the multiplication of all values (i.e. 1*2*3*-4*5=-120). Start by carefully writing the function prototype - put some thought into this. Think about the good programming habits,...

  • In C: Write a function "MinMax" that takes as an argument an integer array, an integer...

    In C: Write a function "MinMax" that takes as an argument an integer array, an integer for the size of the array, and two integer pointers. The function should print nothing and return nothing but change the value of the first pointer to the minimum value in the array and change the value in the second pointer to the max value in the array.

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Write a C++ program to : Create array arr1 of size 10 Assign values to elements...

    Write a C++ program to : Create array arr1 of size 10 Assign values to elements such that - say index = i, arr1[i] = 10 - i. Print arr1 Create arr2 and copy alternate elements from arr1 starting from index 1. Print arr2. Sort arr1 in ascending array and print its elements from indices 3 to 8 Print every output on a different line and separate array elements by spaces.

  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

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