1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

First day on C, super confused

  1. Jul 26, 2013 #1
    I am trying to make it computer a side of a right triangle

    here is my code?

    double x1-1, y1=5, x2=4, y2=7;
    side_1, side2, distance;
    side_1 - x2 - x1;
    side_2 = y2- y1;
    distance = sqrt(side_1*side_1 + side_2*side_2);

    printff(The distance between the two points is "
    "%5.2f\n",distance);
    return 0;
    }

    My errors are apparently
    A namespace does not directly contain members such as fields or members
    and
    Type or namespace definition, or end-of-file expected



    Could anybody help me?
     
  2. jcsd
  3. Jul 26, 2013 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    That cannot be all of your code. You need a main() function. At a minimum. You need to include header files. Is this C or C++? C does not support namespaces as such. Please post all of your code.
     
  4. Jul 26, 2013 #3

    Zondrina

    User Avatar
    Homework Helper

    First let me say, use the pow(double x, double y) function when dealing with powers. It takes a double x, and raises it to a double y.

    I edited your code a tiny bit. You should really pay more attention to your syntax. I bolded the things I changed so you could see them.

    Code (Text):

        double x1[B]=[/B]1, y1=5, x2=4, y2=7;
        side_1, side2, distance;
        side_1 [B]=[/B] x2 - x1;
        side_2 = y2- y1;
        distance = sqrt([B]pow(side_1, 2) + pow(side_2, 2)[/B]);
       
        [B]printf("The distance between the two points is : %5.2f \n", distance);[/B]

        return 0;
    }
     
  5. Jul 26, 2013 #4
    This is my code in full.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ClassLibrary1
    {
    public class Class1
    {
    }
    }

    double x1-1, y1=5, x2=4, y2=7;
    side_1, side2, distance;
    side_1 - x2 - x1 side_2 = y2- y1;
    distance = sqrt(side_1*side_1 + side_2*side_2);

    printff("The distance between the two points is"
    ,distance);
    return 0;
    }
    /*...................*/
     
  6. Jul 26, 2013 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You have a number of errors. Your compiler should have given you an error message. Read it!

    One error is here: double x1-1, ...

    The thread title mentions C, so what's this namespace and class business? That isn't a part of C.

    What is that statement side_1, side_2, distance; supposed to do? That's just a start. Your code, to be blunt, is an absolute mess.
     
  7. Jul 26, 2013 #6

    Mark44

    Staff: Mentor

    If you're dealing with small powers (such as 2 or 3), it's much more efficient to write it as a product, which the OP did.
     
  8. Jul 26, 2013 #7
    This is the first time I have ever done anything with code.

    THe stuff at the top was already on there when I went to new project. I also was told that C++ compilers also do C so I have Bloodshed Dec C++ compiler (recommended by my comp sci teacher for next semester)
     
  9. Jul 26, 2013 #8
    Also, the thing is, I copied all of this out of my textbook...I expected it too work. I am just trying to learn how everthing works, I have no idea what a name space is
     
  10. Jul 26, 2013 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    In my opinion, this is bad advice. And it's not just my opinion. Take some code that uses pow(x, 2.0) to a code review in front of people who are savvy with regard to scientific programming and you'll receive *lots* of complaints about that usage. That one bit of bad code may well make your reviewers look over every bit of your code in excruciating detail.

    The C family of languages are notoriously weak when it comes to scientific computing, and this is one of those weak spots. Most C compilers doesn't know how to compute small integer powers correctly. Even the weakest Fortran compiler knows to compile x**2 as if it were written as x*x. With C, you have to help the compiler out and write x*x explicitly. Alternatively, write a macro (C) / compile-time template (C++) that will do the translation for you. Only use pow() when the power is a non-integer quantity such as the heat capacity ratio γ.
     
  11. Jul 26, 2013 #10

    Mark44

    Staff: Mentor

    The first part of the code you copied looks like C# (C Sharp) to me. It's definitely using .NET Framework namespaces. I doubt that the compilers you mentioned will know how to compile this code.

    As already noted, when you copied the code, you were a little sloppy in your typing.

    double x1-1, should be double x1 = 1,
    side_1 - x2 - x1 should be side_1 = x2 - x1;
    printff should be printf (only one f)
    Also, printf isn't called correctly.
     
  12. Jul 26, 2013 #11

    Zondrina

    User Avatar
    Homework Helper

    @ Mark & D H

    Agreed. I know the consequences of using pow() for smaller integer values. I was intending to give the OP advice on how to deal with the nth power ( What if the OP wanted the nth power? ) as I'm sure you would not want to write :

    side_1*side_1*side_1*side_1*side_1*side_1* ... *side_1 n times and perform n calculations.

    Knowledge of pow() is not a bad thing, but for small values of n it is indeed better to perform the n calculations.
     
  13. Jul 26, 2013 #12
    THis is my new code yet still doesn't work :( Sorry, first day, I can tell there is no room for error and you have to pay attention to detail. Is there anything else I am doing wrong, I keep reviewing it too the book and see nothing wrong....uggg....Could the first day of coding be an indicator if I will be good at computer science or not? :P

    double x1=1, y1=5, x2=4, y2=7;
    side_1, side_2, distance;
    side_1 = x2 - x1
    side_2 = y2- y1;
    distance = sqrt(side_1*side_1 + side_2*side_2);

    printf("The distance between the two points is"
    "%5.2f \n" ,distance);
    return 0;
    }
    /*...................*/
     
  14. Jul 26, 2013 #13

    Zondrina

    User Avatar
    Homework Helper

    Look at post #3. I already showed you how to correct your syntax.
     
  15. Jul 26, 2013 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Almost. You forgot main. The OP showed the code in its entirety in post #4. There's no main.
     
  16. Jul 26, 2013 #15

    Zondrina

    User Avatar
    Homework Helper

    I had assumed the existence of the int main() beforehand as I would presume the OP could at least do that ( if you look there's a close brace at the end of his code... and return 0; ). I found it more important to fix the actual things which would be causing an error.
     
    Last edited: Jul 26, 2013
  17. Jul 26, 2013 #16

    Mark44

    Staff: Mentor

    Clearly that's not what he intended to do, as he was calculating distance.
    Or it could be done in a loop, as below:
    Code (Text):

    double product = 1.0;
    for (i = 0; i < n; ++i)
    {
       product *= side_1;
    }
     
     
  18. Jul 26, 2013 #17
     
    Last edited by a moderator: Sep 25, 2014
  19. Jul 26, 2013 #18

    Zondrina

    User Avatar
    Homework Helper

    I know he was calculating the distance. Perhaps the reason I gave was not the most polynomial time efficient method in this particular case, but the answer was still correct.

    What I was trying to do, was give a beginner C programmer another tool for his toolkit. A way to not just calculate low powers of a number, but any power he might need later on ( without having to write a for loop every time ).

    ##O(n)## is not very efficient after all.
     
  20. Jul 26, 2013 #19

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    In the context of scientific programming with any language derived from C, no it's not correct. Suppose you had a list to sort. While bubble sort would give the right answer, it would be incorrect, ever. In scientific programming, powers of ±1/2, ±2, ±3/2, and ±3 predominate. Using pow to calculate those is akin to using bubble sort.

    Correct. An O(n) algorithm to calculate xn where n is a positive integer is exactly akin to bubble sort. Properly done, this is an O(log2 n) calculation using multiplication rather than pow. It only takes 4 multiplies to calculate x16, for example.
     
  21. Jul 26, 2013 #20

    Zondrina

    User Avatar
    Homework Helper

    How can you expect a day one C programmer to understand a second year data structures course? Of course I understand what you're saying, but the OP surely does not ( yet, given what I've seen in this thread ).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: First day on C, super confused
  1. Super Position (Replies: 4)

  2. C++ . (Replies: 19)

  3. Super position theorum (Replies: 3)

Loading...