# Homework Help: 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 ).

21. Jul 26, 2013

### D H

Staff Emeritus
Given what we've seen in this thread, even pow is off-topic.

I don't even know how to start helping the OP other than to suggest he or she talk to the instructor or teaching assistant. The misunderstandings are too deep for the kind of help that can be offered in a forum setting. This needs a face-to-face kind of meeting.

22. Jul 26, 2013

### Zondrina

I agree, but that almost sounds like giving up (in a way) on our part.

The instructor would probably be able to explain it best based on the curriculum and demonstrate it using actual examples and concepts.

23. Jul 26, 2013

### TheKracken

I appreciate the extra help everyone. As to using the pow for doing what I did, thank you. I did not know about that. As for what is more efficient, I would have no idea, and have no idea how it makes an actual difference but I hope one day to understand that. Also, I fixed the syntax error regarding to 2 f's in print f, though I kept my way of calculating the distance. I don't see what is wrong since I got it right out of a book and it is for sure now word for word and letter for letter. I wanted to get it working and then experiment with it to see how it works but it is sad that I can't even get code the I copied to work. I was really liking the idea of computer science.

24. Jul 26, 2013

### rcgldr

To get you started without going through several more iterations of redo's and feedback cycles, only to end up with what is shown below, here is your code fixed so that it will work. Compare the differences to see what your source code problems were. Other than missing main, most of your problems were typos, and you don't need stuff like "namespace" or "using" for a simple C program like this. Be sure the name the source code file with a .c suffix such as

example.c

and not example.cpp.

Code (Text):

#include <stdio.h>
#include <math.h>

int main()
{
double x1=1.0, y1=5.0, x2=4.0, y2=7.0;
double 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;
}

Last edited: Jul 27, 2013
25. Jul 27, 2013

### Staff: Mentor

I'm pretty sure that D H's remarks were addressed to you, not to the OP. For the same reason that a day one C programmer is not expected to comprehend data structures and algorithm analysis, your advice to use pow to calculate the squares of a couple of numbers was not helpful, in my opinion, given that the OP already had an expression that would do the job. When someone is struggling to understand how to use a chisel, it's not the time to suggest that a six-axis CNC milling machine is a useful addition to his box of tools.