Skip to main content
Code Review

Return to Answer

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

No of times condition gets checked:

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

Possible Improvements:

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.
  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    

Use standard C++

  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

No of times condition gets checked:

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

Possible Improvements:

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.
  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    

Use standard C++

  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

No of times condition gets checked:

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

Possible Improvements:

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.
  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    

Use standard C++

  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

added 784 characters in body
Source Link
Max Payne
  • 359
  • 5
  • 12

Conditions checking countNo of times condition gets checked:

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

Possible Improvements:

  • Small optimisations will not make significant difference in program efficiency.

    As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.
  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    
  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.

Conditions checking count

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

  • Small optimisations will not make significant difference in program efficiency.

  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    
  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.

No of times condition gets checked:

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

Possible Improvements:

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.
  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    
  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

added 784 characters in body
Source Link
Max Payne
  • 359
  • 5
  • 12

Conditions checking count

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

  • Small optimisations will not make significant difference in program efficiency.

  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     unsigned int sum = 0;
     num < 0 
    
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum << " ";sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    

Use standard C++

  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.

Conditions checking count

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

  • Small optimisations will not make significant difference in program efficiency.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     int row = 20, col = 10;
     unsigned int sum = 0;
     for( int i = 0; i < row; ++i) {
     for( int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum << " ";
     }
     std::cout << '\n';
     }
     return 0;
     }
    

Use standard C++

  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

Conditions checking count

  • In all your examples except second one, condition was checked maxrows*maxcols times.

  • In second example, checking is done 2maxrowsmaxcols times. While this is not a significant drawback, it certainly is a bit inefficient.

  • Small optimisations will not make significant difference in program efficiency.

  • Use type-modifiers like unsigned where-ever applicable. For example, the iteration variables are positive, so change their type to unsigned int.
    Perhaps you should not use unsigned for sum and num variables (refer code below), because number entered could be negative.

  • Lastly, although it doesn't make any significant changes, but addition is done more efficiently in computers than multiplication, so you can use repeated addition instead of multiplying everytime (note the sum += num) :

     #include <iostream>
     #include <iomanip>
     int main() {
     int num;
     std::cout << "Enter no: ";
     std::cin >> num;
     unsigned int row = 20, col = 10;
     int sum = 0;
     num < 0 
    
     for( unsigned int i = 0; i < row; ++i) {
     for( unsigned int j = 0; j < col; ++j) {
     sum += num;
     std::cout << std::setw(6) << sum;
     }
     std::cout << '\n';
     }
     return 0;
     }
    

Use standard C++

  • Instead of using non-standard getch() from conio.h, use cin.get() from iostream

  • Do not use using namespace std;. Refer link.
    If you want to use cin and cout, prepend them with their namespace, followed by scope resolution operator:
    std::cout << " "; and std::cin >> x;

  • As highlighted by other answers, strive for readable code. Optimisation is very important, but mainly if it causes significant changes to efficiency. Its better to put our concentration in improving algorithm and the big picture, rather than keep on tinkering with tiny details. That being said, adopting good habits, which provide us with legible and fairly efficient code, is in my opinion the way to go. Micro-optimisations often lead to cryptic code.

added 31 characters in body
Source Link
Max Payne
  • 359
  • 5
  • 12
Loading
Source Link
Max Payne
  • 359
  • 5
  • 12
Loading
lang-cpp

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