Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# how to read from a list of numbers to float numbers

Posted on 2007-11-22
Medium Priority
244 Views
I have a list of int numbers and a corresponding list of float numbers.  Example,

12.50       15
14.23       22
15.12       43

When I receive data over thenetwork I will receive a 15 or 22 number, when I recive this number
how can I print the coresponding float number?

example:   receive 15 wish to print 12.50
0
Question by:DanFlyBoy
• 5
• 4
• 2
• +3

LVL 85

Expert Comment

ID: 20337209
If there is a maximum int value, you could put the floats in an array indexed by the int
0

LVL 5

Expert Comment

ID: 20337611
if what i understood is correct,

then this is as simple as taking data value from array.

have a array with maximum possible number size.
say if you may receive value 500 as maximum
have array[500]
when you want to return value for n (15 or 22..) just return array[n].

or
if integer number can be very high but the  count is limited. then have two a struct (with int,float) array .

store all the int and float values in each index. then when you retirve just iterate through struct array till match with int value. once reached return associated float value

0

LVL 53

Expert Comment

ID: 20337651
I don't think there needs to be an array for this ... Simply receive the input one line at a time, tokenize the line so it gives you the two values, and depending on the int value, decide whether to print it or not.

Take a look at sscanf (http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html) to extract the two values from a string.
0

LVL 28

Expert Comment

ID: 20337882
if you can store the list of ints and floats in arrays then :

you should have something like ( both the integer array and float array sizes will be same in this case )

for (a=0;a<=max_size;a++)
{
if ( received_value = int_arr[a] )
printf("\n%f",float_arr[a]);
}
}

Thanks
0

LVL 53

Expert Comment

ID: 20337887
Why is everybody talking about arrays ? Did I miss something ? How do you receive the data, DanFlyBoy ?
0

LVL 28

Expert Comment

ID: 20337920
Infinity08:

i presume that way because asker said
"I have a list of int numbers and a corresponding list of float numbers"

In C, in simple terms he should have arrays to hold lists and if not, otherwise left with files. I took the simple case. May be i am wrong here :) and you are right here :)
0

LVL 53

Expert Comment

ID: 20337942
You could be right, so I'd like clarification from DanFlyBoy.
0

Expert Comment

ID: 20344462
If i understand you right:
Maybe you store your list in a text file. Then you walk through the file until you find the corresponding number.

See sample code (only as an idea, no working code):
``````
int intNum;
float floatNum;

FILE *fh = fopen("path/to/file", "a");

if (fh == NULL)
perror("oops");

while (fscanf(fh, "%f\t%u", &floatNum, &intNum) > 0) {
if (intNum == recvNum) {
printf("%f\n", floatNum;
break;
}
}
fclose(fh);
``````
0

Author Comment

ID: 20348033
T am reading a packet of data over the network, I will receive an int number, based upon the number I receive,  I wish to dis[lay a float value.
0

LVL 28

Expert Comment

ID: 20348068
Did any of the solutions above help or not ?
0

Author Comment

ID: 20349975
I was thinking of creating a structure and have a pointer get the float value.

In this example,
for (a=0;a<=max_size;a++)
{
if ( received_value = int_arr[a] )
printf("\n%f",float_arr[a]);
}
}
how would I corspond the correct float value to the int value?
0

LVL 53

Expert Comment

ID: 20350071
>> how would I corspond the correct float value to the int value?

Have an array of structs :

typedef struct Elem {
int id;
float value;
} Elem;

Elem list[10];

where list is an array of 10 Elem structs, each of which contains an int and a float.

Do you know how to work with structs and arrays ?
0

LVL 28

Accepted Solution

Naveen Kumar earned 750 total points
ID: 20354488
in my last update, i have already said "both the integer array and float array sizes will be same in this case ". Just have two arrays with same size and put whatever values you want into them and search for interger value in the interger array and if found print that indexed element from the float array.

Thanks
0

LVL 53

Expert Comment

ID: 20755937
If I were you, I would really consider grouping data that belongs together in a struct like I suggested in my last post. It's cleaner, and simpler to use.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month10 days, 16 hours left to enroll