yessssssss, I did it

K

kalooody

finally :)
I did the program
but I have one Q ?
the numbers of the addition, ....
wrong :S
I do know y ?


#include<iostream>

using namespace std;

class interval
{
public:

void set(int,int,int,int);
void get(int&,int&,int&,int&);
void print() ;
void addition1() ;
void addition2() ;
void subtract1() ;
void subtract2();
void multiply1() ;
void multiply2() ;
void divide() ;
interval();
interval(int ,int ,int ,int );

private:
int lower1;
int upper1;
int lower2;
int upper2;
int add1;
int add2;
int sub1;
int sub2;
int mul1;
int mul2;
int d1;
int d2;
};

int main()
{
int l,u,ll,uu;
interval frist;

cout<<"Enter the lower and the upper limits of the first interval:
";
cin>>l>>u;
cout<<"["<<l<<","<<u<<"]"<<endl;
cout<<endl;

cout<<"Enter the lower and the upper limits of the second interval:
";
cin>>ll>>uu;
cout<<"["<<ll<<","<<uu<<"]"<<endl;
cout<<endl;

frist.set(l,u,ll,uu) ;
frist.print();

return 0;
}

interval::interval()
{

set (0,0,0,0);

/*
lower1=0;
upper1=0;
lower2=0;
upper2=0;
add1=0;
add2=0;
sub1=0;
sub2=0;
mul1=0;
mul2=0;
d1=0;
d2=0;
*/

}

interval::interval(int l, int u , int ll , int uu)
{
set (l,u,ll,uu);
}

void interval::set(int l, int u , int ll , int uu)
{
lower1=l;
upper1=u;
lower2=ll;
upper2=uu;
}

void interval::get(int& l,int& u ,int & ll , int &uu)
{
l=lower1;
u=upper1;
ll=lower2;
uu=upper2;
}

void interval::addition1()
{

add1=lower1+lower2;

}


void interval::addition2()
{

add2=upper1+upper2;
}


void interval::subtract1()
{

sub1=lower1-upper2;

}

void interval::subtract2()
{
sub2=upper1-lower1;

}

void interval::multiply1()
{
mul1=lower1*upper1;
}

void interval::multiply2()
{
mul2=lower2*upper2;
}

void interval::divide()
{
if(lower1==0&&upper1==0)
cout<<"error"<<endl;
else
{
d1=1/lower1;
d2=1/upper1;
}
}

void interval::print()
{
cout<<"The sum of the two intervals is:"<<"["<< add1 <<","<< add2
<<"]"<<endl<<endl;
cout<<"The subtraction of the two intervals is:"<<"["<< sub1 <<","<<
sub2 <<"]"<<endl<<endl;
cout<<"The multiplication of the two intervals is:"<<"["<< mul1
<<","<< mul2 <<"]"<<endl<<endl;
cout<<"The reciprocal of the first interval is:"<<"["<< d1 <<","<<
d2 <<"]"<<endl<<endl;
}
 
O

osmium

finally :)
I did the program
but I have one Q ?
the numbers of the addition, ....
wrong :S
I do know y ?

<snip>

Now you have written a second program, new and different, and ignored my
advise from earlier today about writing too much without testing.

I can see three reasons for doing this:

o You didn't see what I wrote.
o You didn't understand what I wrote.
o You think my advise is not worth bothering with.

In any event I see that I wasted my time in trying to help you.
 
L

LR

finally :)
I did the program
but I have one Q ?
the numbers of the addition, ....
wrong :S


I don't know.

I suggest the following.

1) Post an example that will compile and demonstrate the error. Please
make it easy for those who might be willing to help you and make the
sample code as short as possible, and since your error seems unrelated
to io problems, please initialize all variables in the code.

2) Tell us what the answer should have been.

3) Post a short description of whatever assignment you're working on.
It can be very difficult to tell you if you're right if we don't know
what your objective is.

4) Before you do that, see if your development environment has a
debugger and use it to see whats happening in your code at runtime. Or
if there is no debugger available, then use std::cout to print things to
see what's happening.

Also, could you please spell out the words you're using in full? There
are many people here for whom english is not their first language and
they may have trouble understanding what you mean. IMHO, some native
speakers may have trouble too. I think if you do this you may get more
people trying to figure out your problem rather than figuring that the
head-scratching isn't worth their time.

And finally, could you please describe what an interval is supposed to
be. Of course, classes don't have to be named in ways that are obvious,
struct Square { double centerX_, centerY_, radius_ }; might make sense
to someone. But my understanding of what an interval is seems at odds
with the class you're creating. Maybe just my own ignorance.

TIA,

LR
 
S

Stuart Golodetz

finally :)
I did the program
but I have one Q ?
the numbers of the addition, ....
wrong :S
I do know y ?

If you're trying to do interval arithmetic, do you mean something more like
this?

#include <iostream>

template <typename T>
class Interval
{
template <typename U> friend class Interval;
private:
T m_lower;
T m_upper;
public:
Interval(const T& lower, const T& upper)
: m_lower(lower), m_upper(upper)
{}

template <typename U> Interval& operator+=(const Interval<U>& rhs)
{
/*
[a,b] + [c,d] = [a+c,b+d]
*/
m_lower += rhs.m_lower;
m_upper += rhs.m_upper;
return *this;
}

template <typename U> Interval& operator-=(const Interval<U>& rhs)
{
/*
[a,b] - [c,d] = [a-d,b-c]
*/
m_lower -= rhs.m_upper;
m_upper -= rhs.m_lower;
return *this;
}

//...

const T& lower() const
{
return m_lower;
}

const T& upper() const
{
return m_upper;
}
};

template <typename T, typename U>
Interval<T> operator+(const Interval<T>& lhs, const Interval<U>& rhs) //
may not always want to return Interval<T> here...
{
Interval<T> ret(lhs);
ret += rhs;
return ret;
}

template <typename T, typename U>
Interval<T> operator-(const Interval<T>& lhs, const Interval<U>& rhs) //
ditto
{
Interval<T> ret(lhs);
ret -= rhs;
return ret;
}

template <typename T>
std::eek:stream& operator<<(std::eek:stream& os, const Interval<T>& interval)
{
os << '[' << interval.lower() << ',' << interval.upper() << ']';
return os;
}

It's late and I'm tired, so there are probably various problems with it
which can usefully be observed (indeed I welcome helpful criticism!), but I
think it's more the sort of thing you might be after.

HTH,
Stu

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top