6 Replies - 1209 Views - Last Post: 25 October 2006 - 06:41 PM Rate Topic: -----

#1 chrisbenoit06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-October 06

output loop

Posted 24 October 2006 - 05:38 PM

Hey Everyone, Can anyone help me out with my question?
How would I be able to make the program loop so that it allows me to repeatedly test the function during the output instead of running it over and over each time for different values of Z? Any ideas? Thanks

#include <iostream>



double f(double x);

int main()
{
double z;
cout<<"Enter a value for z\n";
cin>>z;
cout<<f(z)<<endl;

}
double f(double x)
{
if(x>0){
double sum=0; 
for(int i=1;i<=(int)(x);i++){
sum+=i; 
}
return sum;
}
else if(x==0){
return 0; 
}
else{
return x+2; 
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: output loop

#2 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 07-August 05

Re: output loop

Posted 24 October 2006 - 09:16 PM

Nice screen name :) ; I assume you're a fellow wrestling fan?
Anyway, as for the question at hand, try a while loop. You could run it for a predetermined number of iterations (so then you'd have a counter, and you'd update it by 1 after each iteration, and then quit when you've reached the number of tests you want).

Alternatively, you could run the while loop until the user enters STOP or some other response. Then you would test the input to see if the user said stop or entered a number to test. If you took this route, you'd have to change the z variable to a string (or char; you could ask the user to enter 'n' to end the tests), check the value of z, and if it's a request to quit, end the loop, and if it's a number, convert it to a double.
Was This Post Helpful? 0
  • +
  • -

#3 chrisbenoit06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-October 06

Re: output loop

Posted 25 October 2006 - 08:30 AM

Thanks for replying dragonlady. Yes I am a huge wrestling fan and wrestler myself. I am new to programming however. I think i will go with the while loop up to z=5. Where abouts should I insert it in the program?
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 07-August 05

Re: output loop

Posted 25 October 2006 - 09:13 AM

You can put the while loop in the main function, where you're prompting the user for a value and then outputting the result. Do you know how to use a while loop? Since you said you are new to programming, I don't want to automatically assume that you know. Here's an example:

int i = 0;
while(i < 5)
{
  cout<<i<<endl;
  i++;
}



In this example I print out numbers 0 through 4. Notice that I updated i in the while loop; if you forget that you'll have an infinite loop.

I could have achieved the same thing with a for loop:
for(int i = 0; i<5; i++)
{
  cout<<i<<endl;
}



Either type of loop should work in your case.

Oh and its cool that you're a wrestler :) I have the utmost respect for what wrestlers do and how they put their bodies on the line to entertain fans B)
Was This Post Helpful? 0
  • +
  • -

#5 chrisbenoit06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-October 06

Re: output loop

Posted 25 October 2006 - 01:53 PM

Thank-You very much. Your very kind. However I am still having difficulties with my code. Essentially what Im trying to output is type a vale for z and then produces the answer in which I can repeatedly test the next value of z.
Example:
Enter a value for z
1
1
Enter next value for z
2
Etc

Is that possible?
Was This Post Helpful? 0
  • +
  • -

#6 chrisbenoit06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-October 06

Re: output loop

Posted 25 October 2006 - 05:21 PM

I got it....but is it write?

 
#include <iostream>

double f(double x);

int main()
{
double z;
cout<<"Enter a value for z: ";
cin>>z;
cout<<"f(x)="<<f(z)<<endl;

cout<<"Enter a value for z: ";
cin>>z;
cout<<"f(x)="<<f(z)<<endl;

cout<<"Enter a value for z: ";
cin>>z;
cout<<"f(x)="<<f(z)<<endl;
}

double f(double x)
{
if(x>0){
double sum=0;
for(int i=1;i<=(int)(x);i++){
sum+=i;
}
return sum;
}
else if(x==0){
return 0;
}
else{
return x+2;
}
}


Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: output loop

Posted 25 October 2006 - 06:41 PM

Well that would most certainly work. Although its not very efficient and you have some code redundancy.

It would be much better if you put this inside a FOR loop that iterates 3 times. I am assuming based on your code that you only want to get 3 responses. Otherwise it would be better to use a WHILE loop with a sentinal value that the user would enter to cause the loop to exit.

Like this:

#include <iostream>

double f(double x);

int main()
{
double z;

for(int i = 0; i<3; i++)
{
   cout<<"Enter a value for z: ";
   cin>>z;
   cout<<"f(x)="<<f(z)<<endl;  
}

}

double f(double x)
{
if(x>0){
double sum=0;
for(int i=1;i<=(int)(x);i++){
sum+=i;
}
return sum;
}
else if(x==0){
return 0;
}
else{
return x+2;
}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1