Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to read from a list of numbers to float numbers

Posted on 2007-11-22
14
238 Views
Last Modified: 2010-04-15
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
Comment
Question by:DanFlyBoy
  • 5
  • 4
  • 2
  • +3
14 Comments
 
LVL 84

Expert Comment

by:ozo
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

by:abith
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

by:Infinity08
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 28

Expert Comment

by:Naveen Kumar
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

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

Expert Comment

by:Naveen Kumar
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

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

Expert Comment

by:sumawuscha
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);

Open in new window

0
 

Author Comment

by:DanFlyBoy
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

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

Author Comment

by:DanFlyBoy
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

by:Infinity08
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

by:
Naveen Kumar earned 250 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

by:Infinity08
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question