I am trying to find the smallest element of an array, I think I am doing it correctly however I am receiving 0 as my smallest element, however, I am not entering 0 for any elements of my array. I understand some things in here are done poorly but this is my whole code in order to be reproducible and fixed.
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main(){
char input[500];
printf("Enter a list of whitespace-separated real numbers terminated by EOF or 'end'.");
puts("");
printf("-----------------------------------------------------------------------------");
puts("");
gets(input);
int size = strlen(input);
int elements[size];
int i = 0;
char *p = strtok(input," ");
while( p != NULL)
{
elements[i++] = strtol(p, NULL, 10);
p = strtok(NULL," ");
}
//NUM OF ELEMENTS
int numOfElements = 0;
for(int j = 0; j < i; j++){
elements[j] = numOfElements++;
}
//MIN ELEMENT
int min = INT_MAX;
for(int k = 0; k < i; k++){
if(elements[k] < min){
min = elements[k];
}
}
printf("-----------------------------------------------------------------------------\n");
printf("# of Elements: %d\n", numOfElements);
printf("Minimum: %d\n", min);
return 0;
}
RESULT:
Enter a list of whitespace-separated numbers.
-----------------------------------------------------------------------------
1 2 3 4 5
-----------------------------------------------------------------------------
# of Elements: 5
Minimum: 0
EXPECTED:
Enter a list of whitespace-separated numbers.
-----------------------------------------------------------------------------
1 2 3 4 5
-----------------------------------------------------------------------------
# of Elements: 5
Minimum: 1