Compiler?

I've written a program that calculates the sum of numbers on a number line.

<----------|-------------------->
-4 -3 -2 -1 0 1 2 3 4 5 6

The sum depends on two points, say first point and second point. (Order is first from the left)
Test cases: If first point is -1, and second point is 4, then the sum should look like " -1 + 0 + 1 + 2 + 3 + 4 ", which is = 9

If first point is -3 and second point is 5, then the sum should look like
" -3 + -2 + -1 + 0 + 1 + 2 + 3 + 4 + 5 + 6 ", which is = 15.

Below is the C++ source 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

#include <cstdlib>
#include <iostream>

using namespace std;

int sum (int m, int n); //Recursive function
void welcome_screen();
void user_entry();
void good_bye();

int main () //MAIN FUNCTION
{
	welcome_screen();
	user_entry();
	good_bye();
		
return 0;
} //END OF MAIN FUNCTION







int sum (int fn, int ln) 
{
	
	if (fn < ln)  
	{
		int temp = fn;
		fn = ln;		
		ln = temp;
	} 
	

		if (fn == 1)
		{
			return 1;	
		}
		
		else if ((fn > ln) && (fn > 0 && ln >= 0))
		{	
			return fn+sum(fn-1,ln); 		
		}
	
		else if ((ln < fn) && (fn <= 0 && ln < 0))
		{	
			return ( fn+sum(fn-1,ln) ); 		
		}
		
		
		else if (fn > 0 && ln < 0) 
		{
			int pos = 0;
			int neg = 0;
			
			if(fn <= ln)
			{
				pos = fn+sum(fn-1,ln);
				
			} 
			
					
			if(ln <= fn)
			{
				neg = (ln+sum(ln+1,fn)); 
			}
			
			return pos + neg;
			
		}

}


void welcome_screen()
{ //Prints the Welcome message
	
	cout <<"\n--------------------------------------\n";
	cout << "\n                WELCOME \n";
	cout <<"\n--------------------------------------\n";
	cout << "\n   Let's find out the sum of consecutive integers from 'point 1' to 'point 2' . \n";
}


void user_entry()
{ //User entries are done here
	int fn = 0;
	int ln = 0;
	cout << "\n\n";
	cout << "\n First enter the value of 'point no.1': ";
	cin >> fn;
	cout << "\n Please enter the value of 'point no.2': ";
	cin >> ln;
	cout << "\n The sum of consecutive integers from "<< fn << " to " << ln << " is: " << sum(fn,ln) << " . \n";
	char choice = '\0';
	cout << "\n\t Do you want to try again? [Y/n]: ";
	cin >> choice;
		while (choice == 'Y')
		{
			cout << "\n\n";
			cout << "\n Please enter the value of 'point no.1': ";
			cin >> fn;
			cout << "\n Please enter the value of 'point no.2': ";
			cin >> ln;
			cout << "\n The sum of consecutive integers from "<< fn << " to " << ln << " is: " << sum(fn,ln) << " . \n";
			cout << "\n\t Do you want to try again? [Y/n]: ";
			cin >> choice;
		}

}


void good_bye()
{ //Print the good bye message
	
	cout <<"\n\n\n--------------------------------------\n";
	cout << "\n        THANK YOU \n";
	cout <<"\n--------------------------------------\n\n\n";

}




I'm using Codelite IDE (http://www.codelite.org) on Kubuntu 14.04 and the program works just fine, with correct output.(The same goes for NetBeans on Kubuntu)
In this case, 3 + 4 + 5 comes to 12
When first point is 3 and second point is 5

However, I get different results when I use DevC++ or Microsoft Visual Studio on windows, as well as the Online C++ compiler, i.e C++Shell (http://cpp.sh/). I notice that this problem comes up when I use other IDE's on other OS's like Windows, since I always use a Linux distro like Kubuntu.
In this case, 3 + 4 + 5 comes to 9
When first point is 3 and second point is 5

Can someone tell me where did I go wrong?

I was thinking that the differences in the compilers might have been the issue.
Last edited on
What happens if none of the if in sum(...) apply? I.e. fn == ln and both > 0. Like 3 -> 5.
Oh! I forgot to add a conditional checkpoint for fn == ln. It looks like nothing happens (*Big mistake) in the "sum(...) function" when fn == ln, where the sum comes to 0 .

Wow! The program really works fine now when I add if(fn == ln) { .... } .
Now 3->5 is coming to 12. (Even 4->4 comes to 4, and not 0)

Thank you very much coder777!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

// sum all numbers in [from,to]
int sum_range( int from, int to )
{
    int sum = 0 ;
    for( int n = from ; n <= to ; ++n ) sum += n ;
    return sum ;
}

int main()
{
	std::cout << "\n First enter the value of 'point no.1': ";
	int first ;
	std::cin >> first;

	std::cout << "\n Please enter the value of 'point no.2': ";
	int second ;
	std::cin >> second;

	std::cout << "\n Sum: " << sum_range( first, second ) << '\n' ;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    int start , end, sum = 0, temp;
    
    std::cin >> start;
    std::cin >> end;

   if( start > end )
  {
      temp = start
      start = end;
      end = temp;
  }
    
    for (int i = start; i <= end; i++)
    sum += i;
    
    std::cout << sum;
}
Last edited on
It's nice to see other examples, and it's useful too.

Thank you! JLBorges, and kemort
Topic archived. No new replies allowed.