Array of indices

This is a discussion on Array of indices within the C Programming forums, part of the General Programming Boards category; This program is taking an array of floats (A) & build out of it arrary of numbers (B) which are ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    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;
    }

    }
    Last edited by ronenk; 06-15-2004 at 02:31 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    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

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    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;
            }
        }
    
    }
    Last edited by sand_man; 06-15-2004 at 12:33 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21