All pastes #911926 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-02-21 00:34 UTC
rendered paste body
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<ctype.h>
#include<string>

using namespace std;

char selection;

char printMenu(void);
void array(float& obs, int& i, float x[]);
void bins(float& interl, float width, float& interu, int& i, float x[], float lower, float upper, float y[], int star, int& a, int c, int d, float starmax, float stars);
void input(float x[], int& i, float& obs, char input[], float& min, float& max, char answer);
void reset(int& i, float x[]);

int main (void) {
    int i=0;
    float interu;
    float interl;
    float width;
    float x[1000];
    float y[1000];
    float obs;
    float max;
    float min;
    float lower;
    float upper;
    int star;
    int a;
    int b;
    int c;
    int d;
    float starmax;
    float stars;
    char file[1000];
    char answer;

    printMenu();
    cin >> selection;
    while (selection != 'q' || selection != 'Q'){
        if (selection == '1') {
                cout << endl << "Enter the data (-1 to end): ";
            cin >> obs;
            max=obs;
            min=obs;
            while (obs!= -1){
                array( obs, i, x);
                cin>>obs;
                if (obs < min) min=obs;
                else if (obs > max) max=obs;
            }
            if (obs=-1) cout<< endl;
        }
        else if (selection == '2') {
            bins(interl, width, interu, i, x, lower, upper, y, star, a, c, d, starmax, stars);
        }
        else if (selection == '3') {
            reset (i, x);
            cout << endl;
        }
        else if (selection == '4'){
            input(x, i, obs, file, min, max, answer);
            cout << endl << "The number of observations in the data set = " << i << endl;
            cout << "The minimum value = " << min << endl;
            cout << "The maximum value = " << max << endl;
        }
        else if (selection == 'q' || selection == 'Q'){
            break;
        }
        else {
            cout << endl << endl << "Invalid choice! Please try again.";
            cout << endl;
        }
        printMenu();
        cin >> selection;
            }


}

char printMenu(void){
    cout << endl << "      CGS2425: A HISTOGRAM DRAWING PROGRAM" << endl << endl;
    cout << "1 - Enter data manually" << endl << endl;
    cout << "2 - Draw a histogram of the data" << endl<< endl;
    cout << "3 - Clear" << endl<< endl;
    cout << "4 - Read a data file" << endl << endl;
    cout << "Q - Quit the program" << endl << endl;
    cout << "  Enter choice: ";
    }

void array(float obs, int& i, float x[]){
    x[i]=obs;
    i++;
    }

void bins(float& interl, float width, float& interu, int& i, float x[], float lower, float upper, float y[], int star, int& a, int b, int c, int d, float starmax, float stars, float min, float max){

    cout << "The number of observations in the data set = " << (i+1) << endl;
    cout << "The minimum value = " << min << endl;
    cout << "The maximum value = " << max << endl;
    cout << "Approximate interval width to get 20 intervals = " << (max-min)/20 << endl;
    cout << endl << "Enter the lower limit, upper limit and the width of the interval." << endl;
    cin >> lower >> upper >> width;
    interl=lower;
    starmax=0;
    if ( width<=0||upper < lower||upper < min||lower > max) {
        cout << "Error! Invalid values were entered." << endl;

    }
    else if (lower<min && upper > max) {
        cout << "Error! Invalid values were entered." << endl;
    }
    else {
        for (b=0; interl<upper; b++){
            star=0;
            interu= lower+(width*(b+1));
            if (interu > upper) interu=upper;
            for (a=0; a==i; a++){
                if (x[a] < interu && x[a] >= interl){
                    star++;
                }
            }
            y[b]=star;
            if (star > starmax) starmax=star;
            interl= lower+(width*(b+1));
        }
        if (starmax > 50){
            stars=starmax/50;
            for (a=0; a==b; a++) {
                y[a]=y[a]/stars;
            }
        }
        cout << endl << "  Interval" << endl;
        cout << "  Start      Freq   Prob   Each * represents " << stars << " observations" << endl;
        for (c=0; c==b; c++){
            interl=lower+(width*c);
            cout<< interl << y[c] << (y[c]/i);
            for (d=0; d>=y[c]; d++){
                cout << "*";
            }
            cout << " " << endl;
        }
        cout << endl;
    }
}

void input(float x[], int& i, int obs, char file[], float& min, float& max, char answer) {
    ifstream infile;
    cout << endl << "Enter the name of the data file: ";
    cin >> file;
    infile.open(file);
    while (infile.fail()){
        cout << "Cannot open " << file << " !!!" << endl;
        cout << "Try again? (Y/N): ";
        cin >> answer;
        if (answer == 'Y' || answer == 'y'){
           cout << "Enter the name of the data file: ";
           cin >> file;
        }
        if (answer == 'n' || answer == 'N') break;
        infile.open(file);
    }
    while ( infile.eof()==false ){
        infile >> obs;
        x[i]=obs;
        i++;
        if (obs < min) min=obs;
        else if (obs > max) max=obs;
    }
    infile.close();
}
void reset(int& i, float x[1000]){
i=0;
cout << endl << "The data has been cleared!" << endl;
}