Decimal Values & ANSI C Code for Image File

AI Thread Summary
The discussion revolves around coding in ANSI C to read and process a structured file containing decimal values. The user seeks assistance in reading the first line, printing it, and then reading subsequent lines to sum specific values. A suggestion is made to utilize the `scanf` function for input, specifically using `scanf("%d %c %f", &a, &b, &c)` for the header line and a loop with `scanf("%f %f %f %f %f", &d, &e, &f, &g, &h)` for the data lines. Additionally, it is recommended to create separate functions for clarity, such as `readHeaderLine` and `readDataLine`, which can return values to indicate when a data set is finished or when no more data sets are available. This modular approach aims to simplify the main loop and enhance code organization.
brad sue
Messages
270
Reaction score
0
Hi, I have a problem to code this:

Image that you have the following file presenting the following decimal values:

2 A 40.0
115.00 1.02842 5.944 10.0528 -1.656
115.00 1.01911 4.678 10.0528 177.078

11 6 100.0
115.00 1.01530 2.392 2.9097 11.933
115.00 1.00338 2.849 0.2633 37.685
115.00 1.00566 2.460 1.5274 8.387
230.00 0.99738 5.209 4.3274 -2.728

How can I code in ANSI C so that the program reads the first line, prints it ( I know this part) then reads the following 5 ( which are the values for voltages) add all of them, then goes to to the second ste of five data.

After thatthe program reads the second set of three data, print them, and reads the follwoing five, add them all and so forth.

can some help me or give me some suggestions
Thank you

B
 
Technology news on Phys.org
Do you know how to use the scanf function?

For the first line you can do:

scanf("%d %c %f",&a, &b, &c) ;

Then for the the rest of the lines you have a for loop that does this:

scanf("%f %f %f %f %f",&d, &e, &f, &g, &h) ;
 
dduardo said:
Do you know how to use the scanf function?

For the first line you can do:

scanf("%d %c %f",&a, &b, &c) ;

Then for the the rest of the lines you have a for loop that does this:

scanf("%f %f %f %f %f",&d, &e, &f, &g, &h) ;

Thank you
My problem was where to start the loop.
 
brad sue said:
can some help me or give me some suggestions
My suggestion is that you write separate functions for each separate task, which helps make everything much clearer. For example, write function "readHeaderLine" to process the top line of each data set and write function "readDataLine" to read and process each data line. If these functions return a value like the number of items processed then you can make them return 0 when they come across an empty line. This way your main function will know when a data set is finished (readDataLine has returned 0) or when there is no more data sets to process (readHeaderLine has returned 0). Using these accessory functions, you will find it simple to write the main loop.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
2
Views
3K
Back
Top