# First day on C, super confused

1. Jul 26, 2013

### TheKracken

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. Jul 26, 2013

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

3. Jul 26, 2013

### Zondrina

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;
}

4. Jul 26, 2013

### TheKracken

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;
}
/*...................*/

5. Jul 26, 2013

### D H

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

6. Jul 26, 2013

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

7. Jul 26, 2013

### TheKracken

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)

8. Jul 26, 2013

### TheKracken

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

9. Jul 26, 2013

### D H

Staff Emeritus
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 γ.

10. Jul 26, 2013

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

11. Jul 26, 2013

### Zondrina

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

12. Jul 26, 2013

### TheKracken

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;
}
/*...................*/

13. Jul 26, 2013

### Zondrina

Look at post #3. I already showed you how to correct your syntax.

14. Jul 26, 2013

### D H

Staff Emeritus
Almost. You forgot main. The OP showed the code in its entirety in post #4. There's no main.

15. Jul 26, 2013

### Zondrina

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
16. Jul 26, 2013

### 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;
}

17. Jul 26, 2013

### Turion

Last edited by a moderator: Sep 25, 2014
18. Jul 26, 2013

### Zondrina

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.

19. Jul 26, 2013

### D H

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

20. Jul 26, 2013

### Zondrina

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