(correct answer gets all credit) C++ finding how many occurreces each value has
ID: 3716300 • Letter: #
Question
(correct answer gets all credit) C++ finding how many occurreces each value has in an array
program will compute a frequency distribution using a structure array
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 "<
Helpful information
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
Explanation / Answer
ANS:-
Given that,
PROGRAM:-
#include <fstream.h>
#include <iostream.h>
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
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 {
short* data=new short[size];
short a,i=0;
while(filein>>a)
{
data[i]=a;
i++;
}
return data;
} // the data will be read from a file not the keyboard
void display_array(short *data, short size)
{
for(short i=0;i<size;i++)
cout<<data[i];
}
Fdistribution *frequency_distribution(short *data, short size, short &sizeF)
{
Fdistribution *result=new Fdistribution[sizeF];;
result[0]->unique=data[0];
result[0]->count=1;
for(short i=1;i<size;i++)
{
short flag=0;
for(short k=0;k<l;k++)
{
if(result[k]->unique==data[i]) //check element already exists or not
flag=1;
break;
}
if(flag==0)
{
result[l]->unique=data[i]; // insert element because it does not exist already
result[l]->count=1;
l++;
}
else
{
result[k]->count++; //element already there. so increment the count.
}
}
return result;
}
void display_distribution(Fdistribution frequency[], short sizeF)
{
for (short i=0;i<sizeF;i++)
{
cout<<frequency[i].unique<<" "<<requency[i]..count<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.