patterncMinor
Finding the min and max of an array, using a pointer
Viewed 0 times
thearrayminpointermaxusingfindingand
Problem
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
The code I have written:
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
#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++) {
if ([a[i] > *max)
*max - a[i];
else if (a[i] < *min)
*min - a (i] ;
}
}The code I have written:
#include
#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*max)
*max=*i;
else if (*i<*min)
*min=*i;
}}Solution
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*max)
*max=*i;
else if (*i<*min)
*min=*i;
}with
int *current;
*max = *min = *start;
for (current = start + 1; 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.Code Snippets
int b[N], *i, big, small;
printf("Enter %d numbers\t:",N);
for(i=&b[0];i<&b[N];i++)
scanf("%d",i);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);
}void max_min(int a[], int n, int *max, int *min);void max_min(int *start, int *end, int *max, int *min);max_min(b, end, &big, &small);Context
StackExchange Code Review Q#93120, answer score: 7
Revisions (0)
No revisions yet.