#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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?
#include <iostream>
#include <math.h>
usingnamespace 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
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.
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