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!

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

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!
1
2
3
4
5
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

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:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "otherData.cc" 

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

#include<iostream> 
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
////  source0.cpp
#include <iostream>
using namespace std;

double u(double t);

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


1
2
3
4
5
6
//// 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?
Try entering 2.5 instead.
Last edited on
Oh yeah thank you . It´s working now.
Topic archived. No new replies allowed.