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;
}
largest=a[i];-largestis a pointer toint,a[i]is anint.largest->*largestetc. And turn on those compiler warnings.largestand thesmallestin the same loop (there is no need to iterate twice)