C++

Dec 11, 2012 at 7:31pm
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;


}

Dec 11, 2012 at 8:36pm
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;
}
Dec 11, 2012 at 9:05pm
Ok it´s good that my code is right.

Can you tell me how I can create a matching testprogramm?
Dec 11, 2012 at 9:32pm
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;
    }
}
Dec 11, 2012 at 10:00pm
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 Dec 11, 2012 at 10:02pm
Dec 12, 2012 at 6:51am
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;
    }
}
Dec 12, 2012 at 8:04am
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?



Dec 12, 2012 at 11:13am
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 Dec 12, 2012 at 8:36pm
Dec 12, 2012 at 8:35pm
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 Dec 12, 2012 at 8:37pm
Dec 12, 2012 at 9:01pm
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;
}
Dec 12, 2012 at 10:18pm
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.
Dec 13, 2012 at 6:41pm
Has somebody an idea?

Why does this happen?
Dec 16, 2012 at 12:24pm
Hi people can somebody help me?
Dec 16, 2012 at 12:51pm
closed account (D80DSL3A)
cin choked to death on the comma in 2,5?
Try entering 2.5 instead.
Last edited on Dec 16, 2012 at 12:52pm
Dec 16, 2012 at 1:18pm
Oh yeah thank you . It´s working now.
Topic archived. No new replies allowed.