Question

In: Computer Science

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 this question before but there was a mistake in the original file and I've been given different instructions
I'll also have to save my outputs using terminals in cloud9, so any help or advice with that would be great!

CODE:

#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;
}

OUTPUT:

55 9870 33 28 44 77 8756 312 2 78 
Min Value:2
Max Value:9870
Average:1925.50
Std:3703.04

ARRAY FILE(numbers.txt):

55
9870
33
28
44
77
8756
312
2
78

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

code

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
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)
            cout << " ";
    }
    cout << endl;
}

void fillArray(int array[], int &dsize, ifstream &fin)
{
    int i = 0;
    while (!fin.eof())
    {
        fin >> array[i];
        i++;
    }
    dsize = i;
}
double meanAvg(int array[], int arraySize)
{
    double sum = 0;
    for (int i = 0; i < arraySize; i++)
        sum = sum + array[i];
    double mean = sum / arraySize;
    return mean;
}

double std_deviation(int array[], int arraySize)
{
    double mean = meanAvg(array, arraySize);

    double squareDiff = 0;
    for (int i = 0; i < arraySize; i++)
        squareDiff = squareDiff + (array[i] - mean) * (array[i] - mean);

    double variance = squareDiff / arraySize;
    double std = sqrt(variance);
    return std;
}

int main()
{
    ifstream fin;
    ofstream fout;
    string ifilename = "numbers.txt";
    int const asize = 100;
    int a[asize];
    int dsize = 0;

    fin.open(ifilename.c_str());
    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;

    cout << "Mean :" << meanAvg(a, dsize) << endl;
    cout << "Standard deviation :" << std_deviation(a, dsize) << endl;

    ofstream outfile1("myquiz3outf.txt");
    ofstream outfile2("myquiz3outc.txt");

    outfile1 << "Min Value:" << min(a, dsize) << endl;
    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;
}


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. 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++...
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of...
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]; }...
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);   ...
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...
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...
// 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,...
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...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT