Question: C++ Frequency Distribution Program program will compute a frequency distribution using a structure array with subscripts program will dynamically allocate memory and read data from

C++ Frequency Distribution Program

program will compute a frequency distribution using a structure array with subscripts

program will dynamically allocate memory and read data from a file

file may contain multiple data sets to process

Data array will be dynamically allocated and deleted; pointers used in all functions except for

finding the frequency distribution using a structure array and printing the structure array

*/

#include // header file to read an external file containing data

#include

#include

#include

using namespace std;

struct Fdistribution // Fdistribution becomes a type declarator

{

short unique, count; // two values for each instance of this structure variable

} ;

short * allocate_array_and_read_file(short size, ifstream &filein);

void display_array(short *data, short size);

Fdistribution *frequency_distribution(short *data, short size, short &sizeF);

void display_distribution(Fdistribution frequency[], short sizeF);

int main()

{

short *data, // pointer to dynamically allocated memory

size, // number of elements in dynamically allocated memory

sizeF, // number of values stored in structure array

datasetNum = 1; // number of data set currently processing

Fdistribution *frequency; // pointer to structure array for frequency distribution

ifstream filein("dataset8.txt"); // open file and set file pointer to file location on C drive

// NOTE - data file must be in same folder as cpp and/or exe file

while(filein>>size) // loop continues while data in file

{

data = allocate_array_and_read_file(size, filein);

cout<<" Data set "<

display_array(data, size);

cout<<" Program will compute a frequency distibution ";

frequency = frequency_distribution(data, size, sizeF);

display_distribution(frequency, sizeF);

delete [] data; // delete statement does not change value of pointer

data = NULL;

delete [] frequency;

frequency = NULL;

}

cout<<" program done ";

//system("pause");

//return 0;

}

short * allocate_array_and_read_file(short size, ifstream &filein) // function to dynamically allocate and initialize array

{ /* you write the code */} // the data will be read from a file not the keyboard

void display_array(short *data, short size)

{ /* you write the code */}

Fdistribution *frequency_distribution(short *data, short size, short &sizeF)

{ /* you write the code */}

void display_distribution(Fdistribution frequency[], short sizeF)

{ /* you write the code */}

More information about program functions

Write a void function, frequency_distribution, to store all unique values and their frequencies in a structure array using the data stored in the data array. The structure array is a dynamically allocated array that you will allocate in this function to have the same size as the data array. You can use subscripts to access this array. Be sure that the function assigns a value to the variable, sizeF, so that the number of values stored in the structure array is known to other functions that access this array

Write a void function, display_distribution, to print the values stored in the structure array in two columns. The first column will be the unique value and the second column will be the frequency of the value. There will be only two values on each line of the display. You can use subscripts to access the structure array in this function.

Example: data set contains the following values: 1, 2, 7, 3, 11, 2, 4, 5, 0, 8, 8, 8, 11, 4, 3, 4, 1, 1, 2, 2, 2, 5, 9

Output would be:

Value Frequency

1 3

2 5

7 1

3 2

11 2

4 3

5 2

0 1

8 3

9 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!