Solved

how to read from a list of numbers to float numbers

Posted on 2007-11-22
14
239 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 189
C dll call freezes 5 112
Finding a good hash function 4 130
Handling string inputs in C/Linux 23 195
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

733 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