# Thread: Creating a table and I/O file help?

1. ## Creating a table and I/O file help?

I'm a bit confused by a problem on some homework for my programming test. I'm not really understanding what the question is asking though, or how to apply it to the text file I'm supposed to be outputting from. I emailed my professor for clarification but I was hoping for some forum help?

There is a file, radius.txt, containing two numbers. The first number is a start point; for the sake of argument, let’s say it’s 0.5. The second number is the number of iterations that a table must contain; let’s agree to call this 5 for now. Now, assume the first number is a radius with which you must find the circumference and area of a circle as well as the volume of a sphere.

Create a table that does this for all iterations described in the file, assuming a user-defined iterative step. Do not assume pi to be previously defined, but do assume that it is a constant defined at 3.14159265. Use proper data types and restrict calculated output to 8 decimal places but radii to 4. Print output to a separate file, circles.txt, as well as the screen.

Sample
Input file: 0.5 5

User input: Input a step size: 0.6

Output:
radius circumf area volume
0.5000 3.14159265 0.78539816 0.52359879

1.1000 6.91150398 3.80132727 5.57528028

1.7000 10.68141531 9.07920327 20.57952860

2.3000 14.45132739 16.61902787 50.96502456

2.9000 18.22123797 26.42079592 102.16041731

I created a file radius.txt and inputted the .5 and 5

Code:
```#include<stdio.h>
#include<math.h>

int main(void)
{
FILE *fin *fout;

fout=fopen("circles.txt");

fscanf(fin, "%f %f", &f1,&f2);
printf("Input: %f %f" rad,iter);
fprintf(fout, "input was %f %f", rad, iter);

fclose(fin);
Fclose(fout);

return 0;
}```
I tried to use the integers i typed into the file, but i wasnt sure how to manipulate them to act as regular variable for all of my calculations. I'm very confused I would really appreciate the help.

2. I also have the following, but I'm not sure how useful it would be or how to relate it to the previous post
Code:
```	#include<stdio.h>#include<math.h>
#define PI 3.14159265

int main(){

float r,c,area,volume;

printf("Enter radius of the sphere: ");     /*from file?*/
scanf("%f",&r);

c =  PI * r * 2;
area = PI * r * r;
volume = (4.0/3) * PI * r * r * r;

printf("\nCircumference: %f",c);
printf("\nArea: %f",area);
printf("\nVolume: %f",volume);

return 0;```

3. I would change the f1 variable to something a lot more descriptive, like "radius" maybe, and f2, same thing. Something like "steps2take" or etc. Finally, "stepSize" for the user's input on the size of each step.

Heaven save us from variables named f1, f2, etc.!

Code:
```//make your for loop:

for(i=0;i<steps2take; i++) {
//use your equations here, then
//write and print out the data here

radius+=stepSize;  //get ready for the next step
}```
If you can ask more specific questions, we can be more helpful. I'm not entirely sure what you want here.

4. I'm not sure what questions to ask... firstly, how do I get it to have an output table like that one (in the program) and also, why isn't it outputting to a txt file like I programmed it to do? My program has errors and I'm not sure how to correct them, I just want to run what I already have and see how well it can be applied to my professors problem.

As of now my code is:
Code:
```#include<stdio.h>#include<math.h>
#define PI 3.14159265

int main(void)
{
/*Creat file pointers*/
FILE *fin *fout;
float r, c, area, volume, stepsize;
float steps2take;

/*Open radius.txt for reading and circles.txt for writing*/
fout=fopen("circles.txt");

/*Read values from radius.txt and store in r and steps2take*/
fscanf(fin, "%f %f", &r,&steps2take);
printf("Input: %f %f" r,steps2take);

printf("Input a step size");
scanf("%f", &stepsize);

for(i=0;i<steps2take; i++) {
c =  PI * r * 2;
area = PI * r * r;
volume = (4.0/3) * PI * r * r * r;

/*Print values to screen*/
printf("\nCircumference: %f",c);
printf("\nArea: %f",area);
printf("\nVolume: %f",volume);

/*Print values to circles.txt*/
fprintf(fout,"\nCircumference: %f",c);
fprintf(fout,"\nArea: %f",area);
fprintf(fout"\nVolume: %f",volume);

r+=stepSize;
}
/*close loops*/
fclose(fin);
Fclose(fout);

}```

5. and in the for loop i changed "i" to "r" for the radius

6. Originally Posted by dKarayof
and in the for loop i changed "i" to "r" for the radius
*fout requires a "w" for it's output mode - currently has none.

*The header row of text has to be printed before the for loop.
radius circumf area volume

*the increment for r in each loop has to be the stepSize, it can't be r++.

*Fclose() must be changed to fclose()

*inside the for loop, with each time around the loop, you will have to have the data printed out to the display, and to the file.

*the for loop requires a closing curly brace at the end of it: }

*the for loop requires indentation that matches the rest of your program.

*You'll use printf's format features to make your table up. Don't worry about that much, for now. That is a detail easily handled, later.

DO NOT post code with a poor indentation style. Those kinds of programs are VERY difficult to study, for you, me, and everybody else. Your brain will be slowly training itself to recognize common C idioms if you use proper indentation. Before long, you'll be able to pick out some errors, with just a quick read over. Also, code that looks like something the cat killed and dragged in, is bound to get less help here, and a lower grade from your teacher.

Those errors from the compiler are important, although they are not easy to understand.

7. I fixed some of the problems you described... I feel like my for loop looks exactly how it should I'm not sure why it isn't written to repeat however many times the user specified... isnt that accomplished in the initial for(,,?

Is my fprintf format not good to print the code to a file after the loop is run?

I tried to fix my indentations as best as possible.. I appreciate the help a lot

8. Code:
```#include<stdio.h>#include<math.h>
#define PI 3.14159265

int main(void)
{
/*Creat file pointers*/
FILE *fin *fout;
float r, c, area, volume, stepsize;
int steps2take;

/*Open radius.txt for reading and circles.txt for writing*/
fout=fopen("circles.txt","w");

/*Read values from radius.txt and store in r and steps2take*/
fscanf(fin, "%f %d", &r,&steps2take);
printf("Input: %f %d" r,steps2take);

/*Prompt user to input step size*/
printf("Input a step size");
scanf("%f", &stepsize);

printf("\nCircumference: %f",c);
printf("\nArea: %f",area);
printf("\nVolume: %f",volume);

/*Set initial value as 0, NEEEEED MORE HERE*/
for(r=0;r<steps2take; stepsize)
{
c =  PI * r * 2;
area = PI * r * r;
volume = (4.0/3) * PI * r * r * r;

/*Print values to screen*/
printf("%f",r);
printf("\n%f",c);
printf("\n%f",area);
printf("\n%f",volume);

/*Print values to circles.txt*/
fprintf(fout,"\nCircumference: %f",c);
fprintf(fout,"\nArea: %f",area);
fprintf(fout"\nVolume: %f",volume);

r+=stepSize;
}
return 0;

/*close loops*/
fclose(fin);
fclose(fout);

}```
updated version

9. Do you know how to turn on the compiler warnings?

Code:
```\$ gcc -Wall -Wextra -ggdb3 -lm -o foo foo.c
foo.c: In function ‘main’:
foo.c:9:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
foo.c:9:16: error: ‘fout’ undeclared (first use in this function)
foo.c:9:16: note: each undeclared identifier is reported only once for each function it appears in
foo.c:15:5: error: ‘fin’ undeclared (first use in this function)
foo.c:21:27: error: expected ‘)’ before ‘r’
foo.c:21:27: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat]
foo.c:21:27: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat]
foo.c:36:13: warning: statement with no effect [-Wunused-value]
foo.c:54:17: error: expected ‘)’ before string constant
foo.c:54:17: error: too few arguments to function ‘fprintf’
foo.c:57:8: error: ‘stepSize’ undeclared (first use in this function)```
Your are missing commas in several function calls.

Bye, Andreas

10. FILE *fin, *fout;

Watch those commas!

Popular pages Recent additions