Help with this array?

This is a discussion on Help with this array? within the C Programming forums, part of the General Programming Boards category; Here is the problem. I need to make an array with floating point numbers, after entering the numbers, the program ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Help with this array?

    Here is the problem. I need to make an array with floating point numbers, after entering the numbers, the program asks me to enter another number to search the array:

    Example:
    Consider the following array of floats

    2.1 1 9 -14 1.3 5.9 9 17
    [0] [1] [2] [3] [4] [5] [6] [7]
    If the number to be searched is 5.4 the function returns 1
    If the number to be searched is 9 the function returns 2

    This is what I have:
    Code:
    #include <stdio.h>
    int main (void)
    {
        float ary[8];
        int input;
        int numbers;
        int x;
        
        printf("Enter 8 floating point numbers: \n");
        for (numbers = 0; numbers < 8; numbers++)
            scanf("%f", &ary[numbers]);
        
        printf("Enter the number to be searched: \n");
        scanf("%f", &input);
        
        for (x = 0; x < 8; x++)
            {
               if (ary[x] == input)
               {
                  printf("%d", x);
               }
            }
        if (ary[x] != input)
            {
               printf("-1\n");
            }
        system ("pause");
        return 0;
    }
    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by special1zed View Post
    Here is the problem. I need to make an array with floating point numbers, after entering the numbers, the program asks me to enter another number to search the array:

    Example:
    Consider the following array of floats

    2.1 1 9 -14 1.3 5.9 9 17
    [0] [1] [2] [3] [4] [5] [6] [7]
    If the number to be searched is 5.4 the function returns –1
    If the number to be searched is 9 the function returns 2

    This is what I have:
    Code:
    #include <stdio.h>
    int main (void)
    {
        float ary[8];
        int input;
        int numbers;
        int x;
        
        printf("Enter 8 floating point numbers: \n");
        for (numbers = 0; numbers < 8; numbers++)
            scanf("%f", &ary[numbers]);
        
        printf("Enter the number to be searched: \n");
        scanf("%f", &input);
        
        for (x = 0; x < 8; x++)
            {
               if (ary[x] == input)
               {
                  printf("%d", x);
               }
            }
        if (ary[x] != input)
            {
               printf("-1\n");
            }
        system ("pause");
        return 0;
    }
    What am I doing wrong?

    First, I would like you to tell me what is the problem with your program, as written. If it's the wrong output, then what output are you getting? Nine is in the second array index position (0, 1, 2), so that return of 2 looks just fine.

    If it won't compile, then post up the compiler's errors and warnings.

    A potential problem with floats is that they can't represent every value, exactly. It's just the nature of that data type, based on the architecture of the binary system.

    So your search may need a "delta", that is, it may need a little "fudge factor", but we'll deal with that, if needed, later.

    Post up what problems you have, with what input, and what output you're getting. My computer is perpetually tied up, and I hate to stop it's calculations for things you should already know, clearly.
    Last edited by Adak; 03-15-2009 at 10:15 PM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    I'm sorry about not being clear enough. Here is the new code:
    Code:
    #include <stdio.h>
    int main (void)
    {
        float ary[8];
        float input;
        int numbers;
        int x;
        
        printf("Enter 8 floating point numbers: \n");
        for (numbers = 0; numbers < 8; numbers++)
            scanf("%f", &ary[numbers]);
        
        printf("Enter the number to be searched: \n");
        scanf("%f", &input);
        
        for (x = 0; x < 8; x++)
            {
               if (ary[x] == input)
               {
                  printf("\n%d", x);
               }
               else if (ary[x] != input)
               {
           printf("\n-1");
               }
            }
    
        system ("pause");
        return 0;
    }
    The problem is that after I run the program, it always says -1 at the end even when its not supposed to. Can you help me with this problem? Thanks

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, let me stop the current program and take a look. I'll use your original number set, since I don't want to add 8 values while troubleshooting.

    Your program works fine for me. Since the input number is a float as well as the numbers being checked, there is no "delta" problem,
    so you can forget that worry.

    I would change this line of code:

    if(ary[x] != input) //etc.

    to just

    else
    print -1;

    Because you don't care what the input does *NOT* match. A no match, is a no match, so no more logic
    is needed to make further comparisons.
    Last edited by Adak; 03-15-2009 at 10:33 PM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    I just did 8, 7, 6, 5, 4, 3, 2, 1 for my array. And then I put 5 as my search number. Thanks for your help!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    Hey I changed it, but the answer I'm getting this as the output when I put 5 as my search number:

    -1 -1 -1
    3
    -1 -1 -1 -1
    Last edited by special1zed; 03-15-2009 at 10:51 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That is the right answer! What is it that you want?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm going to guess that you want just one answer from your search, not a bunch of -1's for instance.

    Code:
    #include <stdio.h>
    int main (void)
    {
        float ary[8];
        float input;
        int numbers;
        int x;
        int match;    
    
        printf("Enter 8 floating point numbers: \n");
        for (numbers = 0; numbers < 8; numbers++)
            scanf("%f", &ary[numbers]);
        
        printf("Enter the number to be searched: \n");
        scanf("%f", &input);
        
        for (x = 0, match = 0; x < 8; x++)
            {
               if (ary[x] == input)
               {
                   match = x;
               }
               
            }
        if(match > 0)
           printf("Match found at index %d\n", x);
        else
           printf("-1. No match was found\n");
    
        system ("pause");
        return 0;
    }

    Something like the above change?

    Note that this will only give you the last index number that matches, not all of the matching indexes. Do you want all the matching indexes?
    Last edited by Adak; 03-15-2009 at 11:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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