Difference of 2 #s using static variables

Hi. I'm doing this homework assignment for my c++ class and I'm (for lack of a better word) stuck.

Basically, I need to write a function named range- which returns the difference between the largest number that has been passed to it and the smallest number that has been passed to it.

Range needs to remember the largest number so far and the smallest so far. The instructor wants us to use static variables. I can't declare any global variables.

If main says:

cout << range (1) << ", ";
cout << range (4) << ", ";
cout << range (-2) << ", ";
cout << range (3);

then the program should output:

0, 3, 6, 6


This is what i have so far:

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
#include <iostream>
#include <string>

using namespace std;

double range( double x );

int main(){

	double x;

	cout <<range( 1 ) <<", ";
	cout <<range( 4 ) <<", ";
	cout <<range( -2 ) <<", ";
	cout <<range( 3 );

	/*double difference = 0;
	cout << "type some numbers, non-numbers to quit: ";
	while (cin >> x)
		difference = difference - x;
	
	cout << "difference is " << difference << endl;*/

	system("pause");
}

double range( double x ){

	double difference = 0;
	static double largestNow = 0;
	static double smallestNow = 0;
	static bool beenHere = false;

	if( !beenHere )
	{
		beenHere = true;
		if( x > 0 ){
			x = largestNow;
		}
		else
			x = smallestNow;
	}
	else
	{
		if( x <= smallestNow ){
			x = smallestNow;
			difference = largestNow - smallestNow;
		}
		else if( x >= largestNow ){
			x = largestNow;
			difference = largestNow - smallestNow;
		}
		else
			difference = largestNow - smallestNow;
	}
	return difference;
}


The program is running just fine. No errors. However, the results aren't what I want. Instead of getting:

0, 3, 6, 6

I get:

0, 0, 0, 0

I'm not sure how to fix this problem, so it would be nice if anyone can help me out.

Also, I feel like I'm overcomplicating things. I think the more experienced programmers here can already see it when they look at my code haha.
You have your assignment operator backwards. You want to do

smallestNow = x, largestNow = x
Wow! Incredible! Thank you, Smac89!

i didn't think having it backwards would make this big of an impact in the results.

I'm now getting the results i sought. i'll make sure never to make this mistake again haha.

thank you very much!
Last edited on
actually, I still have 1 small error.

i'm getting 0, 4, 6, 6 instead of 0, 3, 6, 6

i tested this out with other numbers. it's not giving the difference for the 2nd number in the result.

for example if i put:
10, 4, -2, 3
i get
0, 10, 12, 12 instead of 0, 6, 12, 12

if i put
1, 14, -2, -3
i get
0, 14, 16, 17 instead of 0, 13, -2, -3

etc.

seems as though something is wrong with the first number entered. maybe there's something wrong with:

1
2
3
4
5
6
7
8
9
if( !beenHere )
	{
		beenHere = true;
		if( x > 0 ){
			largestNow = x;
		}
		else
			smallestNow = x;
	}


i tried, but i don't know what's wrong here. any ideas?
Last edited on
Too much code.

1. Initialize the static/global "min" and "max" sensibly. An example:
static int x_min = std::numeric_limits<int>::max();
static int x_max = std::numeric_limits<int>::min();

2. If new x is outside of a current limit, then update that limit. Two oneliner if-statements

3. Return the difference of the current limits.
Topic archived. No new replies allowed.