Basic C-- Help Desperately Needed

This is a discussion on Basic C-- Help Desperately Needed within the C Programming forums, part of the General Programming Boards category; Hey guys, in serious need of some help with a homework assignment for my C class. I'm not asking anyone ...

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Umm...Ok?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by toadkiwi View Post
    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. #5
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #7
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #9
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    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. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You cannot define functions inside other functions. You also cannot refer to local variables in one function in another function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Basic Help Needed
    By pobri19 in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2008, 04:57 AM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  3. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM
  4. VB vs. BASIC
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-05-2002, 08:55 PM
  5. DJGPP help needed
    By dune911 in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2001, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21