I have to write three functions: zero_to_parameter, parameter_to_zer0, range.
Program basically takes two positive integers from user:input1,input2. zero_to_parameter, is supposed to print out numbers from zero to input 1. It already does perfect.
Basically I have trouble with functions parameter_to_zero and range
parameter_to_zero is supposed to print out numbers from input1 to o. Need help with the for loop.
and finally "range" is suppposed to print numbers between input1 and input2. But I don't know how to write the for loop for that. Please help!
#include <iostream>
#include <iomanip>
usingnamespace std;
void zero_to_parameter (int x);
//void parameter_to_zero (int k);
//void range (int y);
int main ()
{
int input1,input2;
cout << " Enter Two Numbers: ";
cin >> input1>> input2;
zero_to_parameter (input1);
// parameter_to_zero(input1);
return 0;
}
void zero_to_parameter (int x)
{
int num;
for (num =0; num <=x; num++){
cout << num << " ," ;
}
}
/*void parameter_to_zero (int k) // Gives out a weird output. Supposed to print numbers from the parameter to zero
{
int num;
for (num =0; num <=k; num--){
cout << num << " ," ;
}
}
//void range (int y)
int num;
*/
for( int i = LASTVALUE; i > FIRSTVALUE - 1; --i ); //decremented since we start from highest and go to the lowest
//here are two more ways
for( int i = 100; i > -1; --i ); //prob want to make them signed but doesn't look to great
for( int i = 100; i >= 0; --i );
If range is from input one to input two you should use a loop then for that?
Have the initial value being the lower value of input 1 and 2. if input 1 is 10 and input 2 is 5 that would be 5. Then loop until it reaches the highest value which would be 10 incremented by 1 each time.
But it would help of you posted the code which is causing the problem. for (num =0; num <=k; num--)
This is the original code.
The initial value of num should be k (or perhaps k-1), but definitely not zero.
The loop will continue to repeat as long as the condition num <=k; is true.
Well, if num is zero and k is 5, the condition is true. Next time num is -1. The condition is still true. It will never stop until num has cycled thyrough all the negative numbers and rolled over to become positive again - that's billions of iterations. How about changing the condition to test for num being greater than or equal to zero?
That is the same thing I also said earlier =p It will ALWAYS be less than the starting value if you are decreasing each time. But it will not always be greater than -1 ( greater than or equal to 0 ) when you decrease from a starting number.