Write a C program to read values for times from the input file?

In summary: But please don't ask for someone to write your code for you. It's important to learn and understand the material, not just get a quick solution. In summary, the conversation discusses the task of writing a C program to read values from an input file, compute the sine of 5*pi*t for each time given in the file, and output the results into a separate file. The conversation also mentions using a makefile and includes a pseudo code outline for the program. The expert reminds the individual to put in effort and not rely on others to write the code for them.
  • #1
sheldonrocks97
Gold Member
66
2

Homework Statement


Write a C program to read values for times from the input file times.txt; compute sin(5*pi*t) for each time t given in times.txt; output comma separated values of t, sin(5*pi*t) into an output file named output.csv; and compile the program using a makefile. Hand in a copy of your program, output file, and makefile.

LIST OF TIMES:

0
0.0121
0.0241
0.0362
0.0483
0.0603
0.0724
0.0845
0.0965
0.1086
0.1207
0.1327
0.1448
0.1569
0.1689
0.1810
0.1931
0.2051
0.2172
0.2293
0.2413
0.2534
0.2655
0.2775
0.2896
0.3017
0.3137
0.3258
0.3379
0.3499
0.3620
0.3741
0.3861
0.3982
0.4103
0.4223
0.4344
0.4465
0.4585
0.4706
0.4827
0.4947
0.5068
0.5189
0.5309
0.5430
0.5551
0.5671
0.5792
0.5913

Homework Equations

The Attempt at a Solution


#include<stdio.h>

#include<math.h>

void main (void)

{

FILE *times.txt, *output.csv; //input/output files//

int t; //time variable//

int x = sin(5*M_PI*t);Please help my friend and I are very very desperate!
 
Physics news on Phys.org
  • #2
I'm sorry to say that while many of us could easily write this program, we can't in good conscience do it. This is your homework assignment you and your friend need to mount a valiant defense, read your book and just get down and do it. It should take no more than an hour or so to write.

I can show you some pseudo code to follow that may help you get started. Also you can easily find program recipes that read files, convert strings to numbers, compute values and print them out which are the basic things your program needs to do.

Some pseudo code:

Code:
Open file
For each line in the file
{
        Read one number
        Compute sin()
        Print t, comma, sin()
}
Close file

So follow each line and look up in your book for things that match what the line does.
 
  • #3
Thank you. My friend and I got the help we needed.
 
  • Like
Likes jedishrfu
  • #4
That's great if you run into specific questions or problems with your code, post it and we can provide some help.
 
  • #5


Hello, thank you for sharing your code and assignment prompt with me. I understand the importance of efficient and accurate coding in scientific research. Here is my response to your content:

Firstly, I would like to commend you for providing a clear and detailed assignment prompt. It is important to have a thorough understanding of the task at hand before attempting to write a program.

In terms of your code, there are a few things that need to be addressed. Firstly, I would suggest using a proper main function with a return type of int. This will ensure that your program returns a value when it is executed. Additionally, you should declare your variables at the beginning of the function, before any other statements.

Next, you have declared your input and output files, but you have not used them in your program. To read values from the input file, you can use the fscanf function. This function takes in the file pointer, the format of the data in the file, and the variable where the data will be stored. For example, for your time values, you can use the format specifier "%lf" to read in decimal numbers and store them in a double variable.

Once you have read in the time values, you can then use a loop to compute the value of sin(5*pi*t) for each time value. You can use the sin function from the math library, but make sure to include the math library at the beginning of your program using the #include directive.

Finally, to output the comma-separated values into the output file, you can use the fprintf function. This function takes in the file pointer, the format of the data, and the variables to be written to the file. For example, you can use "%lf, %lf" as the format specifier to output the time and sin value, respectively.

I would also suggest adding error handling to your program to ensure that the input and output files are successfully opened and that the values are read and written correctly.

In terms of the makefile, it should include instructions for compiling your program and any necessary dependencies. It is important to test your makefile to ensure that it is properly compiling your program.

Overall, your attempt at a solution shows that you have a good understanding of the task at hand. I hope my suggestions will help you in completing your assignment successfully. Best of luck!
 

1. How can I read values for times from an input file in a C program?

In order to read values for times from an input file in a C program, you can use the fscanf() function. This function takes in the file pointer, format specifier, and the address of the variable where you want to store the input value.

2. What is the syntax for using fscanf() to read values for times?

The syntax for using fscanf() to read values for times is fscanf(file_pointer, "%d:%d:%d", &hours, &minutes, &seconds); where file_pointer is the pointer to the input file, and hours, minutes, and seconds are variables to store the input values. The format specifier %d:%d:%d is used to read the hours, minutes, and seconds in the format of HH:MM:SS.

3. Can I use scanf() instead of fscanf() to read values for times from an input file?

No, scanf() is used to read input values from the standard input, not from an input file. To read values for times from an input file, you must use fscanf() and pass in the file pointer as an argument.

4. How do I handle errors while reading values for times from an input file in a C program?

You can check the return value of fscanf() to determine if there was an error while reading from the input file. If the return value is less than the number of format specifiers used, it indicates an error. You can also use the feof() function to check if the end of the file has been reached.

5. Is there a limit to the number of values for times that can be read from an input file in a C program?

The limit for the number of values that can be read from an input file in a C program depends on the size of the file and the amount of memory available. However, it is good practice to check for the end of file while using fscanf() to avoid any potential issues with large files.

Back
Top