## digitalmars.D.learn - 111

• Lisa (2/2) Feb 19 2016 Can you please help me and explain how to create a program, which
• cym13 (3/5) Feb 19 2016 What do you need help for exactly? What have you tried? What do
• Lisa (3/8) Feb 19 2016 With everything, actually i need full explanation and listing of
• Chris Wright (2/12) Feb 19 2016 Is this a homework problem that was assigned to you?
• Lisa (3/15) Feb 19 2016 Yes, exectly. It's new for me, and i don't even know how should I
• Chris Wright (16/33) Feb 19 2016 Break the problem down into parts.
• Adam D. Ruppe (9/11) Feb 19 2016 Getting the height can be tricky sometimes though, depending on
• cym13 (14/23) Feb 19 2016 I don't think you'll ever get the program listing here, it's just
• Ivan Kazmenko (9/11) Feb 19 2016 First, one can't find these unless something is given. So, what
• =?UTF-8?Q?Ali_=c3=87ehreli?= (9/11) Feb 19 2016 It's great to have student questions on this forum. If you don't mind
• Lisa (3/16) Feb 19 2016 Thank you :)
• Lisa (17/30) Feb 19 2016 import std.stdio;
• Lisa (4/6) Feb 19 2016 And for everybody - I know how to find area and perimetr, but i
Lisa <dark_nessi mail.ru> writes:
```Can you please help me and explain how to create a program, which
would find area of triangle and its perimeter?
```
Feb 19 2016
cym13 <cpicard openmailbox.org> writes:
```On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

What do you need help for exactly? What have you tried? What do
you struggle with?
```
Feb 19 2016
Lisa <dark_nessi mail.ru> writes:
```On Saturday, 20 February 2016 at 00:01:47 UTC, cym13 wrote:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

What do you need help for exactly? What have you tried? What do
you struggle with?

With everything, actually i need full explanation and listing of
programm from its beginning
```
Feb 19 2016
Chris Wright <dhasenan gmail.com> writes:
```On Sat, 20 Feb 2016 00:04:04 +0000, Lisa wrote:

On Saturday, 20 February 2016 at 00:01:47 UTC, cym13 wrote:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

What do you need help for exactly? What have you tried? What do you
struggle with?

With everything, actually i need full explanation and listing of
programm from its beginning

Is this a homework problem that was assigned to you?
```
Feb 19 2016
Lisa <dark_nessi mail.ru> writes:
```On Saturday, 20 February 2016 at 00:15:16 UTC, Chris Wright wrote:
On Sat, 20 Feb 2016 00:04:04 +0000, Lisa wrote:

On Saturday, 20 February 2016 at 00:01:47 UTC, cym13 wrote:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

What do you need help for exactly? What have you tried? What
do you struggle with?

With everything, actually i need full explanation and listing
of programm from its beginning

Is this a homework problem that was assigned to you?

Yes, exectly. It's new for me, and i don't even know how should I
start.
```
Feb 19 2016
Chris Wright <dhasenan gmail.com> writes:
```On Sat, 20 Feb 2016 00:17:28 +0000, Lisa wrote:

On Saturday, 20 February 2016 at 00:15:16 UTC, Chris Wright wrote:
On Sat, 20 Feb 2016 00:04:04 +0000, Lisa wrote:

On Saturday, 20 February 2016 at 00:01:47 UTC, cym13 wrote:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

What do you need help for exactly? What have you tried? What do you
struggle with?

With everything, actually i need full explanation and listing of
programm from its beginning

Is this a homework problem that was assigned to you?

Yes, exectly. It's new for me, and i don't even know how should I start.

Break the problem down into parts.

First you need to get a triangle. Then you need to calculate the
dimensions.

How do you get the triangle? The homework problem should tell you.

How do you calculate the area of a triangle? Same way as you'd do it by
hand -- 1/2 base times height. Break that down further and further and
you'll have that part squared away before long.

How do you calculate the perimeter of a triangle? Same way as you'd do by
hand -- sum of distances between corners. Again, break it into parts and
you'll have a program before long.

Programmers tend to be reluctant to do other people's homework for them.
(People in general are reluctant to do other people's homework for them.)
week and you'd be pretty much bound to fail. Or you'd have a lot more
work to catch up and a lot less time in which to do it.
```
Feb 19 2016
Adam D. Ruppe <destructionator gmail.com> writes:
```On Saturday, 20 February 2016 at 00:42:36 UTC, Chris Wright wrote:
How do you calculate the area of a triangle? Same way as you'd
do it by hand -- 1/2 base times height.

Getting the height can be tricky sometimes though, depending on
what information you know about the triangle.

I like to check wikipedia for refreshers on math tricks.

https://en.wikipedia.org/wiki/Triangle#Computing_the_area_of_a_triangle

There's formulas to follow for almost any kind of input there...
and if you have to get perimeter anyway, you are already
calculating the lengths of the sides, so I'd use the convenient
formula in there based on those very lengths!
```
Feb 19 2016
cym13 <cpicard openmailbox.org> writes:
```On Saturday, 20 February 2016 at 00:04:04 UTC, Lisa wrote:
On Saturday, 20 February 2016 at 00:01:47 UTC, cym13 wrote:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

What do you need help for exactly? What have you tried? What
do you struggle with?

With everything, actually i need full explanation and listing
of programm from its beginning

I don't think you'll ever get the program listing here, it's just
not how it works. We can help you, guide you to the solution but
you are the one that has to do it otherwise it defeats the whole
purpose of the exercise.

First of all, programming is explaining a solution to a really
dumb student (the computer). If you can't do it by hand, you
can't explain it to the computer. Once you can do it by hand,
decompose each step into steps as simple as possible and write
them down on paper. Then it's just a matter of translating your
pseudo-code on paper to real code: if it's an exercise I'm sure
you have had courses and examples. Take inspiration, try things
and don't hesitate to fail: nobody expects you to get it right on
the first time.
```
Feb 19 2016
Ivan Kazmenko <gassa mail.ru> writes:
```On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
which would find area of triangle and its perimeter?

First, one can't find these unless something is given.  So, what
is given: sides? angles? two-dimensional coordinates?

The next stop is Google for how to do that mathematically,
without touching the keyboard.

Once you have the above, you may have some specific difficulty
expressing that in a programming language of your choice, one
which Google (again) can't resolve in a few minutes.  If that is
the case, please state that difficulty.
```
Feb 19 2016
=?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
```On 02/19/2016 03:56 PM, Lisa wrote:
find area of triangle and its perimeter?

It's great to have student questions on this forum. If you don't mind
telling us, which teacher and school teaches or uses D? The only one
that I know of is Professor Chuck Allison at Utah Valley University.

Assuming that you are given the lengths of the three sides, you can
calculate the area with Heron's formula. I had used that method in this
otherwise unrelated chapter:

http://ddili.org/ders/d.en/invariant.html

Ali
```
Feb 19 2016
Lisa <dark_nessi mail.ru> writes:
```On Saturday, 20 February 2016 at 01:48:35 UTC, Ali Çehreli wrote:
On 02/19/2016 03:56 PM, Lisa wrote:
which would
find area of triangle and its perimeter?

It's great to have student questions on this forum. If you
don't mind telling us, which teacher and school teaches or uses
D? The only one that I know of is Professor Chuck Allison at
Utah Valley University.

Assuming that you are given the lengths of the three sides, you
can calculate the area with Heron's formula. I had used that
method in this otherwise unrelated chapter:

http://ddili.org/ders/d.en/invariant.html

Ali

Thank you :)
I'm actually from Ukraine)
```
Feb 19 2016
Lisa <dark_nessi mail.ru> writes:
```On Saturday, 20 February 2016 at 01:48:35 UTC, Ali Çehreli wrote:
On 02/19/2016 03:56 PM, Lisa wrote:
which would
find area of triangle and its perimeter?

It's great to have student questions on this forum. If you
don't mind telling us, which teacher and school teaches or uses
D? The only one that I know of is Professor Chuck Allison at
Utah Valley University.

Assuming that you are given the lengths of the three sides, you
can calculate the area with Heron's formula. I had used that
method in this otherwise unrelated chapter:

http://ddili.org/ders/d.en/invariant.html

Ali

import std.stdio;
import std.math;

int main()
{
double a, b, c, p;

writef("Enter a: ");
scanf("%d", &a);
writef("Enter b: ");
scanf("%d", &b);
writef("Enter c: ");
scanf("%d", &c);

p = a + b + c;

writeln("P=", p);

return 0;
}

I try to do just perimetr, but it doesn't work :(
```
Feb 19 2016
=?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
```On 02/19/2016 07:37 PM, Lisa wrote:

import std.stdio;
import std.math;

int main()
{
double a, b, c, p;

writef("Enter a: ");
scanf("%d", &a);

scanf is not a safe function. It trusts the format string and assumes
that 'a' really is what the programmer told it. (For example, although
'a' is not an 'int, %d means 'int' and scanf treats it as such.)

The correct format identifier for double is %lf. You need to replace all
three of with %lf.

However, you are not writing idiomatic D code. I recommend dropping C

http://ddili.org/ders/d.en/input.html

Ali
```
Feb 19 2016
Lisa <dark_nessi mail.ru> writes:
```On Saturday, 20 February 2016 at 03:43:17 UTC, Ali Çehreli wrote:
On 02/19/2016 07:37 PM, Lisa wrote:

import std.stdio;
import std.math;

int main()
{
double a, b, c, p;

writef("Enter a: ");
scanf("%d", &a);

scanf is not a safe function. It trusts the format string and
assumes that 'a' really is what the programmer told it. (For
example, although 'a' is not an 'int, %d means 'int' and scanf
treats it as such.)

The correct format identifier for double is %lf. You need to
replace all three of with %lf.

However, you are not writing idiomatic D code. I recommend

http://ddili.org/ders/d.en/input.html

Ali

module main;

import std.stdio;
import std.math;

int main() {
int A, B, C;
writef("A = ");

writef("B = ");

writef("C1= ");

writefl("P = (%lf)", A + B + C);
return 0;
}

It whatever doesn't work
```
Feb 19 2016
Steven Schveighoffer <schveiguy yahoo.com> writes:
```On 2/19/16 11:15 PM, Lisa wrote:
module main;

import std.stdio;
import std.math;

int main() {
int A, B, C;

writef("A = ");

You want &A, not %A.

Also, note that you declared A, B, C as integers, but are using %lf
which is for doubles. However, readf is much smarter than scanf. You can
just use %s as format specifier, and it will figure out what should
happen based on the given type.

-Steve
```
Feb 19 2016
Ivan Kazmenko <gassa mail.ru> writes:
```On Saturday, 20 February 2016 at 04:15:50 UTC, Lisa wrote:
module main;

import std.stdio;
import std.math;

int main() {
int A, B, C;
writef("A = ");

writef("B = ");

writef("C1= ");

writefl("P = (%lf)", A + B + C);
return 0;
}

It whatever doesn't work

The line "int A, B, C;" should be "double A, B, C;" if you want
to be able to operate non-integer lengths as well.

http://ddili.org/ders/d.en/input.html.  The " %s" can be " %f"
but not " %lf" (that would be the correct string for C's printf

On the output line, you perhaps meant "writefln" instead of
"writefl".  Again, "%lf" should be changed into "%f" or "%s".
```
Feb 20 2016
Lisa <dark_nessi mail.ru> writes:
```On Saturday, 20 February 2016 at 12:59:58 UTC, Ivan Kazmenko
wrote:
On Saturday, 20 February 2016 at 04:15:50 UTC, Lisa wrote:
module main;

import std.stdio;
import std.math;

int main() {
int A, B, C;
writef("A = ");

writef("B = ");

writef("C1= ");

writefl("P = (%lf)", A + B + C);
return 0;
}

It whatever doesn't work

The line "int A, B, C;" should be "double A, B, C;" if you want
to be able to operate non-integer lengths as well.

&A);".
http://ddili.org/ders/d.en/input.html.  The " %s" can be " %f"
but not " %lf" (that would be the correct string for C's printf

On the output line, you perhaps meant "writefln" instead of
"writefl".  Again, "%lf" should be changed into "%f" or "%s".

import std.stdio;
import std.math;

void main() {

writef("Enter side A: ");
double A;

writef("Enter side B: ");
double B;

writef("Enter side C: ");
double C;

writefln("Perimetr is", A + B + C, " centimetrs.");

return 0;
}

Is there smth wrong again?
```
Feb 21 2016
Ivan Kazmenko <gassa mail.ru> writes:
```On Sunday, 21 February 2016 at 12:35:31 UTC, Lisa wrote:
...
Is there smth wrong again?

Yes.

As a programmer, most of the time, you will have to try your
programs by yourself before you consider them correct.

Now, run a compiler, and it complains:
-----
main.d(20): Error: cannot return non-void from void function
-----

Line 20 of your program is "return 0;", and the void function in
question is "void main() {...}".  So, you have to fix either of
that: make main return int instead of void, or remove the return
line.

After that, the program will finally compile.  But that's not the
end, you have to try running it.
"Enter side A:"
shall we say,
"1"
and then it writes
"Enter side B:"
and fails:
-----
std.conv.ConvException c:\Tools\dmd\windows\bin\..\..\src\phob
s\std\conv.d(2729): no digits seen
----------------
0x0040666A in ...
-----

That's a whole lot of unfriendly error text on the screen, but
B.

Now, read the chapter of Ali's book again very carefully, or one
of the posts here.  You may then notice that the space inside the
quotes is important, and also learn why.

The bottom line: the task of writing a program is not finished
until you can compile it, run it, give it at least a few example
inputs, and it prints the right output for all these inputs.

Ivan Kazmenko.
```
Feb 21 2016
Lisa <dark_nessi mail.ru> writes:
```On Sunday, 21 February 2016 at 14:03:56 UTC, Ivan Kazmenko wrote:
On Sunday, 21 February 2016 at 12:35:31 UTC, Lisa wrote:
...
Is there smth wrong again?

Yes.

As a programmer, most of the time, you will have to try your
programs by yourself before you consider them correct.

Now, run a compiler, and it complains:
-----
main.d(20): Error: cannot return non-void from void function
-----

Line 20 of your program is "return 0;", and the void function
in question is "void main() {...}".  So, you have to fix either
of that: make main return int instead of void, or remove the
return line.

After that, the program will finally compile.  But that's not
the end, you have to try running it.
"Enter side A:"
shall we say,
"1"
and then it writes
"Enter side B:"
and fails:
-----
std.conv.ConvException c:\Tools\dmd\windows\bin\..\..\src\phob
s\std\conv.d(2729): no digits seen
----------------
0x0040666A in ...
-----

That's a whole lot of unfriendly error text on the screen, but
variable B.

Now, read the chapter of Ali's book again very carefully, or
one of the posts here.  You may then notice that the space
inside the quotes is important, and also learn why.

The bottom line: the task of writing a program is not finished
until you can compile it, run it, give it at least a few
example inputs, and it prints the right output for all these
inputs.

Ivan Kazmenko.

Thank you sooooo much! :)
Thanks everyone!) You're really cool)
```
Feb 21 2016
Lisa <dark_nessi mail.ru> writes:
```On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote: