Help Writing a Loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main () {

	int i = 1;
	int n, Sn, sSum;

	cout << "Please enter a value for n: ";
	cin >> n;	

/*	run the equation for Sn for each value in the series i to n

	Sn = ( n + n2 ) / 3

	the sum of the value(s) of Sn equal sSum	*/

	cout << sSum;

	system("PAUSE");
	return 0;
}


The comment part is my best explanation of what I need the loop to do. My best guess is it needs to be a for-while loop of some sort.

The question reads:

Write a program to compute, and later output to the screen, the sum of the series Sn, where Sn is the sum, from i = 1 to n, of ( n + n2 ) / 3. The value n, is entered, via the keyboard, by the user.
Last edited on
closed account (E3h7X9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main () {

	int i = 1;
	int n, Sn, sSum = 1; // initialise sum with 1

	cout << "Please enter a value for n: ";
	cin >> n;

    for( i; i <= n; i++) //for loop will make one loop then increment i with i++ which means i = i + 1
    {                    // till i is greater than n
        Sn = (n + n*2) / 3; // calculating Sn
        sSum += Sn; // equivalent to sSum = sSum + Sn
    }
	cout << sSum;

    cin.ignore(1, ' '); // I recommend to use cin.ignore instead of system pause
	return 0;
}



Thank you! That helps me understand.

Can you tell me more about the reasoning behind this bit:

int sSum = 1; // initialize sum with 1

What is the reasoning behind "initializing" the variable Sn?
closed account (E3h7X9L8)
oh sorry I did a mistake you should initialise sSum with 0

because when you do the first loop sSum = sSum + Sn is sSum = 0 + Sn; then next time is sSum = sSum(which is 0 + Sn) + Sn;

if you dont initialise sSum with anything strange things can happen to the value of variable
Last edited on

This is the loop I've got now:

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 main () {

	int i = 1;
	int n, Sn, sSum = 1;

	cout << "Please enter a value for n: ";
	cin >> n;

    for( i; i <= n; i++)
    {                   
        Sn = ( n + n*2 ) / 3 / 3;
        sSum += Sn
    }
	cout << sSum;

	return 0;
}


But I made a mistake in the equation, it's suppose to be:

Sn = ( n + n2) / 3

However, when I make that change in the code to:

Sn = ( n + pow(n,2)) / 3

The "pow" has a squiggly red line under it, and I get an error message:

IntelliSense: more than one instance of overloaded function "pow" matches the argument list

Why wont the program run after I make this change?
Last edited on
#include <math.h>

for pow function
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>
#include <math.h>
using namespace std;

int main () {

	int i = 1;
	int n, Sn, sSum = 1;

	cout << "Please enter a value for n: ";
	cin >> n;

    for( i; i <= n; i++)
    {                    
        Sn = ( n + pow(n,2) ) / 3;
        sSum += Sn;
    }
	
	cout << sSum;

    system("PAUSE");
	return 0;
}


The "pow" still has a squiggly red line under it, and I still get the same error message:

IntelliSense: more than one instance of overloaded function "pow" matches the argument list
Last edited on
Ignore the intellisense. It's giving you a false warning. I just copied and compiled your code. No errors. What IDE are you using?

One little sidenote.

1
2
3
4
5
    for(int i = 1; i <= n; i++) //declare and define i here
    {                    
        Sn = ( n + pow(n,2) ) / 3;
        sSum += Sn;
    }
Last edited on
And don't forget to change sSum to 0
If the intellisense keeps giving you problems then just tweak the code like so

 
Sn = ( n + (n * n)) / 3;


Last edited on
Guess nobody has told you that you're doing integer division yet. If you divide two integers, the result will also be an integer. EG 5/2 = 2, not 2.5

EDIT: Ignore this post. Forgot that pow() returns a float.
Last edited on
@ thomasedler Thank you! You've been a big help.

I'm using Visual Studios 2010 Pro, btw, would that explain this issue?

I'm not even sure exactly what the difference between an IDE and a compiler is.

Also, what exactly is intellisense?
Sometimes if you just close VS and open it up again that will fix the error. Also, sometimes if you've made a change and haven't clicked to a different line, it still shows the error. Other times you can just run the build and it will compile even though there is something underlined red.

It happens to me every once in a while with VS.

The compiler is what translates the c++ into machine code.

An IDE is describes something like VS, CodeBlocks, or Ecliplse. (Integrated Development Environment.) Basically an all in one program.

Intellisense detects errors real-time. Probably also handles auto-complete and stuff, but I'm not certain.
Sometimes if I have issue with code blocks I delete all the files in the debug, the exe, and all the files in the project folder except the headers and cpps, basically everything except the cpps and the headers, then compile again.
This may be bad but it works for me if code::blocks starts messing up.
Last edited on
closed account (D80DSL3A)
To solve the mystery regarding the warning or error. There are several overloads for pow. There is pow(float, float) and pow(double, double) but no pow(int, int). When you give 2 ints the compoler doesn't know which way to cast the ints - as float or as double, so it can't figure out which version of pow to call.
Calling pow is overkill for squaring a number anyways. Just write n*n for the square of n.

@cam16
Did you figure out to use i instead of n in line 16 of your last code posted here yet?
Also make Sn and sSum type double. Write:
 
 sSum += ( i + i*i ) / 3.0;// the 3.0 solves the "integer division" problem 
Thx for the explanation of pow, fun2code.
Topic archived. No new replies allowed.