www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 111

reply 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
next sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
 Can you please help me and explain how to create a program, 
 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
parent reply 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:
 Can you please help me and explain how to create a program, 
 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
next sibling parent reply 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:
 Can you please help me and explain how to create a program,
 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
parent reply 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:
 Can you please help me and explain how to create a program, 
 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
parent reply 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:
 Can you please help me and explain how to create a program,
 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.) It would help you this week, but you'd be further out of your depth next 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
parent 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
prev sibling parent 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:
 Can you please help me and explain how to create a program, 
 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
prev sibling next sibling parent Ivan Kazmenko <gassa mail.ru> writes:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
 Can you please help me and explain how to create a program, 
 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
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/19/2016 03:56 PM, Lisa wrote:
 Can you please help me and explain how to create a program, 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
Feb 19 2016
next sibling parent 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:
 Can you please help me and explain how to create a program, 
 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
prev sibling parent reply 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:
 Can you please help me and explain how to create a program, 
 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
parent reply =?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 functions altogether an taking advantage of readf(): http://ddili.org/ders/d.en/input.html Ali
Feb 19 2016
parent reply 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 dropping C functions altogether an taking advantage of readf(): 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 = "); readf("%lf", %A); writef("B = "); readf("%lf", %B); writef("C1= "); readf("%lf", %C); writefl("P = (%lf)", A + B + C); return 0; } It whatever doesn't work
Feb 19 2016
next sibling parent 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 = ");
      readf("%lf", %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
prev sibling parent reply 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 = ");
 	readf("%lf", %A);

 	writef("B = ");
 	readf("%lf", %B);

 	writef("C1= ");
 	readf("%lf", %C);

 	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. The lines like "readf("%lf", %A);" should be "readf(" %s", &A);". Please read the reasoning again carefully in Ali's book: 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 but not for D's readf), and the leading space is important. On the output line, you perhaps meant "writefln" instead of "writefl". Again, "%lf" should be changed into "%f" or "%s".
Feb 20 2016
parent reply 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 = ");
 	readf("%lf", %A);

 	writef("B = ");
 	readf("%lf", %B);

 	writef("C1= ");
 	readf("%lf", %C);

 	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. The lines like "readf("%lf", %A);" should be "readf(" %s", &A);". Please read the reasoning again carefully in Ali's book: 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 but not for D's readf), and the leading space is important. 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; readf("%s", &A); writef("Enter side B: "); double B; readf("%s", &B); writef("Enter side C: "); double C; readf("%s", &C); writefln("Perimetr is", A + B + C, " centimetrs."); return 0; } Is there smth wrong again?
Feb 21 2016
parent reply 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 the human-readable part is "no digits seen" when reading 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.
Feb 21 2016
parent 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 the human-readable part is "no digits seen" when reading 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
prev sibling parent Lisa <dark_nessi mail.ru> writes:
On Friday, 19 February 2016 at 23:56:29 UTC, Lisa wrote:
 Can you please help me and explain how to create a program, 
 which would find area of triangle and its perimeter?
And for everybody - I know how to find area and perimetr, but i don't know how to write it on programming language) Can i use "scanf" in D language?
Feb 19 2016