Why Does My C Code for Calculating Triangle Side Lengths Generate Errors?

  • Thread starter TheKracken
  • Start date
  • Tags
    Confused
In summary: Once it works, they can learn the "right" way to do it.I probably should have been more specific. Given the code, and you can't just say "I copied it from my book", I personally would have rewritten the sqrt part to be side_1*side_1 + side_2*side_2. I guess I figure that, given the code, they are using an integer value for the power. I wouldn't have used pow() here. Anyways, I agree with what you're saying. I guess I just tend to give the advice that will make the code work. We all have our own styles, I guess. As a side note, I never
  • #1
TheKracken
356
7
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?
 
Physics news on Phys.org
  • #2
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
TheKracken said:
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?

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:
    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
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
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
Zondrina said:
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.
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
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
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 everything works, I have no idea what a name space is
 
  • #9
Zondrina said:
First let me say, use the pow(double x, double y) function when dealing with powers.
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
TheKracken said:
This is my code in full.

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

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
@ 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
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
TheKracken said:
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;
}
/*......*/

Look at post #3. I already showed you how to correct your syntax.
 
  • #14
Zondrina said:
Look at post #3. I already showed you how to correct your syntax.
Almost. You forgot main. The OP showed the code in its entirety in post #4. There's no main.
 
  • #15
D H said:
Almost. You forgot main. The OP showed the code in its entirety in post #4. There's no main.

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:
  • #16
Zondrina said:
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? )
Clearly that's not what he intended to do, as he was calculating distance.
Zondrina said:
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.
Or it could be done in a loop, as below:
Code:
double product = 1.0;
for (i = 0; i < n; ++i)
{
   product *= side_1;
}
Zondrina said:
Knowledge of pow() is not a bad thing, but for small values of n it is indeed better to perform the n calculations.
 
  • #17
 
Last edited by a moderator:
  • #18
Mark44 said:
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:
double product = 1.0;
for (i = 0; i < n; ++i)
{
   product *= side_1;
}

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
Zondrina said:
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.
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.

##O(n)## is not very efficient after all.
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
D H said:
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.

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

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
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
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:
#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:
  • Like
Likes 1 person
  • #25
D H said:
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.

Zondrina said:
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 ).
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.
 
  • #26
TheKracken said:
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.
Don't give up so easily. When you're first learning how to instruct a computer to do something, it takes a while to understand that computers are pretty much dumb machines that can produce several million wrong answers very quickly. If you've never done any programming before, there's a lot to learn at the beginning.

The first things you need to watch out for are syntax errors (improper use of the programming language), such as the incorrect characters you typed when you copied the code. After you've had some practice, you won't make as many syntax errors, but you'll probably have errors in your logic, or semantic errors. These are harder to find, as the compiler won't give you any indication that something is wrong, but your results will be incorrect. With more practice, you'll be in a better position to spot these types of errors.

Back in my first programming class (many years ago), I was in pretty much the same boat you're in. I would write some code (on IBM punch cards), submit my deck of cards, and come back the next day to see how my program fared. Most of the time the program had syntax errors, and I got a very wide piece of paper with a whole lot of incomprehensible gibberish on it. Like you, I was discouraged, and didn't do anything with computers for several years. By that time, things had progressed to the point that you could write your program using a text editor, and compile the program within a minute or two. The fact that I could get feedback within a much shorter time (minutes vs. hours) made it a lot easier to take.

In my current job I work for a software company as a writer who also writes programs. This is the aspect of my work that I find most interesting, and that gives me the most satisfaction. So hang in there - you might find that there's an attraction for you as well. If you get stuck, we're here.
 

What is C and why is it important in science?

C is a high-level programming language commonly used in scientific research and data analysis. It is important because it allows scientists to write efficient and complex code to process and analyze large datasets.

Why do I feel confused on my first day using C?

C is a complex language with a steep learning curve, especially for those who are new to programming. It may take some time and practice to fully understand its syntax and concepts.

How can I improve my understanding of C?

The best way to improve your understanding of C is to practice writing code, read through tutorials and documentation, and seek help from experienced programmers or online communities.

Are there any common mistakes to avoid when learning C?

Some common mistakes to avoid when learning C include forgetting to end statements with a semicolon, using incorrect data types, and not properly managing memory allocation.

Can I use C for any type of scientific research?

Yes, C can be used for a wide range of scientific research, including data analysis, simulations, and creating algorithms. Its versatility and efficiency make it a popular choice among scientists.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top