Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A data file has a set of integer values. The maximum number of values in the fil

ID: 3766372 • Letter: A

Question

A data file has a set of integer values. The maximum number of values in the file that are greater than or equal to 0 will be 30. Each value will be separated by at least one blank space and the last value in the file will be followed by a linefeed.

Design a C++ program with void functions that will:
Interactively prompt the user for the name of the input file and read it

open the file for reading using a C++ filestream

interactively prompt the user for the name of the output file and read it

open the file for writing using a C++ filestream

read the integer values from the file

count and sum the negative values and find the average.

store the other values (>=0) in an array, counting them as they are read

write the following to the output file you created - leave at least one blank line between each set of outputs and clearly label all output.

display the sum, count, and average of the negative values (display average with 3 digits to the right of the decimal) (if there were no negative values, do not display an average) (left justify labels and right justify numbers)

if there are values stored in the array: (if there are none, display an error message)

-use bubblesort to arrange the values in the array into ascending order

-list the values read into the array - right justified, 4 per line - values may have up to 7 digits

-the number of values stored in the array, the average of the values, the variance and the standard deviation of the values - (display average, variance and standard deviation with 3 digits to the right of the decimal) - left justify labels and right justify numbers

-list all the values in the array that are prime (4 per line, right justified)

-list each nonprime number greater than 0 found in the array along with how many factors it has (arrange in 2 columns - right justified), then display a message stating how many of these values were found

Definitions and Formulas

A prime number is a positive, nonzero integer that has exactly 2 different factors.

if n=# values stored in the array and datai represents the ith value in the array,

    average = (data1 + data2 + ... + datan) / n

    variance = ((data1-average)2 + (data2-average)2 + ... + (datan-average)2) / n

    standard_deviation = sqrt(variance)

Assumptions

The input file will not be empty. Each line in the file will be terminated by a linefeed (' ').

All values in the file will be integers and they will be separated by whitespace.

Maximum number of values in the file greater than or equal to 0 will be 30.

Maximum number of columns needed to display an integer will be 7.

Maximum number of columns needed to display a floating point value will be 15.

All values in the file could be negative.

All values in the file could be greater than or equal to 0.

Requirements

The program MUST make use of functions.

The program MUST PASS PARAMETERS to communicate values. No global variables are allowed.

Program must make use of filestream variables to represent the input and output files.

The program must prompt for the name of the input file first. Then prompt for the name of the output file.

Program must use a static array (do not use a variable for the size of the array).

Example session:


Input file data (called data):
51 144 5 16 8 0 -2 14
6 -4 173
263 11 9345 -135
777

Compiling and running the program:

g++ program.cpp
./a.out

Result:

Input file name? data

Output file name? results

[compiler keys]$ results

Negative Values
Count:                            3
Sum:                       -141.000
Average:                    -47.000

Values Stored in Array
       0       5       6       8
      11      14      16      51
     144     173     263     777
    9345

Statistics for Data in Array
Count:                           13
Average:                    831.769
Variance:               6081689.716
Standard Deviation:        2466.108

Primes Stored in Array
       5      11     173     263

Nonprime Factor
Number   Count
       6       4
       8       4
      14       4
      16       5
      51       4
     144      15
     777       8
    9345      16
Nonprime number (>0) count: 8

more moredata
-3 -5 -3
-2

./a.out

Input file name? moredata
Output file name? moreresults

[compiler keys]$ results


Negative Values
Count:                            4
Sum:                        -13.000
Average:                     -3.250

No values greater than or equal to 0 in file.

Here's a start to the code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(){

string filename = "";
int array[20];
int count = 0;
ifstream in_file;
ofstream out_file;

get_filename("input", filename);
in_file.open(filename.c_str());
get_filename("output", filename);
out_file.open(filename.c_str());

while(in_file){
count++;
in_file >> array[count];
}

}

(Not all the code may be correct.)

Explanation / Answer

Complete Program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int SIZE = 50;

void get_filename(string str, string &filename)
{
cout << str << " file name? : ";
cin >> filename;
}

void bubbleSort(int arr[], int count)
{
for(int i = 0; i < count - 1; i++)
{
  for(int j = 0; j < count - i - 1; j++)
  {
   if(arr[j] > arr[j + 1])
   {
    int temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp;
   }
  }
}
}

void compute(int arr[], int count, double sum, double &average, double &variance, double &standard_deviation)
{
if(count > 0)
{
  average = sum / count;

  for(int i = 0; i < count; i++)
   variance += pow((arr[i] - average), 2.0);

  variance = variance / count;

  standard_deviation = sqrt(variance);
}
}

int getPrimeFactorsCount(int num)
{
if(num <= 0)
  return 0;

int pfc = 0;

for(int i = 1; i <= num; i++)
{
  if(num % i == 0)
   pfc++;
}

return pfc;
}

int main()
{
string filename = "";
int arr[SIZE];
int number;

int negatives = 0;
double negativeSum = 0;
double negativeAverage = 0;

int count = 0;
double sum = 0;
double average = 0;
double variance = 0;
double standard_deviation = 0;

ifstream in_file;
ofstream out_file;

get_filename("input", filename);
in_file.open(filename);

if(!in_file)
{
  cout << filename << " file cannot be opened!" << endl;
  system("pause");
  return 1;
}

get_filename("output", filename);
out_file.open(filename);

in_file >> number;
while(in_file)
{
  if(number < 0)
  {
   negativeSum += number;
   negatives++;
  }
  else
  {
   arr[count] = number;
   sum += number;
   count++;
  }

  in_file >> number;
}

if(negatives > 0)
{
  negativeAverage = negativeSum / negatives;

  out_file << "Negative Values" << endl;
  out_file << setw(15) << left << "Count:" << setw(12) << right << setprecision(3) << fixed << negatives << endl;
  out_file << setw(15) << left << "Sum:" << setw(12) << right << setprecision(3) << fixed << negativeSum << endl;
  out_file << setw(15) << left << "Average:" << setw(12) << right << setprecision(3) << fixed << negativeAverage << endl;
  
}
else
{
  out_file << "No values less than 0 in file" << endl;
}

if(count > 0)
{
  bubbleSort(arr, count);

  out_file << " Values Stored in Array" << endl;
  for(int i = 0; i < count; i++)
  {
   if(i != 0 && i % 4 == 0)
    out_file << endl;

   out_file << setw(8) << fixed << arr[i];  
  }
  out_file << endl;

  compute(arr, count, sum, average, variance, standard_deviation);

  out_file << " Statistics for Data in Array" << endl;
  out_file << setw(20) << left << "Count:" << setw(12) << right << setprecision(3) << fixed << count << endl;
  out_file << setw(20) << left << "Sum:" << setw(12) << right << setprecision(3) << fixed << sum << endl;
  out_file << setw(20) << left << "Average:" << setw(12) << right << setprecision(3) << fixed << average << endl;
  out_file << setw(20) << left << "Variance:" << setw(12) << right << setprecision(3) << fixed << variance << endl;
  out_file << setw(20) << left << "Standard Deviation:" << setw(12) << right << setprecision(3) << fixed << standard_deviation << endl;

  out_file << " Primes Stored in Array" << endl;
  int primes = 0;
  for(int i = 0; i < count; i++)
  {
   if(getPrimeFactorsCount(arr[i]) == 2)
   {
    if(primes != 0 && primes % 4 == 0)
     out_file << endl;

    out_file << setw(8) << fixed << arr[i];

    primes++;
   }
  }

  out_file << endl << endl;
  out_file << setw(10) << left << "Nonprime" << setw(10) << "Factor" << endl;
  out_file << setw(10) << left << "Number" << setw(10) << "Count" << endl;
  int nonprimes = 0;
  for(int i = 0; i < count; i++)
  {
   int pfc = getPrimeFactorsCount(arr[i]);
   if(pfc > 2)
   {
    out_file << setw(10) << left << fixed << arr[i] << setw(6) << right << fixed << pfc << endl;
    nonprimes++;
   }
  }
  out_file << "Nonprime number (>0) count: " << nonprimes << endl;
}
else
{
  out_file << "No values greater than or equal to 0 in file." << endl;
}

in_file.close();
out_file.close();

system("pause");
return 0;
}

Input file: data.txt

51 144 5 16 8 0 -2 14
6 -4 173
263 11 9345 -135
777

Output on Console:

Output file: results.txt

Negative Values
Count:                    3
Sum:               -141.000
Average:            -47.000

Values Stored in Array
       0       5       6       8
      11      14      16      51
     144     173     263     777
    9345

Statistics for Data in Array
Count:                        13
Sum:                   10813.000
Average:                 831.769
Variance:            6081689.716
Standard Deviation:     2466.108

Primes Stored in Array
       5      11     173     263

Nonprime Factor   
Number    Count    
6              4
8              4
14             4
16             5
51             4
144           15
777            8
9345          16
Nonprime number (>0) count: 8