0

The code line cout.width(10); cout << right << (freq) /(n)*100 <<"\t"; specifically is always getting 0 as a value. I'm trying to get the relative frequency of the table. Please help and explain, I don't understand why it keeps getting 0 as an output even though freq and n has its respected values.

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
    int i, j, nci, n, freq = 0;
    float temp, min, max, range, interval, cibase;

    cout << "Enter the Sample Size: ";
    cin >> n;

    float x[n];
    cout << "Enter the Data: ";
    for(i = 0; i < n; ++i){
        cin >> x[i];
    }

    cout << endl;
    cout << "Number of Class Interval (6 - 20): ";
    cin  >> nci;

    if(nci >= 5 && nci <= 20){
        for(i = 0; i < n; ++i){
            for(j = 1 + i; j < n; ++j){
                if(x[i] > x[j]){
                    temp = x[i];
                    x[i] = x[j];
                    x[j] = temp;
                }
            }
        }

        cout << endl;

        min = x[0];
        max = x[n - 1];
        range = max - min;
        interval = ceil(range / nci);

        cout << " Class Interval \t";
        cout << " Freq \t";
        cout << "Xi \t" ;
        cout << " Class Boundary \t";
        cout << " Class Mark " << endl;

        cibase = min;
        while(cibase <= max){
            cout.width(5); cout << right << cibase << " - ";
            cout.width(4); cout << right << (cibase - 1) + interval << "\t\t";
            freq = 0;
            for(i = 0; i < n; ++i){
                if(x[i] >= cibase && x[i] <= ((cibase - 1) + interval)){
                    ++freq;
                }
            }
           
            
            
            cout.width(4); cout << right << freq << "\t";
            cout.width(7); cout << right <<(cibase + interval/2) -.5 <<"\t";
            cout.width(6); cout << right << cibase - 0.5 << " - ";
            cout.width(5); cout << right << (cibase - 0.5) + interval << "\t\t";
            cout.width(9); cout << right << (cibase + ((cibase - 1) + interval)) / 2 << "\t";
            cout.width(10); cout << right << (freq) /(n)*100 <<"\t";
            cout <<  "\n" << endl;
            cibase = cibase + interval;
        }

        cout.width(6); cout << "Minimum: " << setprecision(5) << right << min << endl;
        cout.width(6); cout << "Maximum: " << setprecision(5) << right << max << endl;
        cout.width(6); cout << "Range: " << setprecision(5) << right << range << endl;
        cout.width(6); cout << "Interval: " << setprecision(5) << right << interval << endl;
    }
    else{
        cout << "ERROR: Choose Between 7 and 20" << endl;
        cout << "Try Again!" << endl;
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Miks
  • 9
  • 2

1 Answers1

1

short answer: do replace 100 with 100.0

(freq) /(n)*100.0

long answer:

dividing integer data types produce an integer as data type too, so if you do A/B and B>A and both B and A are integers, the result is 0

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97