Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualSubtle_Wonders

Posted 14 February 2013 - 03:27 AM

//*******************************************************************
// 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) {
                cout << "Please try again!\n";
            }
        } 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++.


#1Subtle_Wonders

Posted 14 February 2013 - 02:59 AM

//*******************************************************************
// 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) {
                cout << "Please try again!\n";
            }
        } 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++.


PARTNERS