Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help me sloving this exercise !


This topic has been archived. This means that you cannot reply to this topic.
24 replies to this topic

#13 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 13 December 2012 - 10:06 AM

greg, he edited his post after i told him.

Saw that after the fact, sorry.

ErtanTaboo, no that's not right. Have you learned how to write loops yet? Try a for loop.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#14 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 13 December 2012 - 10:06 AM

more like
do {
    statements;
}while (condition);
You did
cin
{
and removed the while() which is wrong

#15 ErtanTaboo

ErtanTaboo

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 13 December 2012 - 10:15 AM

is correct now ?

#include <iostream>
using namespace std;
int main ()
{
int m,n,i;
double x,S,Y;
cout<<"value of x , m , n=";
cin>>x,m,n;
S=0;
i=1;
do {
S=S+(x+2/i);
i=i+1;}
while
	 (i>(m+n-1));
Y=x/3+2*S;
cout<<"Amount of function Y="
  <<s
  <<"\n";
return 0;
}


#16 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 13 December 2012 - 10:18 AM

Your > is still wrong in the conditional. Remember what I said earlier, you want to continue looping while i is less than the upper bound.

And your output variable is wrong again.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#17 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 13 December 2012 - 10:20 AM

When you end the loop, },while and the condition need to be on the same line. That's an error right there.

#18 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 13 December 2012 - 10:23 AM

The S*2 is gone again.

He doesn't need it. It's right here:
Y=x/3+2*S;

and when you end the loop, },while and the condition need to be on the same line. That's an error right there.

C++ doesn't care about whitespace. He can place it anywhere he likes and it will compile. This is just about formatting standards.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#19 ErtanTaboo

ErtanTaboo

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 13 December 2012 - 10:24 AM

like this ? or something else because i cant understand you very well , if this is not correct , could you writte that for me ?

#include <iostream>
using namespace std;
int main ()
{
    int m,n,i;
    double x,S,Y;
    cout<<"value of x , m , n=";
    cin>>x,m,n;
    S=0;
    i=1;
    do {
    i=i+1;
    S=S+(x+2/i);
    } while (i>(m+n-1));
    Y=x/3+2*S;
    cout<<"Amount of function Y="
        <<Y
        <<"\n";
    return 0;
}


#20 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 13 December 2012 - 10:25 AM

On this line:
 } while (i>(m+n-1));
turn this:
>
into this:
<

If you don't understand why, try single stepping through your code with a pencil and paper and see how the statements evaluate.

Also, I trust you're going to change that last variable back to 'Y' before you hand it in.

Edit:

Also, another thing: You need to switch the position of these two statements:
i=i+1;
S=S+(x+2/i);

The reason being is that you do not want to increment i until after you use its value in the summation equation. The way you use it above, you will never get the value of 1 used, and you will pass the upper bound by 1 as well. It will yield incorrect results. Switch them so you increment i after the summation equation.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#21 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 13 December 2012 - 10:26 AM

greg:
1. woops..noticed and edited that right before you posted haha
2. did not know that..

#22 ErtanTaboo

ErtanTaboo

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 13 December 2012 - 12:43 PM

is that right now ?

#include <iostream>
using namespace std;
int main ()
{
int m,n,i;
double x,S,Y;
cout<<"value of x=";
cin>>x;
cout<<"value of n=";
cin>>n;
cout<<"value of m=";
cin>>m;
S=0;
i=1;
do {
i=i+1;
S=S+(x+2/i);
} while (i<(m+n-1));
Y=x/3+2*S;
cout<<"Amount of function Y="
  <<Y
  <<"\n";
return 0;
}


#23 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 13 December 2012 - 01:14 PM

Looks good. Did you run it ?

#24 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 13 December 2012 - 01:21 PM

You haven't reversed the statements within your do-while loop like I mentioned before. If you insist on leaving it that way, then you must start i at 0 instead of 1.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download