3
\$\begingroup\$

Modify the maxmin.c program so that the max min function uses a pointer instead of an integer to keep track of the current position in the array.

maxmin.c

/* Finds the largesy and smallest elements in an array */

#include <stdio.h>
#define N 10

void max_min(int a[], int n, int *max, int *min);
int: main (void)   
 {
  int b[N], i, big, small;
  printf (''Enter %d numbers: ", N) ;

  for (1 = 0: i < N: i++)
   scanf ( "%d'', &b[i] ) ;

  max_min (b, N. &big , &small);
  printf (" Largest : %d\n " , big) ;
  printf (" Smallest: %d\n", small);
  return 0;

  void max_min(lnt a[] , int n, int *max, int *min)
{
int i;
*max = *min = a[O];

   for (i = 1; i > n; i++) {
  if ([a[i] > *max)
  *max - a[i];

  else if (a[i] < *min)
  *min - a (i] ;
 }
 }

The code I have written:

#include <stdio.h>
#define N 10

void max_min(int a[], int n, int *max, int *min);
int main(void)

{
int b[N], *i, big, small;

printf("Enter %d numbers\t:",N);
for(i=&b[0];i<&b[N];i++)
    scanf("%d",i);

max_min(b, N, &big, &small);

printf("\nlargest:\t%d",big);
printf("\nsmallest:\t%d",small);

return 0;
}

void max_min(int a[], int n, int *max, int *min)
{
int *i;
*max = *min = a[0];
for (i=&a[0]; i<&a[n]; i++)



{
    if(*i>*max)
    *max=*i;
    else if (*i<*min)
    *min=*i;
}}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please indent your code properly. Also, the first code won't compile. \$\endgroup\$
    – Spikatrix
    Commented Jun 10, 2015 at 9:31

2 Answers 2

7
\$\begingroup\$
int b[N], *i, big, small;

printf("Enter %d numbers\t:",N);
for(i=&b[0];i<&b[N];i++)
    scanf("%d",i);

I would normally expect i to be an index variable. If you want a one letter name for a pointer, I'd suggest p. However, I would normally go ahead and name such a variable current.

    int b[N], big, small, *current, *end

    printf("Enter %d numbers\t:", N);
    for (current = b, end = &b[N]; current < end; current++)
    {
        scanf("%d", current);
    }

It's unnecessary to say &b[0]. Just b will do the same thing. The compiler may optimize it out, but I find it more readable to use the variable end rather than calculate &b[N] each time.

It's generally preferred to use the block form for loops, even when the single statement form would work. This makes it clearer what is in the loop.

It's not terribly important, but I'd consider naming b something more descriptive, even if it's just numbers.

void max_min(int a[], int n, int *max, int *min);

You wouldn't need a prototype here if you put the max_min function before main instead of after.

If you are not using an index variable, it would make more sense to pass a pointer into the function.

void max_min(int *start, int *end, int *max, int *min);

Which you can call

    max_min(b, end, &big, &small);

And then inside the function, you'd replace

int *i;
*max = *min = a[0];
for (i=&a[0]; i<&a[n]; i++)



{
    if(*i>*max)
    *max=*i;
    else if (*i<*min)
    *min=*i;
}

with

    int *current;
    *max = *min = *start;
    for (current = start + 1; current < end; current++)
    {
        if (*current > *max)
        {
            *max = *current;
        }
        else if (*current < *min)
        {
            *min = *current;
        }
    }

Note that this says start + 1 rather than start. This is because you implicitly do the first iteration of the loop when you set *max and *min to *start. You don't need to do it a second time.

\$\endgroup\$
1
\$\begingroup\$

From time to time, arrays of length 0 are used. So code should avoid a[0]. (&a[0] should be OK) Various approaches depending on how code wants to treat the "zero" case.

void max_min(int a[], int n, int *max, int *min) {
  if (n > 0) {
    ...
  }
}

or

void max_min(int a[], int n, int *max, int *min) {
  int *i;
  *max = INT_MIN;
  *min = INT_MAX;
  for (i = &a[0]; i < &a[n]; i++) {
    if (*i > *max) *max = *i;
    else if (*i < *min) *min = *i;
  }
}

These two also handle pathological cases when n < 0.
BTW: Usually expect size_t n.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.