# Thread: finding non-zero minimum in array

1. ## finding non-zero minimum in array

i have an array (d_available[i])with some zero elements and non zero elements i want to find the minimum of the non zero elements (min_available)

this is what i thought will work but still gives me 0 as minimium

Code:
```min_available=d_available[0] ;
for(i=0;i<NROWS;i++){
if (min_available>d_available[i] && d_available> 0)
min_available=d_available[i];

}```

2. Test for the minimum of zero elements available, and when you find one, add one tick onto an accumulator. Write some sort of a small "keep track" segment of code. Accumulators can use KeepTrack++ or KeepTrack = KeepTrack + 1
Code:
```#include <stdio.h>

int main ( void )
{
int KeepTrack = 0;
int Counter = 0;
for ( Counter = 0; Counter < 10; Counter ++ )
{
printf("%d\n", KeepTrack);
KeepTrack++;
}
return 0;
}```
If you provided more information..you'd have more repsonses/I'd help more.

3. well i dont need to know how many zerow i have i need to know what is the minimum value in my array which is not zero. ie if my array contains
(0,1,2,5,8,9,10,0,2,0,0,1,2) my min will be 1 as i dont want to consider the zeros thats all what i want to do

hope thats clear

4. If your first element (d_available[0]) is zero then your result will always be zero.

Are negative values allowed?

5. One solution: initialise min_available with INT_MAX (assuming it is an int).

min_available = INT_MAX; /* limits.h */

Another solution (if negative numbers are not allowed ) is using 2 loops. The first loop to get the first non-zero number in the array and the second to check for smaller numbers (continue where the first loop ended).

6. negative values not allowed in the array and the element inside the array of arr all of type double

could you please give me an example of how to use 2 loops one to get the first non-zero element and one to calculate the minimum

thanx

7. Psuedo code to find first non 0 value
Code:
`for tempindx = 0 while yourarray[tmpindx] != 0 tempindx++`
Psuedo code to find lowest value
Code:
```for othertemp = tempindx while othertmp < NUMINDICES othertemp++
if yourarray[othertemp] < yourarray[tempindx] && yourarray[othertemp] > 0
tempindx = othertemp```
That should help a bit

8. here is the full code but still give me 0 as minimum??????

Code:
```#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define NROWS 20
#define NCOLS 3
#define STATECOL 0
#define XCOL 1
#define YCOL 2

main()
{
FILE *inputdata;
int i,j, xc,yc,tc,k;
int taxi_list[NROWS][NCOLS];
int taxistates[NROWS],xtaxi[NROWS],ytaxi[NROWS];
double d_available[NROWS],d_inuse[NROWS],d_oncall[NROWS];
double min_available,min_inuse,min_oncall;
double t_available,t_inuse,t_oncall;

// customer info.

printf(" please note that u need to press enter after entering any inputs \n Enter your location as X and Y coordenate:\n");
printf("Enter your X-coordenate \n X=");
scanf("%d",&xc);
printf("Enter your Y-coordenate \n Y=");
scanf("%d",&yc);
printf("after how many mins you need the taxi ");
scanf("%d",&tc);

//open input file to read
inputdata=fopen("taxis_info.txt","r");
if (inputdata==NULL){
printf("error canot open taxis_info \n");
exit(1);
}
//read all info from input file to one array
for (i=0;i<NROWS;i++){
for(j=0;j<NCOLS;j++){
fscanf(inputdata,"%d",&taxi_list[i][j]);
}
}

//  creat 3 different arrays from the previous 20x3 array
//for taxistates,x-coord, y-coord of the taxis

for ( i = 0 ; i < NROWS ; i++ ) {
taxistates[i] = taxi_list[i][STATECOL];
xtaxi[i] = taxi_list[i][XCOL];
ytaxi[i] = taxi_list[i][YCOL];
}
//craet 3 arrays to hold distance from customer to available,inuse and oncall taxis
//initialise the three arrays with zeros
for(i=0;i<NROWS;i++){
d_available[i]=0;
d_inuse[i]=0 ;
d_oncall[i]=0  ;

}

//check taxistates f (ie available,inuse,oncall or not available taxis)
//calculate distance between customer and  each available,inuse and oncall taxi
//place each set of distances in an array

for (i=0;i<NROWS;i++){
if (taxistates[i]==1)
d_available[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2));

else if (taxistates[i]==2)
d_inuse[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2)) ;

else if (taxistates[i]==3)
d_oncall[i]=sqrt(pow(xc-xtaxi[i],2)+pow(yc-ytaxi[i],2));

}

// pick the smallest distance from d-available[i], d-inuse[i] and d-oncall[i]
//divide the smallest distance by 20 to get t1 t2 t3

for( i = 0; d_available[i] == 0 && i < NROWS; ++i ) ;
if( i == NROWS ) {
puts( "No nonzero at all" );
/* error handling */
}
printf( "First nonzero element = d_available[%d]\n", i );
for( min_available = 1; i < NROWS; ++i ) {
if( d_available[i] != 0 )
++min_available;
}
printf( "min_available: %d\n", min_available );

}```
plz ayone can help

9. Code:
```#include <stdio.h>

#define NROWS 8

int main(void)
{
int i;
int numbers[NROWS] = { 0, 0, 3, 7, 1, 0, 15, 1 };
int min;

for(i = 0; i < NROWS && numbers[i] == 0; i++); /* find first non-zero value in array */

if(i == NROWS)
{
fprintf(stderr, "No non-zero values found\n");
return -1;
}

min = numbers[i]; /* store first non-zero value */

for( ; i < NROWS; i++)
if(numbers[i] != 0 && numbers[i] < min)
min = numbers[i];

printf("min: %d\n", min);

return 0;
}```

Popular pages Recent additions