# Basic C-- Help Desperately Needed

• 02-14-2008
Basic C-- Help Desperately Needed
Hey guys, in serious need of some help with a homework assignment for my C class. I'm not asking anyone to do my homework for me, but just help me realize what I'm doing wrong, and help me figure out what I still need to do.

My assignment is here:
http://ece.arizona.edu/~ece175/assig...signment03.pdf

And here's what I have done so far--I'm working on the degrees_to_radians section, but having loads of trouble. I'm totally new to C, so I guess I'm just generally confused as to what's going on.

Code:

```#include <stdio.h> #include <math.h> double degrees_to_radians(4 doubles main() {         inp = fopen ("a3_input.txt", "r");         outp = fopen ("a3_output.txt", "w");         double w;         double x;         double y;         double z;         int a;         double b;         fscanf(inp, "%lf", &w);         fscanf(inp, "%lf", &x);         fscanf(inp, "%lf", &y);         fscanf(inp, "%lf", &z);         fscanf(inp, "%d", &a); degrees_to_radians() {         FILE *inp;         FILE *outp; #define pi 3.14159265         scanf(inp, "%lf", &w, &x, &y, &z);         b = (scanf(inp, "%lf", &w, &x, &y, &z))*pi/180;         printf("The radian equivalents of %f", b,y);         return(b); } }```
• 02-14-2008
laserlight
Do things step by step. You're basically supposed to implement three functions (degrees_to_radians, num_digits_in_base, main), so pick one and work on it.
• 02-14-2008
Umm...Ok?
• 02-14-2008
tabstop
Quote:

Originally Posted by toadkiwi
Umm...Ok?

Well, off you go then. One extra piece of advice for free: your instructor has, in numbering the steps of the program 1-5, set up an order. That order is probably not random, and you may want to proceed from 1 through 5.
• 02-14-2008
Did some work on it. It's missing a ton of stuff--but I've got a basic idea of what needs to be done. Firstly, I need to figure out how to work the input/output txt files. I need help reading from one and writing to the other, all while using the printf function to show on screen...

Code:

```#include <stdio.h> #include <math.h> double degrees_to_radians(double, double, double, double, int) double compute_distance(double, double, double, double, int) num_of_digits_in_base(double, double, double, double, int) main() {         inp = fopen ("a3_input.txt", "r");         outp = fopen ("a3_output.txt", "w");         double w;         double x;         double y;         double z;         int a;         double b;         double c;         fscanf(inp, "%lf", &w);         fscanf(inp, "%lf", &x);         fscanf(inp, "%lf", &y);         fscanf(inp, "%lf", &z);         fscanf(inp, "%d", &a); input() {         fscanf(inp, "%lf", &w, &x, &y, &z);         printf("The four values read as input are ); } degrees_to_radians() {         FILE *inp;         FILE *outp; #define pi 3.14159265         scanf(inp, "%lf", &w, &x, &y, &z);         b = (scanf(inp, "%lf", &w, &x, &y, &z))*pi/180;         printf("The radian equivalents of %f", &w, &x, &y, &z are /n/n");         return(b); } compute_distance() {         FILE *inp;         FILE *outp;         scanf(inp, "%lf", &w, &x, &y, &z);         double x1, y1, x2, y2;         c = sqrt(((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)));         printf("The distance between %lf (&w, &x) and (&y, &z) is"         return(c) } num_of_digits_in_base() {         printf("The number of digits required to represent <a> in base 2, 4, 8,         10 are <scanf()>;         printf("The smallest integer greater than or equal to <x> is <scanf()>;         printf("The largest integer smaller than or equal to <y> is <scanf()>; } }```
Any help is appreciated!
• 02-14-2008
tabstop
Basic things:
• You can/should only read from the input file once.
• You need to find "printf" in your textbook's index and read that more carefully.
• You need to get your assignment sheet out and read that more carefully too.
• Functions should only do what they say they do. For instance from "Write a function degrees_to_radians that takes a double value (x) as input, which denotes an angle in degrees. The function returns the angle in radians. The return value is also of data type double." you know that degrees_to_radians should not be doing any inputting or printing of any kind. You also should then know what the function header is going to be, how many variables you have to work with (one), and how many things you need to compute (one).
• You also need to find "function prototypes" in your textbook's index.
• "return" would be good as well.

You should, for now, send all your output to the screen. Once what you see on your screen matches what it's supposed to be, then change it to send the output to a file.
• 02-14-2008
Unfortunately I don't have the textbook. Reason being, my instructor said we didn't need it. Still my fault for not getting it, but whatever.

ARGH this is brutal for a 3rd assignment.
• 02-14-2008
tabstop
If you don't have the textbook, then GIYF. Plus, based on the lecture notes for the course (you do have notes?), you're on a unix system, so you can type "man printf" to find out everything you want to know about printf. The lecture notes appeared to have the function declarations for the functions you're supposed to write, so there's very little excuse to not have those right.

Since you already have all the pieces, I'll give you compute_distance:
Code:

```double compute_distance(double x1, double y1, double x2, double y2) {     return sqrt(((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2))); }```
• 02-14-2008