#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;
}
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.
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.
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.
#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;
}
@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!
#include <iostream>
#include <vector>
usingnamespace 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;
}
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.