0

I am trying to write a C program that calculates the min and max value from entered numbers. I managed find the min and max value, but for some reason i cannot print out the values outside the function. This is my code:

#include <stdio.h>
#include <stdlib.h>

void find_largest_smallest(int a[], int n, int *largest, int *smallest)
{
    largest=smallest=a[0];
    int i;
    for(i=0; i<n; i++)
    {
        if (largest<a[i])
            largest=a[i];
    }


    for(i=0; i<n; i++)
    {
        //printf("%d\n", a[i]);
        if (smallest>a[i])
            smallest=a[i];
    }
    printf("Largest is %d\n", largest);
    printf("smallest is %d\n", smallest);
}

int main()
{
    int elem;
    int i;
    int *x;
    int *y;

    printf("How many elements you want to store ?:");
    scanf("%d", &elem);
    int store[elem];
    for(i=0; i<elem; i++)
    {
        printf("Enter a value to be stored:");
        scanf("%d", &store[i]);
    }

    find_largest_smallest(store, elem, &x, &y);

    printf("Largest value stored is %d and the smallest is %d.", *x, y);

    return 0;
}
3
  • 1
    Enable compiler warnings. And resolve them all. largest=a[i]; - largest is a pointer to int, a[i] is an int. Commented Sep 30, 2019 at 10:53
  • 1
    largest -> *largest etc. And turn on those compiler warnings. Commented Sep 30, 2019 at 10:53
  • And you can set the largest and the smallest in the same loop (there is no need to iterate twice) Commented Sep 30, 2019 at 10:54

2 Answers 2

2

This:

largest=smallest=a[0];

Is wrong. You are assigning an integer to a pointer. What you should do is instead:

*largest = a[0];
*smallest = a[0];

Same goes for other assignments and reads:

if (*largest < a[i])
    *largest = a[i];

/* ... */

if (*smallest > a[i])
    *smallest = a[i];

/* ... */

printf("Largest is %d\n", *largest);
printf("smallest is %d\n", *smallest);

The declaration of x and y in main should just be int (not int *):

int x, y;

The call to printf in main is also wrong:

printf("Largest value stored is %d and the smallest is %d.", x, y);
//                         no asterisk needed here ----------^
Sign up to request clarification or add additional context in comments.

Comments

1

The parameters largest and smallest have pointer types

void find_largest_smallest(int a[], int n, int *largest, int *smallest)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^  

So within the function you have to dereference the pointers to access pointed objects.

For example

void find_largest_smallest(int a[], int n, int *largest, int *smallest)
{
    *largest = *smallest = a[0];
    int i;
    for ( i = 1; i<n; i++ )
    {
        if ( *largest<a[i] )
            *largest=a[i];
    }


    for ( i = 1; i<n; i++ )
    {
        //printf("%d\n", a[i]);
        if ( *smallest>a[i] )
            *smallest=a[i];
    }
    printf("Largest is %d\n", *largest);
    printf("smallest is %d\n", *smallest);
}

Pay attention to that you could find the largest and the smallest elements using only one loop. Apart this the function should calculates pointers to the largest and smallest elements instead of their values because in general the user can pass the size of the array equal to 0. In this case the function will have undefined behavior.

Also within main the variables x and y should have the type int. That is

int x;
int y;

//...

printf("Largest value stored is %d and the smallest is %d.", x, y );

3 Comments

Does your answer really add anything new to Marco's?
@Adrian It seems we were writing answers simultaneously.
Happens a lot, I know!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.