Max value is wrong cant find solution

Here is my code I can not find out way my max variable is wrong.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 5

int random(int [], int, int, int);
int display(int, int, int);
void heading();

int main()
{
int number, total = 0;
int store[SIZE];

heading();

random(store, SIZE, number, total);

return 0;
}
int random(int arr[], int sz, int num, int sum)
{
int counter, mx = 50, mn = 1;
srand(time(NULL));
mx = num;
mn = num;

for(counter = 0;counter < sz;counter++)
{
num = 1 + rand() % 51;

printf("%d ", num);

arr[counter] = num;

if(num < mn)
mn = num;
if(num > mx)
mx = num;

sum = num + sum;
}
display(mn, mx, sum);

return 0;
}
int display(int x, int y, int z)
{
printf("\n\n");
printf("The min value is %d.\n", x);
printf("The max value is %d.\n", y);
printf("The total of numbers is %d.\n", z);
}
void heading()
{
printf("This program generates 5 random numbers and stores them in an array.\n");
printf("Then determines which number is largest and smallest.\n\n");
}
Please use [code] tags.

At the beginning of random(), you initialize mx and mn to num, which is uninitialized and thus is garbage data.
I took off the initialization of mx and mn to num. It is not giving me the right mx and mn values for the random numbers generated.
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <limits.h>

#define SIZE 5

int random(int);// You send only one value so you need one argument
void display(int, int, int);
void heading();

int main()
{
    heading();

    random(SIZE);

return 0;
}

int random(int sz)
{
    int counter = 0,sum = 0,arr[sz];
    int mx = INT_MIN, mn = INT_MAX,num = 0;

    srand(time(NULL));

    for(counter = 0;counter < sz;counter++)
    {
        num = 1 + rand() % 51;

        printf("%d ", num);

        arr[counter] = num;

        if(num < mn)
            mn = num;
        if(num > mx)
            mx = num;

        sum = num + sum;
    }
    display(mn, mx, sum);

    return 0;
}

void display(int x, int y, int z)
{
    printf("\n\n");
    printf("The min value is %d.\n", x);
    printf("The max value is %d.\n", y);
    printf("The total of numbers is %d.\n", z);

    // Return something or void this function
}

void heading()
{
    printf("This program generates 5 random numbers and stores them in an array.\n");
    printf("Then determines which number is largest and smallest.\n\n");
}
Thank you from helping me
Topic archived. No new replies allowed.