ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
the nos are stored as double... if im not wrong....
and there are some cases to consider .... if the diff between the first and the immediate second line is greater than 10000 then we have to store the range as shown first one .If the diff between 3 or more lines is greater then we have to print the range from the number to previous one like range 3 .. and if it encounters end then we have to print only range.... like range4:
I'm not convinced I should tell you the correct answer, however ....
My comments should help you understand why I've written it this way:
Code:
#include <stdio.h>
#include <stdlib.h>
#define RANGE_LIMIT 10000
/*
* Program to list ranged numbers from an input file.
*
* Comments
* 1. For each entry in v_infile, we need to check that it is not larger than the previous by more than the RANGE_LIMIT.
* 2. Output is formatted without decimal places (same as input) although it is stored in a double variable.
* 3. Input is obtained using fgets, which returns a string, so needs casting to a double.
* 4. Comments added through out the code, to make it easier to read!
*/
int main()
{
FILE *v_infile, *v_outfile;
char v_first_s[100], v_this_s[100];
double v_first, v_this, v_last;
v_infile=fopen("data","r");
if (v_infile)
{
/*
* If I managed to open the file for reading,
* grab the first line into string: v_first_s
* and check it was not an empty file
*/
if(fgets(v_first_s,sizeof(v_first_s),v_infile) == NULL)
{
printf("No data in file!\n");
fclose(v_infile);
return(3);
}
/*
* Input file was not empty so open the output file for writing!
*/
v_outfile=fopen("output","w");
if (v_outfile)
{
/*
* If I can write to the output file,
* convert the string: v_first_s to a double: v_first
* then copy the double: v_first to another double: v_last
*/
v_first = (double)atof(v_first_s);
v_last = v_first;
while (fgets(v_this_s,sizeof(v_this_s),v_infile)!=NULL)
{
/*
* For each entry in the input file,
* grab the line into string: v_this_s
* and convert it to a double: v_this
*/
v_this = (double)atof(v_this_s);
if((v_this - v_first) > RANGE_LIMIT)
{
/*
* if the value of double: v_this
* is greater than the value of double: v_first
* by more than RANGE_LIMIT
* Add a line to the output file and
* start a new range.
*/
fprintf(v_outfile, "Range: %10.0f to %10.0f\n", v_first, v_last);
v_first = v_this;
}
/*
* End of this loop change the value of double: v_last
*/
v_last = v_this;
}
/*
* Following line catches the last piece of data
* and passes it to the output file!
*/
fprintf(v_outfile, "Range: %10.0f to %10.0f\n", v_first, v_last);
/*
* Finished writing to output file, close it!
*/
fclose(v_outfile);
} else {
/*
* Could not open output file for writing!
* Print error message and close program.
*/
printf("Unable to write to output file!\n");
return(4);
}
/*
* Finished reading from input file, close it!
*/
fclose(v_infile);
} else {
/*
* Could not open input file for reading!
* Print error message and close program.
*/
printf("Unable to open input file!\n");
return(2);
}
/*
* Program completed without any errors!
*/
return(0);
}
When writing any programming language, it's important to comment your code.
You may understand it when you write it, but it may not be you that needs to modify it.
Also you may need to understand someone else's code, and it's a lot easier if it's got lot's of comments in it!
If your not on a programming course, you may want to think about going on one.
C is not the easiest language to teach yourself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.