# C programming problem - Arrays

This is a discussion on C programming problem - Arrays within the C Programming forums, part of the General Programming Boards category; Hi again, I have to create a program that should replace the smallest number in a 1-d array, on each ...

1. ## C programming problem - Arrays

Hi again,
I have to create a program that should replace the smallest number in a 1-d array, on each turn
so 5 elements
1
2
3
4
5
it should be 9999 2 3 4 5
newline
9999 9999 3 4 5
Code:
```#include <stdio.h>
#include <conio.h>
#define ROW 5
main()
{
int unsorted[ROW] ;
int i ;
int j  ;
int smallest[ROW] ;

int temp ;

printf("Enter %d numbers\n", ROW);
for(i=0;i<ROW; i++)
{
scanf("%d", &unsorted[i]) ;
}
for(i=0;i<ROW; i++)
{
smallest[i] = unsorted[i] ;
}

for(i=0;i<5;i++)
{
if(unsorted[i] < smallest[i+1])
{

for(i=0;i<ROW; i++)
{
temp = 9999 ;
unsorted[i] = temp ;

for(i=0; i<ROW; i++)
{
printf("%d\t", unsorted[i]) ;
}

}

}
}

getch() ;
}```

2. Code:
```#include <stdio.h>
#include <conio.h>
#define ROW 5
main()
{
int unsorted[ROW] ;
int i ;
int j  ;

printf("Enter %d numbers\n", ROW);
for(i=0;i<ROW; i++)
{
scanf("%d", &unsorted[i]) ;
}

for(i=0;i<ROW;i++)
{
if(unsorted[i] <= unsorted[i+1])
{
unsorted[i] = 999 ;

for(i=0;i<ROW;i++)
{

printf("%d\t", unsorted[i]) ;

}

}

}

printf("\n") ;

for(i=0;i<ROW;i++)
{
if(unsorted[i] <= unsorted[i+1])
{
unsorted[i] = 999 ;

for(i=0;i<ROW;i++)
{

printf("%d\t", unsorted[i]) ;

}

}

}

for(i=0;i<ROW;i++)
{
if(unsorted[i] <= unsorted[i+1])
{
unsorted[i] = 999 ;

for(i=0;i<ROW;i++)
{

printf("%d\t", unsorted[i]) ;

}

}

}

getch() ;
}```
Ok i fixed the problem but i need about 3 loops!! why can't i use just one loop?

3. You should be able to (I didn't look at your code). Just need two variables, one for the lowest value and the index of that lowest value. Just do a comparison for each iteration and if the current value is lower than the lowest value, set that value as the new lowest value and that index as the index of the lowest value. After that iteration, you should have the index of the lowest value and then you can say array[lowestindex]=999; or whatever.

4. I think you should go thru the array once with two variables:

int low = INT_MAX;
int index;

To use INT_MAX you need to include limits.h. Or you could just set "low" to a very high number.

Each iteration of the loop you test unsorted[i]. If is <= to low (the first time, that is guaranteed, because low is INT_MAX) you set low = unsorted[i] and set index=i.

After the loop is complete, "index" will equal the subscript index of the lowest number in the array, so you just set unsorted[index] = 999.
Actually, since you are subing in 999, I guess that should be the initial value of "low", not INT_MAX.