### C++

Hi I have a question for a task:

It is a function of double u (double t) for any t the type to double
, programming the values ​​of a periodic "sawtooth" with
the period 1 calculated for an arbitrary t.
In the interval [0,1) is designed u (t) = t.
Use u in an appropriate cast operator to convert double
to int, and no (!) loops.
Create a matching test program that lets you repeat the
Can calculate value of u for input values ​​of t. The function u, and the
Test program should in different source files in the project
be saved.

I have this programm at the moment .

But I think it is not so right.

Can somebody help me?

#include <iostream>

using namespace std;

int main()
{
double d=3.5;

int i;

i= (int) d;

cout<< d - i << endl;

}

No loops? Try recursion!

 ``12345678910111213`` ``````#include double u(double t) { if (t < 1) return t; return u(t-1); } int main() { double d= 3.5; cout << u(d); }``````

Why do you think your code is not right? It actually is!
 ``12345`` ``````double u(double t) { int i = (int)d; return d-i; }``````
Ok it´s good that my code is right.

Can you tell me how I can create a matching testprogramm?
 ``1234567891011121314151617`` ``````#include using namespace std; double u(double t) { int i = (int)d; return d-i; } int main() { while(true) { double someNum; cin >> somNum; cout << u(someNum) << endl; } }``````
Hallo Stewbond.

My hole programm is looking so now, but my code Blocks is now showing me a error at u.

It says u has not been declared in my scope .
What is wrong?

Can you help me?

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main() { double d=3.5; int i; i= (int) d; cout<< d - i << endl; while(true) { double someNum; cin >> somNum; cout << u(someNum) << endl; } }``````
Last edited on
in the example above, I declared a function `double u(double t)`. You are missing that.

If you want to do it without the function then do this:
 ``123456789101112`` ``````#include using namespace std; int main() { while(true) { double d; cin >> d; int i = (int)d; cout << d-i << endl; } }``````
Can you also help me with this thing here?

Can calculate value of u for input values ​​of t. The function u, and the
Test program should in different source files in the project
be saved.

How can I do this?

I have now made it so:

#include<iostream>
#include "other data.cc"

using namespace std;
double u(double t)
{
int i = (int)t;
return t-i;
}

int main()
{
while(true)
{
double somNum;
cin >> somNum;
cout << u(somNum) << endl;
}
}

But I don´t know how I should save this in another data?
Last edited on
 ``12345678910111213141516171819202122232425`` ``````#include "otherData.cc" using namespace std; double u(double t) { int i = (int)t; return t-i; } #include #include "otherData.cc" using namespace std; int main() { while(true) { double somNum; cin >> somNum; cout << u(somNum) << endl; } } ``````

My programm is showing me an error .
That the directory otherData is missing.

How can I solve this?
Last edited on
You don't include other source files. Obviously this is more useful if you are working on a larger project, but do this:

 ``123456789101112131415`` ``````//// source0.cpp #include using namespace std; double u(double t); int main() { while(true) { double somNum; cin >> somNum; cout << u(somNum) << endl; } }``````

 ``123456`` ``````//// source1.cpp double u(double t) { int i = (int)t; return t-i; }``````
Oh thank you it`s working now, but I have one question.

When I run the programm and tipe a number for example 2,5.

Why does it show a series of 0 ?

I dont understand this.
Has somebody an idea?

Why does this happen?
Hi people can somebody help me?
closed account (D80DSL3A)
cin choked to death on the comma in 2,5?