c++ sequence calculator x_n+1 = f (x_n), trouble with math functionsWhat are POD types in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhat is the effect of extern “C” in C++?What is the “-->” operator in C++?Why do we need virtual functions in C++?Undefined behavior and sequence pointsEasiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?setw within a function to return an ostream
Why do Russians almost not use verbs of possession akin to "have"?
Time complexity of an algorithm: Is it important to state the base of the logarithm?
Should I split timestamp parts into separate columns?
Why A=2 and B=1 in the call signs for Spirit and Opportunity?
Why is the Eisenstein ideal paper so great?
Is it legal to have an abortion in another state or abroad?
Is "vegetable base" a common term in English?
Can a UK national work as a paid shop assistant in the USA?
Why isn't 'chemically-strengthened glass' made with potassium carbonate? To begin with?
Storing voxels for a voxel Engine in C++
Why does splatting create a tuple on the rhs but a list on the lhs?
Heat lost in ideal capacitor charging
A burglar's sunglasses, a lady's odyssey
What is the use case for non-breathable waterproof pants?
Is keeping the forking link on a true fork necessary (Github/GPL)?
Why would a rational buyer offer to buy with no conditions precedent?
Are there "don't read X but Y" that differentiate between Tzere and Segol or Patach and Kamatz?
Cardio work for Muay Thai fighters
Why did Jon Snow do this immoral act if he is so honorable?
What are these clip-like things?
Are there historical examples of audiences drawn to a work that was "so bad it's good"?
Do copyright notices need to be placed at the beginning of a file?
Navigating a quick return to previous employer
How to teach an undergraduate course without having taken that course formally before?
c++ sequence calculator x_n+1 = f (x_n), trouble with math functions
What are POD types in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhat is the effect of extern “C” in C++?What is the “-->” operator in C++?Why do we need virtual functions in C++?Undefined behavior and sequence pointsEasiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?setw within a function to return an ostream
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
example of code
#include <iostream>
using namespace std;
//whitespace package
#include <iomanip>
#include <math.h>
using std::setw;
int main ()
// n is an array of 101 integers
double n[ 101 ];
double exponent=3;
double fraction=1/7;
// initialize elements of array n to 0
for ( int i = 1; i < 100; i++ )
n[ i ] = 0;
//what is input 1?
cout << "Enter x_1" << endl;
cin >> n[1];
//jam ni's into function and loop
for ( int i = 1; i < 100; i++ )
// set element at location i+1 to f(x)= (fraction)*((x)^exponent + 2)
n[ i + 1 ] = fraction*(pow( ((n[ i ]) ), exponent ) + 2);
//header
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 1; j < 100; j++ )
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
return 0;
output
Enter x_1
1
Element Value
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
....
where my expected output would be
Element Value
1 1
2 0.42857142857
3 0.29695960016
4 0.2894553405
5 0.28917883433
6 0.28916891514
7 0.28916855966
...
background: I'm trying to write a simple program that asks what your $x_1$ is and reports $x_1$ to $x_100$ given some series function calculator--like a sequence calculator where $x_n+1 = f(x_n)$. In this example, our function is (1/7)*((x)^3 + 2).
Can you all offer some resources for writing other functions? I have $x_n+1=f(x_n)=(1/7)*((x_n^3)+2)$ right now.
Whenever I look up c++ math functions I get things like how to use the absolute value function, or how to use my cpp file as a function itself, but not information on writing math functions like this.
c++
|
show 3 more comments
example of code
#include <iostream>
using namespace std;
//whitespace package
#include <iomanip>
#include <math.h>
using std::setw;
int main ()
// n is an array of 101 integers
double n[ 101 ];
double exponent=3;
double fraction=1/7;
// initialize elements of array n to 0
for ( int i = 1; i < 100; i++ )
n[ i ] = 0;
//what is input 1?
cout << "Enter x_1" << endl;
cin >> n[1];
//jam ni's into function and loop
for ( int i = 1; i < 100; i++ )
// set element at location i+1 to f(x)= (fraction)*((x)^exponent + 2)
n[ i + 1 ] = fraction*(pow( ((n[ i ]) ), exponent ) + 2);
//header
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 1; j < 100; j++ )
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
return 0;
output
Enter x_1
1
Element Value
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
....
where my expected output would be
Element Value
1 1
2 0.42857142857
3 0.29695960016
4 0.2894553405
5 0.28917883433
6 0.28916891514
7 0.28916855966
...
background: I'm trying to write a simple program that asks what your $x_1$ is and reports $x_1$ to $x_100$ given some series function calculator--like a sequence calculator where $x_n+1 = f(x_n)$. In this example, our function is (1/7)*((x)^3 + 2).
Can you all offer some resources for writing other functions? I have $x_n+1=f(x_n)=(1/7)*((x_n^3)+2)$ right now.
Whenever I look up c++ math functions I get things like how to use the absolute value function, or how to use my cpp file as a function itself, but not information on writing math functions like this.
c++
3
I stopped reading your question here " The program works but is still giving me some kind of weird error.". Could you clarify or remove this as erroring and working are usually mutually exclusive.
– Richard Critten
Mar 23 at 23:23
This asks for input. What are the input, actual output, and expected output of the Minimal, Complete, and Verifiable example? Can't you just set those in the code instead of asking me to type it in, since we're only dealing with fixing the problem?
– Kenny Ostrom
Mar 23 at 23:29
Well, the compiler does have a point. n[i+1] can be outside the array. It looks like you got confused about whether you were going to skip the first or last.
– Kenny Ostrom
Mar 23 at 23:32
3
One thing to watch out for is a numeric literal like 7 is assumed to be an integer. That makes 1/7 integer math. No fractions allowed. 1/7 = 0. 1.0/7 forces floating point math, so you'll get an answer more in line with your expectations. More good reading.
– user4581301
Mar 24 at 0:16
1
as @user4581301 said 1/7 = 0 because it is interpreted as integer division. To fix this just do ` fraction = 1.0 / 7; `
– alvinalvord
Mar 24 at 0:47
|
show 3 more comments
example of code
#include <iostream>
using namespace std;
//whitespace package
#include <iomanip>
#include <math.h>
using std::setw;
int main ()
// n is an array of 101 integers
double n[ 101 ];
double exponent=3;
double fraction=1/7;
// initialize elements of array n to 0
for ( int i = 1; i < 100; i++ )
n[ i ] = 0;
//what is input 1?
cout << "Enter x_1" << endl;
cin >> n[1];
//jam ni's into function and loop
for ( int i = 1; i < 100; i++ )
// set element at location i+1 to f(x)= (fraction)*((x)^exponent + 2)
n[ i + 1 ] = fraction*(pow( ((n[ i ]) ), exponent ) + 2);
//header
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 1; j < 100; j++ )
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
return 0;
output
Enter x_1
1
Element Value
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
....
where my expected output would be
Element Value
1 1
2 0.42857142857
3 0.29695960016
4 0.2894553405
5 0.28917883433
6 0.28916891514
7 0.28916855966
...
background: I'm trying to write a simple program that asks what your $x_1$ is and reports $x_1$ to $x_100$ given some series function calculator--like a sequence calculator where $x_n+1 = f(x_n)$. In this example, our function is (1/7)*((x)^3 + 2).
Can you all offer some resources for writing other functions? I have $x_n+1=f(x_n)=(1/7)*((x_n^3)+2)$ right now.
Whenever I look up c++ math functions I get things like how to use the absolute value function, or how to use my cpp file as a function itself, but not information on writing math functions like this.
c++
example of code
#include <iostream>
using namespace std;
//whitespace package
#include <iomanip>
#include <math.h>
using std::setw;
int main ()
// n is an array of 101 integers
double n[ 101 ];
double exponent=3;
double fraction=1/7;
// initialize elements of array n to 0
for ( int i = 1; i < 100; i++ )
n[ i ] = 0;
//what is input 1?
cout << "Enter x_1" << endl;
cin >> n[1];
//jam ni's into function and loop
for ( int i = 1; i < 100; i++ )
// set element at location i+1 to f(x)= (fraction)*((x)^exponent + 2)
n[ i + 1 ] = fraction*(pow( ((n[ i ]) ), exponent ) + 2);
//header
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 1; j < 100; j++ )
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
return 0;
output
Enter x_1
1
Element Value
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
....
where my expected output would be
Element Value
1 1
2 0.42857142857
3 0.29695960016
4 0.2894553405
5 0.28917883433
6 0.28916891514
7 0.28916855966
...
background: I'm trying to write a simple program that asks what your $x_1$ is and reports $x_1$ to $x_100$ given some series function calculator--like a sequence calculator where $x_n+1 = f(x_n)$. In this example, our function is (1/7)*((x)^3 + 2).
Can you all offer some resources for writing other functions? I have $x_n+1=f(x_n)=(1/7)*((x_n^3)+2)$ right now.
Whenever I look up c++ math functions I get things like how to use the absolute value function, or how to use my cpp file as a function itself, but not information on writing math functions like this.
c++
c++
edited Mar 24 at 0:38
ness
asked Mar 23 at 23:17
nessness
62
62
3
I stopped reading your question here " The program works but is still giving me some kind of weird error.". Could you clarify or remove this as erroring and working are usually mutually exclusive.
– Richard Critten
Mar 23 at 23:23
This asks for input. What are the input, actual output, and expected output of the Minimal, Complete, and Verifiable example? Can't you just set those in the code instead of asking me to type it in, since we're only dealing with fixing the problem?
– Kenny Ostrom
Mar 23 at 23:29
Well, the compiler does have a point. n[i+1] can be outside the array. It looks like you got confused about whether you were going to skip the first or last.
– Kenny Ostrom
Mar 23 at 23:32
3
One thing to watch out for is a numeric literal like 7 is assumed to be an integer. That makes 1/7 integer math. No fractions allowed. 1/7 = 0. 1.0/7 forces floating point math, so you'll get an answer more in line with your expectations. More good reading.
– user4581301
Mar 24 at 0:16
1
as @user4581301 said 1/7 = 0 because it is interpreted as integer division. To fix this just do ` fraction = 1.0 / 7; `
– alvinalvord
Mar 24 at 0:47
|
show 3 more comments
3
I stopped reading your question here " The program works but is still giving me some kind of weird error.". Could you clarify or remove this as erroring and working are usually mutually exclusive.
– Richard Critten
Mar 23 at 23:23
This asks for input. What are the input, actual output, and expected output of the Minimal, Complete, and Verifiable example? Can't you just set those in the code instead of asking me to type it in, since we're only dealing with fixing the problem?
– Kenny Ostrom
Mar 23 at 23:29
Well, the compiler does have a point. n[i+1] can be outside the array. It looks like you got confused about whether you were going to skip the first or last.
– Kenny Ostrom
Mar 23 at 23:32
3
One thing to watch out for is a numeric literal like 7 is assumed to be an integer. That makes 1/7 integer math. No fractions allowed. 1/7 = 0. 1.0/7 forces floating point math, so you'll get an answer more in line with your expectations. More good reading.
– user4581301
Mar 24 at 0:16
1
as @user4581301 said 1/7 = 0 because it is interpreted as integer division. To fix this just do ` fraction = 1.0 / 7; `
– alvinalvord
Mar 24 at 0:47
3
3
I stopped reading your question here " The program works but is still giving me some kind of weird error.". Could you clarify or remove this as erroring and working are usually mutually exclusive.
– Richard Critten
Mar 23 at 23:23
I stopped reading your question here " The program works but is still giving me some kind of weird error.". Could you clarify or remove this as erroring and working are usually mutually exclusive.
– Richard Critten
Mar 23 at 23:23
This asks for input. What are the input, actual output, and expected output of the Minimal, Complete, and Verifiable example? Can't you just set those in the code instead of asking me to type it in, since we're only dealing with fixing the problem?
– Kenny Ostrom
Mar 23 at 23:29
This asks for input. What are the input, actual output, and expected output of the Minimal, Complete, and Verifiable example? Can't you just set those in the code instead of asking me to type it in, since we're only dealing with fixing the problem?
– Kenny Ostrom
Mar 23 at 23:29
Well, the compiler does have a point. n[i+1] can be outside the array. It looks like you got confused about whether you were going to skip the first or last.
– Kenny Ostrom
Mar 23 at 23:32
Well, the compiler does have a point. n[i+1] can be outside the array. It looks like you got confused about whether you were going to skip the first or last.
– Kenny Ostrom
Mar 23 at 23:32
3
3
One thing to watch out for is a numeric literal like 7 is assumed to be an integer. That makes 1/7 integer math. No fractions allowed. 1/7 = 0. 1.0/7 forces floating point math, so you'll get an answer more in line with your expectations. More good reading.
– user4581301
Mar 24 at 0:16
One thing to watch out for is a numeric literal like 7 is assumed to be an integer. That makes 1/7 integer math. No fractions allowed. 1/7 = 0. 1.0/7 forces floating point math, so you'll get an answer more in line with your expectations. More good reading.
– user4581301
Mar 24 at 0:16
1
1
as @user4581301 said 1/7 = 0 because it is interpreted as integer division. To fix this just do ` fraction = 1.0 / 7; `
– alvinalvord
Mar 24 at 0:47
as @user4581301 said 1/7 = 0 because it is interpreted as integer division. To fix this just do ` fraction = 1.0 / 7; `
– alvinalvord
Mar 24 at 0:47
|
show 3 more comments
1 Answer
1
active
oldest
votes
To report x_1 to x_100 you don't need an array of 101 elements but an array of 100 elements, indexed from 0 inclusive to 100 exclusive. The index of the array is one less than the order of the element of the sequence starting at order 1.
You can use value initialization to set all the elements of the array to 0 in same statement than the declaration.
In C++ the result of division of two integers is an integer, this why your fraction number was 0. So to obtain a non-truncated value, one of the operands should be double (the other will be promoted to double by the compiler before the division).
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
constexpr size_t elems_len = 100;
int main()
double n[elems_len]; // value initialization, all elements of n are set to 0
double exponent = 3;
// one of the operands should be double to avoid integer division and truncation
double fraction = 1.0 / 7;
cout << "Enter x_1" << endl;
cin >> n[0];
for (size_t i = 1; i < elems_len; i++)
n[i] = fraction * (pow(n[i-1], exponent) + 2);
cout << "Element" << setw(13) << "Value" << endl;
cout << fixed << setprecision(11);
for (size_t i = 0; i < elems_len; i++)
cout << setw(7) << i + 1 << setw(25) << n[i] << endl;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55319262%2fc-sequence-calculator-x-n1-f-x-n-trouble-with-math-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To report x_1 to x_100 you don't need an array of 101 elements but an array of 100 elements, indexed from 0 inclusive to 100 exclusive. The index of the array is one less than the order of the element of the sequence starting at order 1.
You can use value initialization to set all the elements of the array to 0 in same statement than the declaration.
In C++ the result of division of two integers is an integer, this why your fraction number was 0. So to obtain a non-truncated value, one of the operands should be double (the other will be promoted to double by the compiler before the division).
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
constexpr size_t elems_len = 100;
int main()
double n[elems_len]; // value initialization, all elements of n are set to 0
double exponent = 3;
// one of the operands should be double to avoid integer division and truncation
double fraction = 1.0 / 7;
cout << "Enter x_1" << endl;
cin >> n[0];
for (size_t i = 1; i < elems_len; i++)
n[i] = fraction * (pow(n[i-1], exponent) + 2);
cout << "Element" << setw(13) << "Value" << endl;
cout << fixed << setprecision(11);
for (size_t i = 0; i < elems_len; i++)
cout << setw(7) << i + 1 << setw(25) << n[i] << endl;
add a comment |
To report x_1 to x_100 you don't need an array of 101 elements but an array of 100 elements, indexed from 0 inclusive to 100 exclusive. The index of the array is one less than the order of the element of the sequence starting at order 1.
You can use value initialization to set all the elements of the array to 0 in same statement than the declaration.
In C++ the result of division of two integers is an integer, this why your fraction number was 0. So to obtain a non-truncated value, one of the operands should be double (the other will be promoted to double by the compiler before the division).
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
constexpr size_t elems_len = 100;
int main()
double n[elems_len]; // value initialization, all elements of n are set to 0
double exponent = 3;
// one of the operands should be double to avoid integer division and truncation
double fraction = 1.0 / 7;
cout << "Enter x_1" << endl;
cin >> n[0];
for (size_t i = 1; i < elems_len; i++)
n[i] = fraction * (pow(n[i-1], exponent) + 2);
cout << "Element" << setw(13) << "Value" << endl;
cout << fixed << setprecision(11);
for (size_t i = 0; i < elems_len; i++)
cout << setw(7) << i + 1 << setw(25) << n[i] << endl;
add a comment |
To report x_1 to x_100 you don't need an array of 101 elements but an array of 100 elements, indexed from 0 inclusive to 100 exclusive. The index of the array is one less than the order of the element of the sequence starting at order 1.
You can use value initialization to set all the elements of the array to 0 in same statement than the declaration.
In C++ the result of division of two integers is an integer, this why your fraction number was 0. So to obtain a non-truncated value, one of the operands should be double (the other will be promoted to double by the compiler before the division).
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
constexpr size_t elems_len = 100;
int main()
double n[elems_len]; // value initialization, all elements of n are set to 0
double exponent = 3;
// one of the operands should be double to avoid integer division and truncation
double fraction = 1.0 / 7;
cout << "Enter x_1" << endl;
cin >> n[0];
for (size_t i = 1; i < elems_len; i++)
n[i] = fraction * (pow(n[i-1], exponent) + 2);
cout << "Element" << setw(13) << "Value" << endl;
cout << fixed << setprecision(11);
for (size_t i = 0; i < elems_len; i++)
cout << setw(7) << i + 1 << setw(25) << n[i] << endl;
To report x_1 to x_100 you don't need an array of 101 elements but an array of 100 elements, indexed from 0 inclusive to 100 exclusive. The index of the array is one less than the order of the element of the sequence starting at order 1.
You can use value initialization to set all the elements of the array to 0 in same statement than the declaration.
In C++ the result of division of two integers is an integer, this why your fraction number was 0. So to obtain a non-truncated value, one of the operands should be double (the other will be promoted to double by the compiler before the division).
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
constexpr size_t elems_len = 100;
int main()
double n[elems_len]; // value initialization, all elements of n are set to 0
double exponent = 3;
// one of the operands should be double to avoid integer division and truncation
double fraction = 1.0 / 7;
cout << "Enter x_1" << endl;
cin >> n[0];
for (size_t i = 1; i < elems_len; i++)
n[i] = fraction * (pow(n[i-1], exponent) + 2);
cout << "Element" << setw(13) << "Value" << endl;
cout << fixed << setprecision(11);
for (size_t i = 0; i < elems_len; i++)
cout << setw(7) << i + 1 << setw(25) << n[i] << endl;
answered Mar 24 at 14:06
Jérôme MignéJérôme Migné
20415
20415
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55319262%2fc-sequence-calculator-x-n1-f-x-n-trouble-with-math-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
I stopped reading your question here " The program works but is still giving me some kind of weird error.". Could you clarify or remove this as erroring and working are usually mutually exclusive.
– Richard Critten
Mar 23 at 23:23
This asks for input. What are the input, actual output, and expected output of the Minimal, Complete, and Verifiable example? Can't you just set those in the code instead of asking me to type it in, since we're only dealing with fixing the problem?
– Kenny Ostrom
Mar 23 at 23:29
Well, the compiler does have a point. n[i+1] can be outside the array. It looks like you got confused about whether you were going to skip the first or last.
– Kenny Ostrom
Mar 23 at 23:32
3
One thing to watch out for is a numeric literal like 7 is assumed to be an integer. That makes 1/7 integer math. No fractions allowed. 1/7 = 0. 1.0/7 forces floating point math, so you'll get an answer more in line with your expectations. More good reading.
– user4581301
Mar 24 at 0:16
1
as @user4581301 said 1/7 = 0 because it is interpreted as integer division. To fix this just do ` fraction = 1.0 / 7; `
– alvinalvord
Mar 24 at 0:47