February 20, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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)
|
February 20, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lisa | 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?
|
February 20, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 :(
|
February 19, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lisa | 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 |
February 20, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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
|
February 20, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lisa | 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 |
February 20, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lisa | 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". |
February 21, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Kazmenko | 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?
|
February 21, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lisa | 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\phobos\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. |
February 21, 2016 Re: 111 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Kazmenko | 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\phobos\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)
|
Copyright © 1999-2021 by the D Language Foundation