Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Update:

#Update: II have noticed that the code uses the same names for global variables, local variables and function parameters. This makes things confusing and would be best avoided. For example, your code has a global variable:

#Update: I have noticed that the code uses the same names for global variables, local variables and function parameters. This makes things confusing and would be best avoided. For example, your code has a global variable:

Update:

I have noticed that the code uses the same names for global variables, local variables and function parameters. This makes things confusing and would be best avoided. For example, your code has a global variable:

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Putting using namespace std at the top of every program is a bad habit a bad habit that you'd do well to avoid.

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.

added 2172 characters in body
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

#Update: I have noticed that the code uses the same names for global variables, local variables and function parameters. This makes things confusing and would be best avoided. For example, your code has a global variable:

const int SIZE_OF_FIBONACCI_ARRAY = 20;

This is then used within main as:

fillFibonacciArray(SIZE_OF_FIBONACCI_ARRAY);

The code for that function is:

void fillFibonacciArray(int SIZE_OF_FIBONACCI_ARRAY)
{
 int i;
 Array[0] = { 0 };
 Array[1] = { 1 };
 for (int i = 2; i < SIZE_OF_FIBONACCI_ARRAY; i++)
 Array[i] = Array[i - 1] + Array[i - 2];
 return;
}

The problem is that there are two different things called SIZE_OF_FIBONACCI_ARRAY here, and I think the difference may be confusing you. Let's rewrite the the function a bit to make it easier to talk about:

void fillFibonacciArray(int mysize)
{
 int i;
 Array[0] = 0;
 Array[1] = 1; // note that we don't need braces here
 for (int i = 2; i < mysize; i++)
 Array[i] = Array[i - 1] + Array[i - 2];
 // we don't need an explicit return at the end of a void function
}

When this function is called as fillFibonacciArray(SIZE_OF_FIBONACCI_ARRAY), the formal parametermysize is replaced with the actual parameterSIZE_OF_FIBONACCI_ARRAY which was declared globally with the value of 20. What is confusing about your original code is that there is a formal parameter named SIZE_OF_FIBONACCI_ARRAY that then gets the actual parameter which just happens to have the same name. Unfortunately, the code then refers to Array which is a global variable. The effect is that instead of creating a series of 20 int values in the Fibonacci array, your code is actually putting 20 float values in the first 20 slots in Array and the Fibonacci array is never used. See this article on Wikipedia for a longer and more complete explanation of the terms formal parameter and actual parameter and what they mean.

#Update: I have noticed that the code uses the same names for global variables, local variables and function parameters. This makes things confusing and would be best avoided. For example, your code has a global variable:

const int SIZE_OF_FIBONACCI_ARRAY = 20;

This is then used within main as:

fillFibonacciArray(SIZE_OF_FIBONACCI_ARRAY);

The code for that function is:

void fillFibonacciArray(int SIZE_OF_FIBONACCI_ARRAY)
{
 int i;
 Array[0] = { 0 };
 Array[1] = { 1 };
 for (int i = 2; i < SIZE_OF_FIBONACCI_ARRAY; i++)
 Array[i] = Array[i - 1] + Array[i - 2];
 return;
}

The problem is that there are two different things called SIZE_OF_FIBONACCI_ARRAY here, and I think the difference may be confusing you. Let's rewrite the the function a bit to make it easier to talk about:

void fillFibonacciArray(int mysize)
{
 int i;
 Array[0] = 0;
 Array[1] = 1; // note that we don't need braces here
 for (int i = 2; i < mysize; i++)
 Array[i] = Array[i - 1] + Array[i - 2];
 // we don't need an explicit return at the end of a void function
}

When this function is called as fillFibonacciArray(SIZE_OF_FIBONACCI_ARRAY), the formal parametermysize is replaced with the actual parameterSIZE_OF_FIBONACCI_ARRAY which was declared globally with the value of 20. What is confusing about your original code is that there is a formal parameter named SIZE_OF_FIBONACCI_ARRAY that then gets the actual parameter which just happens to have the same name. Unfortunately, the code then refers to Array which is a global variable. The effect is that instead of creating a series of 20 int values in the Fibonacci array, your code is actually putting 20 float values in the first 20 slots in Array and the Fibonacci array is never used. See this article on Wikipedia for a longer and more complete explanation of the terms formal parameter and actual parameter and what they mean.

Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /