Followers 0

# Looking for ways to improve code...

## 8 posts in this topic

//*******************************************************************
// File:   CIT245_CJepson_Assignment_2a.cpp
// Author: cjepson
// Created on February 7, 2013, 6:49 PM
// Language: C++
// IDE: Netbeans IDE 7.2.1
// Compiler: /usr/include/c++/4.7/x86_64-linux-gnu
// Class: CIT245-BIN1 Data Structures and Programming C++
// Assignment: Assignment_2a
//*******************************************************************

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Used for system() function, to allow easy and bug free translation between OS.
#define WINDOWS 0
#define LINUX 1
#define SYSTEMOUTPUT WINDOWS //WINDOWS = cmd: pause / LINUX = cmd: sleep 1

int main() {
// Console commands between Linux/Windows differ, pause will not work on Linux
// This code identifies which OS is current in use, executes appropriate console command.
#if SYSTEMOUTPUT == WINDOWS
int sysOut = 0;
#define CHOSEN WINDOWS
#else
int sysOut = 1;
#define CHOSEN LINUX
#endif
// *** End of C++ Preprocessor Directives ***

int y = 0;
bool x = false;
//*** DECLARE VARIABLES HERE ***
int userInput;
int randResults;
int sum[12] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int sumRolled[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
int odds[12] = {10, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2 ,1};
int digits = 0;
double errors[12] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
srand(12);
//*** END OF VARIABLE DECLARATION ***

do {
//*** ENTER CODE HERE ***

// Recieve user input
cout << "Enter an integer value: ";
cin >> userInput;
cout << endl;

// Generate numbers user input number of times
for(int count = 0, cnt = 0; count <= userInput; count++, cnt++){
randResults = (rand( ) % 6 ) + (rand( ) % 6) + 1;
sumRolled[randResults]++;
}

// Acquire number of integers user inputed (e.g., 248 = 3).
sum[0] = userInput;
while (sum[0] != 0) {
sum[0] /= 10;
digits++;
}

// Recreate number based on integers keyed in.
// *** Critical for calculating the odds length.
sum[0] =
(digits == 1 ? 1 :
(digits == 2 ? 1 :
(digits == 3 ? 10 :
(digits == 4 ? 100 :
(digits == 5 ? 1000 :
(digits == 6 ? 10000 :
100000))))));

// Text output and formatting
cout << "Sum\t  #Rolled\tOdds\t%Error" << endl;
cout.precision(4); // Floating point precision
cout.setf(ios::fixed,ios::floatfield); // Aligns decimal point

for(int count = 1; count < 12; count++){
cout << setw(2) << right << sum[count] << ":\t";
cout << setw(9) << right << sumRolled[count] << setw(11) << right
<< odds[count] * sum[0] << setw(11) << right
<< abs(((float)sumRolled[count] / (float)(odds[count] * sum[0]) * 100) - 100)
<< endl;
}

// Resetting variable values for loop
userInput = 0;
randResults = 0;
digits = 0;
sum[0] = 0;

for(int count = 0; count < 12; count++){
sumRolled[count] = 0;
errors[count] = 0.0;
}

//*** END CODE HERE ***
do {
cout << "Do you wish to continue? (1 = yes / 0 = no): ";
cin >> y;
if (y == 0) {
x = true;
} else if (y > 1) {
}
} while (y > 1);
} while (x == false);

// Requires the C++ Preprocessor Directives
if (sysOut == 0) {
system("pause");
} else if (sysOut == 1) {
system("sleep 1");
}
return 0;
} // End of main()



Since I haven't been accepted to any hobbiest projects yet, I figured I'll learn something new here while I wait for someone to accept me.  Anyone want to hit this code for ways of improving performance (w/o elliminating features & functionality)?  The code is in C++.

0

##### Share on other sites

The application is IO bound; it spends most of it's time printing. So you can't really improve performance in a meaningful way.

1

##### Share on other sites

The application is IO bound; it spends most of it's time printing. So you can't really improve performance in a meaningful way.

Awesome.  Thank you sir.

0

##### Share on other sites

Just for the sake of being nit picky I would use precompiler directives to separate the wait so that your not wasting a comparison tick every time you want to wait.

#ifdef WIN32

system("pause");

#else

system("sleep 1");

#endif

Using this method the compiler will only include the correct call for the correct system in which it is compiled on.  In real world applications Windows programs are packaged as compiled executable's 99% of the time and Linux is always in source form.  Making this calculation once at compile time vs once per execution (or more if you where to use this again somewhere) doesn't really save much of anything it just gets you in the habit of what I consider a better practice.

::Edit::

I used #ifdef WIN32 because Microsoft's Visual Studio normally defines this for you by default when you have the sub system set to windows.  It could be defined as something different or you can use your own #define WINMODE or whatever you wish, just be sure that the compiler or the source have the correct platform defined for the build you are running.

Edited by Dan Mayor
2

##### Share on other sites

Just for the sake of being nit picky I would use precompiler directives to separate the wait so that your not wasting a comparison tick every time you want to wait.

#ifdef WIN32

system("pause");

#else

system("sleep 1");

#endif

Using this method the compiler will only include the correct call for the correct system in which it is compiled on.  In real world applications Windows programs are packaged as compiled executable's 99% of the time and Linux is always in source form.  Making this calculation once at compile time vs once per execution (or more if you where to use this again somewhere) doesn't really save much of anything it just gets you in the habit of what I consider a better practice.

::Edit::

I used #ifdef WIN32 because Microsoft's Visual Studio normally defines this for you by default when you have the sub system set to windows.  It could be defined as something different or you can use your own #define WINMODE or whatever you wish, just be sure that the compiler or the source have the correct platform defined for the build you are running.

This is awesome!  I've been looking for a way to automate this.  After Googling, I did see some stuff on it but couldn't get it to work.  Thanks a bunch!

0

##### Share on other sites

One idea is to not fork a new process just to wait for input from a user. Simply using std::cin.get() should suffice:


void wait() {
cout << "Press return key to continue...";
cin.get();
}


Bonus: this code is completely cross platform, and actually does the same thing on all platforms (in contrast, pause and sleep 1 do not do the same thing).

There are lots of non-performance related improvements. Some of your variable names are very poor, "x" and "y" in particular. Your use of the first element of the "sum" array as two unrelated temporary values is very confusing.

Overall, the intent behind the program is not really shining through the code. It is difficult to assess it's correctness without knowing what is supposed to be happening.

Never thought about that!  Thanks a bunch!  You do have some valid points and I can definately concor with your concerns. Two things learned from such a simple code post.  This is bloodly awesome.     I'm doing this more often. hahaha!

Edited by Subtle_Wonders
1

##### Share on other sites

On VS you can put breakpoint on last return:

return 0;


system("pause");


or

cout << "Press return key to continue...";
cin.get();

0

##### Share on other sites
I see two bugs:

1. Lines 57 and 58 do not agree.
Either the comment is wrong, or the for-loop is wrong. If I enter 2, for example, the loop runs with values of 0, then 1, then 2, for 'count'. That's three iterations. A typical correct loop uses less-than within the comparison.

2. Lines 65 to 68 are incorrect. The value "0" is one digit!
In the past I've found that the best way to count digits, is to use a do..while loop instead. Edited by iMalc
0

## Create an account

Register a new account