Reputation: 13
Build a program which reads from the user an array with n elements and finds the element with the smallest value.Then the program finds the number of the elements which have an equal value with this minimum.The found element with the smallest value along with the number of the elements which have an equal value with the minimum of the array should be displayed on screen..
I wrote this code :
#include <stdio.h>
int main() {
int n = 1, min = 0, count = 0;
int number[n];
printf("Enter the size of array you want");
scanf("%i", &n);
int x;
for (x = 0; x < n; x++) {
int num;
printf("\nEnter a Integer");
scanf("%i", &num);
number[x] = num;
if (number[x] < min)
min = number[x];
}
int i;
for (i = 0; i < n; i++) {
if (min = number[i])
count++;
}
printf("%s%i", "\nThe smallest Integer you entered was ", min);
printf("%s%i", "\nNumber of times you entered this Integer: ", count);
return 0;
}
But the problem is that when I run this,and I add the integers,it doesnt find the smallest value and how time its repeated correctly!
Where am I wrong?
Upvotes: 0
Views: 193
Reputation: 629
1.Only After the user input the array size, then you can determine the size of the array number.As the size of the array are uncertain, you should use malloc to allocate the array dynamically.
2.you should set min to the first element of the array. As the min of user input may be great than zero, if you set min to zero, then it will return zero even though the minimum is great than zero
3.you should use == but not = to test the equality of two numbers.
4.last, you should use free to make the memory available and avoid memory leak.
Below is the full program:
#include <stdio.h>
int main() {
int n = 1, min = 0, count = 0;
int* number;
printf("Enter the size of array you want");
scanf("%i", &n);
number = (int*)malloc(sizeof(int)*n);
int x;
for (x = 0; x < n; x++) {
int num;
printf("\nEnter a Integer");
scanf("%i", &num);
number[x] = num;
if( x == 0 || number[x] < min )
min = number[x];
}
int i;
for (i = 0; i < n; i++) {
if (min == number[i])
count++;
}
printf("%s%i", "\nThe smallest Integer you entered was ", min);
printf("%s%i", "\nNumber of times you entered this Integer: ", count);
free(number);
number = NULL;
return 0;
}
Upvotes: 1
Reputation: 882
you are checking array element <0 in line:
if (number[x] < min/*as u specified min =0 before*/),...
so the minimum is set to be zero and there is no replacement actually happening..
The full solution:
#include <stdio.h>
int main() {
int n = 1, min = 0, count = 0;
int number[n];
printf("Enter the size of array you want");
scanf("%i", &n);
int x,y;
for (y = 0; y < n; y++)
{
printf("\nEnter a Integer");
scanf("%i", &number[y]);
}
min=number[0];
for (x = 0; x < n; x++) {
if (number[x] < min)
min = number[x];
}
int i;
for (i = 0; i < n; i++) {
if (min == number[i])
count++;
}
printf("%s%i", "\nThe smallest Integer you entered was ", min);
printf("%s%i", "\nNumber of times you entered this Integer: ", count);
return 0;
}
Upvotes: 1
Reputation: 532
There are couple of things wrong on your code. First you defined array number[1]
. Secondly min
in initialised to min = 0
.
I suggest defined the array for a maximum possible size, like number[100]
. And read the numbe of inputs n
from the user, and only use the first n
elements of the array. For the second issue, define min as the maximum number represented by int
type.
Upvotes: 0
Reputation: 42205
Assuming your compiler has support for variable length arrays, you need to re-order the calls
int number[n];
scanf("%i", &n);
so that you know the value for n
before declaring the array
scanf("%i", &n);
int number[n];
After that, you should initialise min
to a larger value to avoid ignoring all positive values
int min = INT_MAX;
(You'll need to include <limits.h>
for the definition of INT_MAX
)
Finally,
if (min = number[i])
assigns number[i]
to min
. Use ==
to test for equality.
Your compiler should have warned you about "assignment in conditional statement" for this last point. If it didn't, make sure you have enabled warnings (-Wall
with gcc, /W4
with MSVC)
Upvotes: 4
Reputation: 3393
In your code
if (min = number[i])
you assigned number[i] to min. You should write
if (min == number[i])
instead.
Upvotes: 0
Reputation: 6969
for (i = 0; i < n; i++) {
if (min = number[i])
count++;
}
Replace min = number[i]
with min == number[i]
.
Upvotes: 1