# Thread: Help with this array?

1. ## 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. Originally Posted by special1zed
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.

3. 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. 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.

5. 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. 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

7. That is the right answer! What is it that you want?

8. 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?