Question

In: Computer Science

1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of...

  1. 1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and

    another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std.

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib> //new g++ is stricter - it rquires it for exit



using namespace std;


int max(int a[],int dsize){
    
    if (dsize>0){
        int max=a[0];
        for(int i=0;i<dsize;i++){
            if(a[i]>max){
                max = a[i];
            }
        }
        return max;
    }
    return -9999999;
}
int min(int a[],int dsize){
    
    if (dsize>0){
        int min=a[0];
        for(int i=0;i<dsize;i++){
            if(a[i]<min){
                min = a[i];
            }
        }
        return min;
    }
    return -9999999;
}

void printArray(int array[],int dsize){
    for(int i=0;i<dsize;i++){
        cout << array[i];
        if(i<dsize-1) //to avoid printing a space after the last one
            cout << " ";
    }
    cout << endl;
}
void fillArray(int array[],int &dsize, ifstream &fin){
    
    int i=0;
    while(!fin.eof()){
        fin >> array[i];
        i++;
    }
    dsize =i;
    
}

int main(){
    ifstream fin;
    ofstream fout;
    string ifilename="/home/ec2-user/environment/quizzes/numbers.txt";
    
    int const asize=100;
    int a[asize];
    int dsize=0;
    
    fin.open(ifilename.c_str()); //make sure you use the c_str the new g++ uses it
    if(!fin){ cout<< "Error opening input file"<<endl;exit(1);}
    fillArray(a,dsize,fin);
    printArray(a,dsize);
    cout <<"Min Value:" << min(a,dsize) << endl;
    cout <<"Max Value:" << max(a,dsize) << endl;
    
    return 0;
}

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

--------------quiz3.cpp---------------

#include <iostream>
#include <string>
#include <fstream>
#include <cmath> //this header file is used for sqrt()
#include <cstdlib>   //new g++ is stricter - it rquires it for exit
using namespace std;

int max(int a[], int dsize)
{
   if (dsize > 0)
   {
       int max = a[0];
       for (int i = 0; i < dsize; i++)
       {
           if (a[i] > max){
               max = a[i];
           }
       }
       return max;
   }
   return -9999999;
}

int min(int a[], int dsize)
{
   if (dsize > 0)
   {
       int min = a[0];
       for (int i = 0; i < dsize; i++)
       {
           if (a[i] < min){
               min = a[i];
           }
       }
       return min;
   }
   return -9999999;
}

void printArray(int array[], int dsize)
{
   for (int i = 0; i < dsize; i++){
       cout << array[i];
       if (i < dsize - 1)   //to avoid printing a space after the last one
           cout << " ";
   }
   cout << endl;
}

void fillArray(int array[], int &dsize, ifstream &fin)
{
   int i = 0;
   while (!fin.eof()){
       fin >> array[i];
       i++;
   }
   dsize = i;
}


///////////////Updated code here/////////////////////////

//this function calculates mean of the array elements
double meanAvg(int array[], int arraySize)
{
   double sum = 0;           //initialize sum to 0
   for (int i = 0; i < arraySize; i++)
       sum = sum + array[i];         //calculate sum of all elements
   double mean = sum/arraySize;   //get the mean by dividing sum by arraySize
   return mean;                   //returns mean
}

//this function calculates standard deviation of arrayc elements
double std_deviation(int array[], int arraySize)
{
   double mean = meanAvg(array, arraySize);   //get the mean of array

   //Variance = ∑(array[i] – mean)2 / n
   double squareDiff = 0;      

   //get sum of squared differences from mean
   for (int i = 0; i < arraySize; i++)
       squareDiff = squareDiff + (array[i] - mean)*(array[i] - mean);

   double variance = squareDiff/arraySize; //calculate variance
   double std = sqrt(variance);   //standard deviation = sqrt of variance
   return std;                       //returns standard deviation
}

int main()
{
   ifstream fin;
   ofstream fout;
   //string ifilename = "/home/ec2-user/environment/bte320/numbers.txt";
   string ifilename = "numbers.txt"; //You can comment this line and use the above line

   int   const asize = 100;
   int a[asize];
   int dsize = 0;

   fin.open(ifilename.c_str());   //make sure you use the c_str the new g++ uses it
   if (!fin)
   {
       cout << "Error opening input file" << endl;
       exit(1);
   }
   fillArray(a, dsize, fin);
   printArray(a, dsize);
   cout << "Min Value:" << min(a, dsize) << endl;
   cout << "Max Value:" << max(a, dsize) << endl;

   //Updated code from here------------
   cout << "Mean :" << meanAvg(a, dsize) << endl;  
   cout << "Standard deviation :" << std_deviation(a, dsize) << endl;


   ofstream outfile1("myquiz3outf.txt");   //open output files for writing
   ofstream outfile2("myquiz3outc.txt");

   outfile1 << "Min Value:" << min(a, dsize) << endl;   //write output to files
   outfile1 << "Max Value:" << max(a, dsize) << endl;
   outfile1 << "Mean :" << meanAvg(a, dsize) << endl;
   outfile1 << "Standard deviation :" << std_deviation(a, dsize) << endl;

   outfile2 << "Min Value:" << min(a, dsize) << endl;
   outfile2 << "Max Value:" << max(a, dsize) << endl;
   outfile2 << "Mean :" << meanAvg(a, dsize) << endl;
   outfile2 << "Standard deviation :" << std_deviation(a, dsize) << endl;

   return 0;
}

----------Screenshots-----------------

----------Input--------------

----------Output--------------

--------------------------------------------------

Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. a. For the file myquiz3outf.txt, you will use an ofstream. b. For the file myquiz3outc.txt, you will capture the cout output in a file using linux commands:./quiz3 > myquiz3outc.txt string ifilename="/home/ec2-user/environment/quizzes/numbers.txt" I've posted...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. side note: ***for the string ifilename, the folder name that the fils are under is "bte320" and the "numbers.txt" that it is referencing is pasted below*** CODE: #include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it employee.h file #include <string> using namespace std; class Employee { private:    string name;    int ID, roomNumber;    string supervisorName; public:    Employee();       // constructor    void setName(string name_input);   ...
CPP 4-1 Complete the Payroll Register & Record the Employee Payroll Journal Entry (#1): Calculate Social...
CPP 4-1 Complete the Payroll Register & Record the Employee Payroll Journal Entry (#1): Calculate Social Security and Medicare tax for the below-listed employees of TCLH Industries, a manufacturer of cleaning products. None of the employees files as married filing separately for a year-end tax return. Zachary Fox does not make any voluntary deductions that impact earnings subject to federal income tax withholding or FICA taxes. He is married, claims two withholding allowances for both federal and state, and his...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘encryptECB(key, secret_message)’ that accepts key and secret_message and returns a ciphertext.          #Electronic Code Block AES algorithm, Encryption Implementation from base64 import b64encode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes secret_message = b" Please send me the fake passport..." password = input ("Enter password to encrypt your message: ") key= pad(password.encode(), 16) cipher...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
// If you modify any of the given code, the return types, or the parameters, you...
// If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // You are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of employees struct employeeList {    struct employee* employee;    struct employeeList* next; } *list = NULL;              ...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify...
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘decryptECB(key, ciphertext)’ that accepts key and ciphertext and returns a plaintext. from base64 import b64decode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.Padding import unpad # We assume that the password was securely shared beforehand password = input ("Enter the password to decrypt the message: ") key= pad(password.encode(), 16) ciphertext = input ("Paste the cipher...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT