I'm sure you can appreciate that I don't want to just do your homework for you, because you won't learn anything from that. But let me see how I can help you in a more constructive way.
Incidentally, I think C is a bad choice for teaching programming to someone who hasn't programmed in any language before. It's too low-level, too easy to make mistakes, and there's too much ancillary "boilerplate" stuff you have to learn about (
directives and the
function). Strings are not a first-class datatype. And pointers are a common stumbling block. C is essentially just a portable assembler with syntactic sugar.
Marik2 said:
Also can anyone help me find some books that will help out a complete noob in basic programming?
If you are chained to using C, these are really good resources from people who know what they're talking about:
http://www.iso-9899.info/wiki/Books
http://www.iso-9899.info/wiki/Web_resources#Getting_Started
Marik2 said:
make the triangle appear in it
I'm not sure what you mean. Is the program supposed to display the resulting triangle, using OpenGL function calls or something? It doesn't sound like you are in a position to do that yet.
Lets read through your assignment:
Design 2: give the data, algorithm, and draft of triangle graphic
It's not worded very clearly, but I guess this means hand in a print-out of your source code, the input data you will use to test it (three sets of three side lengths), the output you get from the program when you test it, and pencil sketches or MSPaint drawings of the example triangles (unless your prof actually said that the program is supposed to draw them).
Write a C program that gives instructions
[font color=green]
Code:
printf("These are some instructions.\n");
printf("These instruction continue onto a second line.\n");
[/font]
The \n is a newline. It's like pressing Enter when you're typing text. Anything printed after a \n will be on the next line. If you miss it out, everything will be printed on the same line. It also causes the output buffer to be flushed, but that's not super important right now.
Apologies if I'm going over stuff you already know.
(The f in printf stands for format, in case you wondered. The text string you pass to it is called a format string, but that's not super important right now either.)
and asks for 3 lengths of the sides of a triangle in cm.
[font color=green]
Code:
float a;
printf("Please enter the length of the first side of the triangle in centimeters:\n");
scanf("%f", & a);
[/font]
Okay, so here I've declared a variable called
, with the datatype
. That means a floating-point number, so it can have a decimal point, rather than an integer (whole number).
Then I
the instruction to tell the user what to do.
Then I use
to read in a number from the keyboard. The
tells it that it's a floating-point number, and the
tells it the address of the
variable where it is going to store the number (the address means the variable's location in memory).
Do this three times, for the three different lengths, obviously.
Include a graphic of a triangle.
Okay, so maybe it means that it should show a picture of an example triangle to the user, before he enters the side lengths. In case he isn't sure what a triangle is, I guess? Still, as this is a console application, I'm not sure how this is proposed to be accomplished. ASCII art?
Calculate the area of the triangle using Heron's formula: [small](put the formula in a comment!)[/small]
Here's that formula in C code:
[font color=green]
Code:
float s = (a + b + c) / 2;
float area = sqrt(s * (s - a) * (s - b) * (s - c));
[/font]
Make sense? See how I got from the mathematical notation to this?
And he wants you to put the formula in a comment, too, for some reason.
[font color=green]
Code:
/* this is a comment. everything between the slash-star and
the star-slash will be ignored by the compiler */
[/font]
If any of the lengths are not positive, tell the user and end.
Okay, so he wants you to do some input validation. Obviously it would not make sense if any of the sides of the triangle were zero or negative length, so if that happens we want to tell the user not to be so stupid, and quit.
[font color=green]
Code:
if (a > 0 && b > 0 && c > 0)
{
/* calculate the area and tell the user what it is */
}
else
{
/* tell the user don't be so stupid */
}
[/font]
So the top part only will be executed if a is more than zero and b is more than zero and c is more than zero. Otherwise, the bottom part will be executed.
You will need sqrt() in math.h - #include <math.h>.
Yes, since you are calling the
function, you need to give the compiler the declaration of that function, which is in the math.h header file, so put this at the top of your source code:
[font color=green]
[/font]
You might also need to link your program's executable against the math library. Your IDE might automatically do this for you, otherwise it would depend on your compiler/linker how you go about doing this.
While we're at it, since you are also using
and
, you also need stdio.h:
[font color=green]
[/font]
You will have in your code something like area_variable = sqrt(expression);
Use printf, scanf and if-else statements.
Yep, covered that already.
Turn in the source C code listing and
three outputs with the first two inputting valid lengths and the last with at lease one negative length value.
So he wants you to test your working program three times. The first two times, with valid inputs (not zero or negative numbers). The third time, with some negative inputs to demonstrate that it handles that case as specified.
Finally, I'm not sure if you've been told, you probably have, all of that code (except the
directives) has to go inside the
function:
[font color=green]
Code:
int main(void)
{
/* code goes here */
return 0;
}
[/font]
Technically, we should give the
function a couple of parameters for command-line arguments, but we can get away with not having those and it makes it easier to explain. Your prof or textbook might say that
can return
instead of
, but that is wrong.
So now you should have everything you need. You just need to put it all together in the right order.
I think depending on which C standard you're using, you have to put all local variable declarations at the beginning of the function or block where they are used. But hopefully you won't have to worry about this. It's not major anyway, just something you might run into.
If you encounter specific problems that you want help with, try and describe the problem in as much detail as you can, paste the code that's giving you the problem and any error messages. "It doesn't work" doesn't help anyone.
References for those functions:
http://www.freebsd.org/cgi/man.cgi?query=printf&sektion=3
http://www.freebsd.org/cgi/man.cgi?query=scanf&sektion=3
http://www.freebsd.org/cgi/man.cgi?query=sqrt&sektion=3
They're from the FreeBSD operating system, but those functions are part of the C standard so should be the same on all operating systems.
Good luck!
Marik2 said:
what were your experiences when you started to program?
I had a blast, because my dad has been a programmer since the 1960s, and he started teaching me elementary programming when I was about 5 or something like that. Initially with the BASIC language, then Pascal. Self-taught C and Perl in my teens. Reading other people's code is a good way to learn. By 18 I was making quite graphically advanced games with C++ and Crystal Space [http://www.crystalspace3d.org/], which I am still doing. It was/is a marathon of frustration and longing punctuated by moments of epiphany and enlightenment.
Marik2 said:
What type of programming do you feel is the easiest and vice-versa?
There is no easy programming.
Hardest for me is functional programming. I can not brain that stuff.
Marik2 said:
What type of programming do you specialize with?
C++ and Perl. Object oriented design. Games.