# Thread: Basic C-- Help Desperately Needed

1. ## 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);
}

}```

2. 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.

3. Umm...Ok?

4. 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.

5. 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!

6. 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.

7. 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.

8. 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)));
}```

9. Yeah, I'm just gonna have to turn in what I have--out of time. Flubbed up on this one. Still though, thanks for the help, glad I registered here.

10. A few syntax notes:
• Make sure you have the same number of closing curly braces as opening ones. For example, main() isn't closed.
• Make sure you have a return value for each function, e.g. void. (int for main().)
• Make sure you close all strings and parentheses -- for example, your printf() statements.
• Make sure you have enough semicolons.

You might want t ohave a look at the sticky "A Development Process".

11. You cannot define functions inside other functions. You also cannot refer to local variables in one function in another function.

Popular pages Recent additions