• 03-10-2004
zp523444
Hi expert,

I need to write a program, function one assigns the maximum and minimum values in intArray to the variables pointed by min and max respectively. The parameter size is the size of intArray.

Code:

`void findMinMax(int intArray[], int size, int* min, int* max)`
Function two returns the smallest positive integer (greater than zero) found in intArray.

Code:

`int findSmallestPositive(int intArray[], int size)`
The array parameter should be traversed only once to maximize the running efficiency of
the function.

This is my script:
Code:

```#include <stdio.h> #include <limits.h> void findMinMax(int intArray[], int size, int* min, int* max) {     size=2;     if (min<max) {       intArray[0]=min;       intArray[1]=max;         }   else  {           intArray[0]=max;       intArray[1]=min;         } ; } int findSmallestPositive(int intArray[], int size) {     size=2;   if (intArray[0]<intArray[1])           return intArray[0];   else           return intArray[1]; }```
Am my code writing correctly? please told me my error
• 03-10-2004
Prelude
>void findMinMax(int intArray[], int size, int* min, int* max)
You're way off. Consider what you're being asked to do. Given an array, you need to find the smallest and largest out of all of the items in the array. This alone suggests a loop. You can only traverse the array once, so you need to save the smallest and largest so far. That way when you get to the end, you have the smallest and largest total. A good first try would look like this:
Code:

```void findMinMax(int intArray[], int size, int* min, int* max) {   int i;   *min = *max = intArray[0];   for ( i = 1; i < size; i++ ) {     if ( intArray[i] < *min )       *min = intArray[i];     if ( intArray[i] > *max )       *max = intArray[i];   } }```
A similar algorithm can be used to find the smallest positive number, just add the extra test that if intArray[i] < min and intArray[i] is also greater than or equal to 0, you make the assignment.
• 03-10-2004
spoon_
Code:

```int findSmallestPositive(int intArray[], int size) {     int i = 1;   int spos = intArray[0];   //assumes array index begins at 0 and ends at size - 1   for(; i < size; i++)   {         if( (intArray[i] >= 0) && (intArray[i] < spos) )             spos = intArray[i];   }     return spos; }```
You'll probably want to break that findMin and findMax up into two functions. I'll leave that to you.

oops, forgot return statement.
[/edit]
• 03-15-2004
anson1999
how can I return two value of min and max in one function?

Quote:

Originally posted by Prelude
>void findMinMax(int intArray[], int size, int* min, int* max)
You're way off. Consider what you're being asked to do. Given an array, you need to find the smallest and largest out of all of the items in the array. This alone suggests a loop. You can only traverse the array once, so you need to save the smallest and largest so far. That way when you get to the end, you have the smallest and largest total. A good first try would look like this:
Code:

```void findMinMax(int intArray[], int size, int* min, int* max) {   int i;   *min = *max = intArray[0];   for ( i = 1; i < size; i++ ) {     if ( intArray[i] < *min )       *min = intArray[i];     if ( intArray[i] > *max )       *max = intArray[i];   } }```
A similar algorithm can be used to find the smallest positive number, just add the extra test that if intArray[i] < min and intArray[i] is also greater than or equal to 0, you make the assignment.

• 03-15-2004
Prelude
>how can I return two value of min and max in one function?
You cannot return two values in C, only one. To return multiple values you must take a roundabout method such as using pointer arguments that reference the original objects or returning a structure of the values you want.