Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###General Comments:

General Comments:

In C++ prefer not to use using namespace std;
See: https://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

###Algorithm

Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

Style

###Style AA agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];

###General Comments:

In C++ prefer not to use using namespace std;
See: https://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

###Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

###Style A agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];

General Comments:

In C++ prefer not to use using namespace std;
See: https://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

Style

A agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

###General Comments:

In C++ prefer not to use using namespace std;
See: http://stackoverflow.com/q/1452721/14065 https://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

###Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

###Style A agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];

###General Comments:

In C++ prefer not to use using namespace std;
See: http://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

###Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

###Style A agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];

###General Comments:

In C++ prefer not to use using namespace std;
See: https://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

###Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

###Style A agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

###General Comments:

In C++ prefer not to use using namespace std;
See: http://stackoverflow.com/q/1452721/14065 and nearly every other C++ question on code review.

Prefer one line per variable.

unsigned int i, j, k;

Also try and give you variables meaningful names.

Technaiclly variable length arrays (VLA) are not part of C++ (they are an extension inherited from C99).

int a[n - m + 1];

Prefer to use std::vector instead.

Main is special and does not need a return

return 0; // If not used the compiler will plant `return 0`

I only use a return in main when there is a possibility of a failure being reported. Your code always returns true and you can indicate that the program is not expected to fail by not using any returns.

###Algorithm

A more efficient prime finding algorithm is Sieve of Eratosthenes

There are several questions already on this subject a quick search should find them.

###Style A agree with @Konrad Rudolph that the vertical spacing is a bit extreme.

Unlike @Kondrad I personally I like having all the braces {} even if they are not required as they help solve potential problems with macros (but these are rare nowadays). So there may be some compromise here.

I also like my braces horizontally aligned like you.
But this is usually a team decision and you don't get a choice (majority rule and being consistent with the code base is more important in terms of readability).

Sometime you can use a compromise:

 if (a[k] != j)
 { a[k] = 0; // I only do this if its a single liner.
 }
 // Alternatively
 a[k] = (a[k] != j) ? 0 : a[k];
lang-cpp

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