need help with functions

Hey guys,

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!

Thanks in advance

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>

using namespace 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;
 
 */
Look at line 30:
 
    for (num =0; num <=x; num++)


• num starts from 0.
• the loop will execute as long as the condition num <=x is true.
• each time around the loop, num is incremented by 1.

You simply need to use similar ideas for the other loops, alter the start value, condition and increment amount as required.

Notice one of the functions will require two parameters to be passed to it.
or you could just reverse the iterations. ex:
1
2
3
4
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.
Thank you both for replying actually I could really try giblit's code for the last function. And I will.

The problem I have is with the second function: parameter_to_zero.

For example if user enters 5 for the FIRSTVALUE it should cout: 4,3,2,1,0

But It doesn't. It outputs something weird. I can't actually post the output here. It's too ling. Basically it outputs 1000+ memory locations.
I can't actually post the output here.

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?
Last edited on
hey Chervil,

Thanks for the reply. I tried with numb=kbut it wouldn't work.
It only runs the first function. zero_to_parameter.

Here's the code

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>

using namespace std;

void zero_to_parameter (int x);
void parameter_to_zero (int k);
void range (int y);

int input1,input2;

int main ()
{
    
 	
	cout << " Enter Two Numbers: ";
	cin >> input1>> input2;
    
	zero_to_parameter (input1);
    
    parameter_to_zero(input1);
    
  //  range (input2, 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 numb;
	for (numb =k; numb <k; numb--){
        
		cout << numb << " ," ;
        
 	}
}

/*
void range (int y) {

for( int i =input2; i > input1 - 1; --i ){
    
    cout << i << ",";

}
}

*/


Heres the output
 Enter Two Numbers: 5
6
0 ,1 ,2 ,3 ,4 ,5 ,
Last edited on
Take a look at line 45, numb <k;

I repeat my previous suggestion: "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.
hey guys,

I'm really sorry, I have a hard time understanding the for loop. So weird.

Can you please fix it yourself and post the proper code below for both the functions

"void parameter_to_zero" and "range"(which is commented out).

Thanks
I don't know what more to say. The answers are right in front of you.
Maybe reading this tutorial will help, study the diagram and read the explanation.
http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm
Topic archived. No new replies allowed.