Using for loop to add sum of numbers over two intervals

I'm having trouble adding up the sum over two values. My assignment is to prompt the user for 1 integer and find the sum of the integers over 1 to what ever the user input. I'm still super new to c++ and don't even know if I did the initialization statement correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int base = 1, uno, dos, tres, sum, temp;

int main()
{
	cout << "Please input an integer greater than or equal to 1: ";
		cin >> uno;
		
		for (base = 1; base <= uno; base++)
		{
			sum = base + temp;
			base = sum;
			base = temp;
		}
		
		cout << sum << endl;
		system("pause");
		return 0;
}
1. You declare all your variables on line 3. That is not inside any function. These are global variables. This program does not need global variables. Most don't.

First thing, make the variables local:
1
2
3
4
5
6
7
#include <iostream>

int main()
{
  int base = 1, uno, dos, tres, sum, temp;
  std::cout << "Please input an integer greater than or equal to 1: ";
  std::cin >> uno;

By now we know that base==1 and that uno has whatever value did the user give.

We do not know the value of dos, tres, sum, or temp.
That is now my fault. When they were global, they were all set 0. As local, they are not. I'll fix that:
1
2
3
int main()
{
  int base = 1, uno {}, dos {}, tres {}, sum {}, temp {};

int uno {}; and int uno = 0; happen to mean the same.

Wait! What are the dos and tres? They are not used anywhere. If you don't need a variable, then don't have it at all:
1
2
3
int main()
{
  int base = 1, uno {}, sum {}, temp {};

Now we get to the loop. On first time the base==1. Lets assume that uno is at least 1. For example 3.

Thus, on first iteration:
1
2
3
sum = base + temp; // sum = 1 + 0, sum=1
base = sum; // base = 1
base = temp; // base = 0 

Furthermore, the base++ executes too, so base becomes 1.

Second iteration. The 1 is still less than 3. Lets compute the body again:
1
2
3
sum = base + temp; // sum = 1 + 0, sum=1
base = sum; // base = 1
base = temp; // base = 0 

Obviously, the base++ executes too, so base becomes 1 (again).

Third iteration. The 1 is still less than 3.
I have an eerie feeling. How about you?


Edit:
Lets try something related:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
  int uno {};
  std::cout << "Please input an integer greater than or equal to 1: ";
  std::cin >> uno;
  int sum {};
  // at this point the sum==0 as it should, for we have added nothing yet
  for ( int base = 1; base <= uno; ++base )
  {
    std::cout << "I should add " << base << " to the sum\n";
  }
  std::cout << "Total " << sum << '\n'; // this will show 0, for we didn't add anything
  return 0;
}

That program should show something like:
Please input an integer greater than or equal to 1:
4
I should add 1 to the sum
I should add 2 to the sum
I should add 3 to the sum
I should add 4 to the sum
Total 0

Would adding the value of base to the sum be the right thing to do?
Last edited on
I think you mean this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;


int main()
{
    int base = 0, uno, dos;
	cout << "Please input an integer greater than or equal to 1: ";
		cin >> uno;
		
		for (dos = 1; dos <= uno; dos++)
		{
			base += dos;
		}
		
		cout << base << endl;
		system("pause");
		return 0;
}
@keskiverto & @Manga
Thanks so much for the quick responses! But to try and answer your last question Keskiverto I think yes. I need to brush up on the tutorial page because I'm unfamiliar with the += sign that you suggested Manga. I'll be sure to see what I can cook up when I get outta class. Thanks again!
Good. Solving the problem with the loop is good practice for you and exactly what you should to do.



However, math calls "1 to whatever" an arithmetic series.
http://www.mathwords.com/a/arithmetic_series.htm
There are uno integers in the series from 1 to uno.
Therefore:
1
2
3
4
// n=uno
// a1=1
//a2=uno
sum = uno * (1 + uno) / 2;

Thus, there is an analytical solution to the problem that requires only one statement and does exactly the same amount of work with every value of uno.

The other part of programming is to learn to apply an appropriate algorithm for a problem.


There is one twist though: the user might not give "an integer greater than or equal to 1". Lets say that I write "-7" or "hello".

The 1 <= -7 is clearly false. Loop does no iterations at all, and the sum remains 0. That seems ok way to handle the user error.

The "hello" does not convert to integer, so the input fails, and the value of uno will be?
[quote]In C++98/C++03, if an error occurs, uno is left unchanged.
In C++11, it is set to 0.[/code]
I.e. answer, and result of condition do depend on language version.

The analytic version has no condition.
1
2
cin >> uno;
sum = uno * (1 + uno) / 2;

One should test the uno before using it in calculation.
Thanks for all the in depth explanations for everything they are really helping me learn. But for the second part of my assignment I'm tasked with using a while loop to output the amount of intervals between two integers (the ones that the user inputs) excluding the 2 integers that they input (EX: 1 and 9 = 7). To be honest I don't know what to even write in the initialization statement.

1
2
3
4
5
6
7
8
9
10
 cout << "Please input two integers: ";
			cin >> uno2 >> dos2;

			while (uno2 != dos2)
			{
				base2 = uno2 + 1;
				base2 = base2 - 2;
				uno2++;
			}
			cout << base2 << endl;

Update I got this far with some thinking, but it doesnt work if I don't use the first variable as 1
Last edited on
What is "amount of intervals between two integers"?


Where is the initialization statement of a while loop?
I meant the amount of integers between two integers so like if the user inputs 1 6 the answer should be 4. or something like -3 2 would also be 4.
Counting. It is similar to computing a sum, but rather than adding values to sum, you increase the count by 1 for each value.
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
#include <iostream>
using namespace std;


int main()
{
int uno2, dos2;

cout << "Please input two integers: ";

			cin >> uno2 >> dos2;


    
    int result = -1;
   while(uno2 < dos2) {
    uno2++; //add 1
    result++; //add 1
   }
  
   cout << result;
   
		system("pause");
		return 0;
}
Last edited on
As for the += sign... it is just shorthand.

1
2
3
4
int a=1;
int b=2;

b += a; // means b = b + a; 


you can also you it with other math symbols...
+=
-=
*=
/=
for example.
Last edited on
Topic archived. No new replies allowed.