Solved

how to read from a list of numbers to float numbers

Posted on 2007-11-22
14
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

by:Naveen Kumar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You could be right, so I'd like clarification from DanFlyBoy.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:sumawuscha
Comment Utility
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
Comment Utility
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
Comment Utility
Did any of the solutions above help or not ?
0
 

Author Comment

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and reading files in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now