# Array of indices

• 06-15-2004
ronenk
Array of indices
This program is taking an array of floats (A) & build out of it arrary of numbers (B) which are the priorities of the array A.
which means:
A={0.3;0.6;0.5;0.1}
B={3,1,2,4,}
what I get is only the biggest no. what is wrong with it?

Cheers!

Ronen
#include<stdio.h>
#define M 20
void func(float A[M],int n,int B[M]);

int i,j,n,max;
float A[M];
int B[M];
void main()
{
puts ("enter the size of array A from 1 to 20");
scanf ("%d", &n);
if (n>20||n<1)
puts ("number should be between 1 & 20. ");

else
{

printf ("\nenter %d positive numbers smaller than 1 for array A\n", n);
for (i=0;i<n;i++)
{
scanf ("%f", &A[i]);
if (A[i]>1||A[i]<0)
{
printf("number does not fit!");
i--;
}
}
func(A,n,B);
}
printf ("B=");
for (j=0;j<n;j++)
{
printf ("%d, ", B[j]);
}
printf ("\n");
}

void func(float A[M],int n,int B[M])
{
int i,j;
float max=A[0];
for(i=1,j=0; i<=n; j++,i++)
if(A[i]>max)
{
max=A[i];
B[j]=i;
}

}
• 06-15-2004
quzah
For starters, use [code] tags. Like so:

[code]
...Your nicely indented code goes here...
[/code]
Otherwise it will look like... well, like yours does.

When dealing with floating point numbers, only compare them to floating point numbers:
Code:

` if (A[i]>1||A[i]<0)`
Should be:
Code:

` if (A[i]>1.0 || A[i]<0.0)`
Code:

`for(i=1,j=0; i<=n; j++,i++)`
This will go out of bounds if the user inputs 20 for the array size, because it'll start at zero, then go all the way through 20, thus, it will be 21 elements, and not 20, total. Lose the = sign.

That'll get you started.

Quzah.
• 06-15-2004
ronenk
Array of indices
OK.
Fixed the comments Quzah sent me.
lets concentrate the function algorithm. It doesnt do the job assigned to it.
Why is that so?

Code:

```void func(float A[M],int n,int B[M]) {         int i,j;         float max=A[0];         for(i=1,j=0; i<n; j++,i++)                 if(A[i]>max)                 {                 max=A[i];                 B[j]=i;                 }```

thanx,

Ronen
• 06-15-2004
sand_man
try putting { } around the for block

Code:

```void func(float A[M],int n,int B[M]) {         int i,j;         float max=A[0];         for(i=1,j=0; i<n; j++,i++)     {         if(A[i]>max)                 {             max=A[i];             B[j]=i;         }     } }```
• 06-15-2004
quzah
Quote:

try putting { } around the for block
That doesn't matter, because it takes the statement immediately following it, which is an if statment, which wraps its contents in the needed { }.

Look at the example you gave. It starts with the highest value being 1, then counts up for each one incrementally lower. That is to say, the second highest has a 2, the third has a three, and so on. Your function doesn't attempt to do that. It would be working backwards.

What you need to do would be to search through the array, and find the biggest value. Put a one by it. Search through the array again, find the next biggest value, put a 2. Search ... and so on.

So, taking the above pharagraph, you need a loop that will control the number of searches. A variable to keep track of what one you're on, this can just be the incremental variable for the search loop if you want to make it easy. You need an inner loop to do a linear search on the array to find the high value. You need to store the highest found value for the inner loop. You need to store the "search lower than this" value also, or, the last high number found.

There's probably a few ways you could do this, but that'll work in a pinch.

Quzah.