I wrote a program to find the minimum and the maximum values from a vector. It works fine. What I'm trying to do is show the positions of said values and it's not working quite right. When I insert 4 elements: 2 0 1 3 it says:
"The min and max are 0 and 3
The position of the min is:
01The position of the max is:
03"
What am I doing wrong? Here is the code:
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int A[10], i, j, n, min, max, C[10], k=0, D[10], l=0;
printf("Insert no. of elements in vector A\n");
scanf("%d", &n);
printf("What are the elements of vector A?\n");
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
min = max = A[0];
for (i = 0; i < n; i++)
{
if (min >= A[i])
{
min = A[i];
C[k] = i;
k++;
}
if (max <= A[i])
{
max = A[i];
D[l] = i;
l++;
}
}
printf("The min and the max are %d and %d\n", min, max);
printf("The position of the min is:\n");
for (i = 0; i < k; i++)
printf("%d", C[i]);
printf("The position of the max is:\n");
for (i = 0; i < l; i++)
printf("%d", D[i]);
getch();
}