Need help with a Programming Project

Recommended Videos

Marik2

Phone Poster
Nov 10, 2009
5,461
0
0
Yes I know this type of thing would go on the advice forum, but I needed more people to look at it.

Alright I need to make a C program that will calculate the area of a triangle and make the triangle appear in it. And Im having a hard time doing it since Im struggling with the course since the textbook is crap(even the professor says its shit) and Im completely new to programming, which makes it even more difficult.

Also can anyone help me find some books that will help out a complete noob in basic programming? And does anyone know if theres like a programming group on this site?




And to add some discussion value to it; what were your experiences when you started to program?

What type of programming do you feel is the easiest and vice-versa?

What type of programming do you specialize with?

And any other story that deals with programming

Discuss away.
 

oktalist

New member
Feb 16, 2009
1,603
0
0
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 (
Code:
#include
directives and the
Code:
main
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
Code:
a
, with the datatype
Code:
float
. That means a floating-point number, so it can have a decimal point, rather than an integer (whole number).

Then I
Code:
printf
the instruction to tell the user what to do.

Then I use
Code:
scanf
to read in a number from the keyboard. The
Code:
%f
tells it that it's a floating-point number, and the
Code:
& a
tells it the address of the
Code:
a
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
Code:
sqrt
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]
Code:
#include <math.h>
[/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
Code:
printf
and
Code:
scanf
, you also need stdio.h:

[font color=green]
Code:
#include &lt;stdio.h>
[/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
Code:
#include
directives) has to go inside the
Code:
main
function:

[font color=green]
Code:
int main(void)
{
/* code goes here */
 
return 0;
}
[/font]

Technically, we should give the
Code:
main
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
Code:
main
can return
Code:
void
instead of
Code:
int
, 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.
 

Marik2

Phone Poster
Nov 10, 2009
5,461
0
0
oktalist said:
Thank you for responding; youve been of great help.

Putting the formula on the code was the biggest problem I was having.

And yeah Im not sure if the program was actually suppose to draw out the triangle, Im gonna have to ask about that.

Now the small problem i have is dealing with a user putting a negative input on one of the sides of the triangle and making the program calculate that.

This is what I had before I saw your response

And yes I know I know its horrible and I know its not complete at all.(That was not all what I was going to turn in)

Still working on it