Max and Min numbers goes wrong

Can anyone help on why my max and min numbers are wroing thx

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
 #include <stdio.h>

int main()
{
	int n, i;
	float num[10], sum = 0.0, average;
	float min = num[0];
	float max = num[0];

	printf("Enter the numbers : ");
	scanf_s("%d", &n);

	while (n > 10 || n <= 0)
	{
		printf("Error! number should in range of (1 to 10).\n");
		printf("Enter the number again: ");
		scanf_s("%d", &n);
	}

	for (i = 0; i < n; ++i)
	{
		printf("%d. Enter number: ", i + 1);
		scanf_s("%f", &num[i]);
		sum += num[i];
	}
	for (i = 0; i < min; i++)
	{
	if (num[i] < min)
		{
		min = num[i];
		}
	if (num[i] > min)
		{
		max = num[i];
		}
	}
	
	average = sum / n;
	printf("max = %d\n", max);
	printf("min = %d\n", min);
	printf("Average = %.2f", average);
	
	return 0;
}
Hello lsam18,

I have not run the program yet to test it, but the if statements at lines 28 and 32 are incorrect.

To start with line 6 defines the array "num" then on line 7 you define and set "min" to "num[0]", But what value does "num[0]" contain. Since it was not initialized before it was used it contains garbage. On my compute this number is usually "-858993460". For a float it would be different. By the time you reach line 28 the "num" array has chaged, but "min" still contains garbage, so when you make the comparison it will most likely come up false and "min" will never receive the correct number.

On line 32 you are comparing "num[i]" to "min" it should be "max". Again the same problem "max" was defined and initialized to garbage although it should work here.

I will suggest using "double"s over "floats"s as doubles have a greater precision and tend to store numbers better.

Testing the program I get some rather large scientific numbers for input. I think it has to do with the "scanf". Kind uf rusty on C so I will have to do some checking.

Hope that helps,

Andy
Lines 7 and 8. What is the value of num[0] at that moment? Uninitialized. Unknown.

You cannot assign values to min and max before you actually have values. After line 25, but before line 26 you do have values.


Line 26. How many times does this loop iterate? 'min'? How about 'n'?


Line 32. What does 'min' have to do with 'max'?


Remark: line 24 could move to the last loop. Then the last loop would do all computations: min, max, sum and the previous loop would do only the input.
Hello lsam18,

After running the program I started with defining "min" as 100 and "max" as zero. You could still use "min = num[0]" and "max = num[0]" just put this before line 26.

I found line 23 was better written as scanf_s("%lf", &num[i]);. The "%lf", that is lower case L, would store the number as 50 and not as a scientific notation.

Lines 39 and 40 I changed to "%.2lf"

It all seemed to work after that.

Hope that helps,

Andy
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
#include <stdio.h>

int main()
{
	int n, i;
	float num[10], sum = 0.0, average;
	float min = 100.0;
	float max = 0.0;

	printf("Enter the numbers : ");
	scanf_s("%d", &n);

	while (n > 10 || n < 0)
	{
		printf("Error! number should in range of (1 to 10).\n");
		printf("Enter the number again: ");
		scanf_s("%d", &n);
	}

	for (i = 0; i < n; ++i)
	{
		printf("%d. Enter number: ", i + 1);
		scanf_s("%f", &num[i]);
		sum += num[i];
	}
	
	for (i = 0; i < n; i++)
	{
		
		if (num[i] < min)
		{
			min = num[i];
		}
		if (num[i] > max)
		{
			max = num[i];
		}
	}

	average = sum / n;
	printf("max = %.0f\n", max); 
	printf("min = %.0f\n", min);
	printf("Average = %.2f\n", average);

	system("pause");
	return 0;
}
Last edited on
@lsam18 -- perhaps try C++ ! Much more fun than C, I assure you ;P
Also you should give the user more specific instructions, like entering 10 integers, or specifying the range in advance so the user can't make a mistake. Example below!

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
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int total_numbers = 3; // Change to whatever, e.g 10
    int input;
    vector<int> arr;
    arr.reserve(total_numbers);
    cout << "Please enter "<<total_numbers<<" integers separated by spaces\n";
    
    for (int x=1; x<=total_numbers; ++x)
    {
        cin >> input;
        arr.push_back(input);
    }

    // Find min, max, sum, average
    int min = arr[0];
    int max = arr[0];
    int sum = min;
    double avg;
    
    // Assigned vars with first, so starting loop with index 1
    for (int i=1; i<arr.size(); ++i)
    {
        int& n = arr[i];
        if (n < min)
            min = n;
        if (n > max)
            max = n;
        sum += n;
    }
    cout << "\n\n";
    cout << "min: " << min << endl <<
            "max: " << max << endl <<
            "sum: " << sum << endl <<
            "avg: " << (double)sum/total_numbers << endl;
    
    return 0;
}


Running at https://repl.it/repls/WateryVillainousMicrostation
Last edited on
perhaps try C++ ! Much more fun than C, I assure you

C++ is more fun, but also has a much steeper learning curve.
For fun it's better to use Python or some other scripting language.
Learning C++ is only worth for people who want to become professional.
Topic archived. No new replies allowed.