Question

(C++ programming) Need help with homework. Write a program that can be used to gather statistical...

(C++ programming) Need help with homework.

Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps:

1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On the next n lines, there is an integer representing the number of hours per week each student played video games. Here is an example:

5 // There are 5 students

9 // 9 hours per week – student 1

4 // 4 hours per week – student 2

10 // 10 hours per week – student 3

4 // 4 hours per week – student 4

7 // 7 hours per week – student 5 Read the rest of the numbers from the file into the dynamically allocated array.


2). Sort the array in ascending order: 4 4 7 9 10
3). Write the sorted array to a file, one number per line, including the number of students on the first line (same format as the input file)

4). Calculate the average of the numbers in the array without the lowest and the highest values: (4 + 7 + 9) / 3 = 6.6 5). Display the number of students, the average, the lowest and highest values in a readable format of your choice (screen output).

6). Finally, release the memory and terminate the program.
Run the program using the following input files: 1. Input file name: videogamesurvey.txt 2. Input file name: gamestats.txt 3. Input file name: survey.txt

These are all function prototypes:

// Function prototypes
int *readSurveyData(string filename, int &n);
void insertionSort(int *pAry, int *pLast);
void writeArray(int *pAry, int *pLast);
void writeArray(string filename, int *pAry, int *pLast);
double calcAvg(int *pAry, int *pLast);
void displayStats(int n, double avg, int smallest, int largest);

Thank You.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int *readSurveyData(string filename, int &n);

void insertionSort(int *pAry, int *pLast);

void writeArray(int *pAry, int *pLast);

void writeArray(string filename, int *pAry, int *pLast);

double calcAvg(int *pAry, int *pLast);

void displayStats(int n, double avg, int smallest, int largest);

int main()

{

    string filename;

    int *data;

    int num_studs;

    cout << "Input filename: ";

    cin >> filename;

    data = readSurveyData(filename, num_studs);

    insertionSort(data, data + num_studs);

    writeArray(filename, data, data + num_studs);

    cout<<"Data from File: "<<endl;

    writeArray(data, data + num_studs);

    double avg = calcAvg(data, data + num_studs);

    displayStats(num_studs, avg, *data, *(data + num_studs-1));

    delete[] data;

    return 0;

}

int *readSurveyData(string filename, int &n)

{

    ifstream inFile;

    inFile.open(filename.c_str());

    if (inFile.fail())

    {

        cout << "Unable to open file" << endl;

        return NULL;

    }

    inFile >> n;

    int *data = new int[n];

    for (int i = 0; i < n; i++)

    {

        inFile >> data[i];

    }

    inFile.close();

    return data;

}

void insertionSort(int *pAry, int *pLast)

{

    int i = 1, key, j;

    while ((pAry + i) < pLast)

    {

        key = *(pAry + i);

        j = i - 1;

        while (j >= 0 && *(pAry + j) > key)

        {

            *(pAry + j + 1) = *(pAry + j);

            j = j - 1;

        }

        *(pAry + j + 1) = key;

        i++;

    }

}

void writeArray(int *pAry, int *pLast)

{

    cout<<"Number of Students: "<<(pLast-pAry)<<endl;

    int i=0;

    while((pAry+i)<pLast){

        cout<<"Student "<<(i+1)<<": "<<*(pAry+i)<<endl;

        i++;

    }

}

void writeArray(string filename, int *pAry, int *pLast)

{

    ofstream outFile;

    outFile.open(filename.c_str());

    outFile<<(pLast-pAry)<<endl;

    int i=0;

    while((pAry+i)<pLast){

        outFile<<*(pAry+i)<<endl;

        i++;

    }

    outFile.close();

}

double calcAvg(int *pAry, int *pLast)

{

    int i=1;

    int sum = 0;

    while((pAry+i)<pLast-1){

        sum += *(pAry+i);

        i++;

    }

    return (double)sum/(i-1);

}

void displayStats(int n, double avg, int smallest, int largest)

{

    cout<<"\nStats: "<<endl;

    cout<<"Number of Students: "<<n<<endl;

    cout<<"Average: "<<avg<<endl;

    cout<<"Smallest: "<<smallest<<endl;

    cout<<"Largest: "<<largest<<endl;

}

Add a comment
Know the answer?
Add Answer to:
(C++ programming) Need help with homework. Write a program that can be used to gather statistical...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that can be used to gather statistical data about the number of movies...

    Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should perform the following steps: A) Ask the user how many students were surveyed. An array of integers with this many elements should then be dynamically allocated. B) Allow the user to enter the number of movies each student saw into the array. C) Calculate and display the average, median, and mode of the values entered....

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • For this assignment, you are to write a program that does the following:  read temperatures...

    For this assignment, you are to write a program that does the following:  read temperatures from a file name data.txt into an array, and  after reading all the temperatures, output the following to the monitor: the average temperature, the minimum temperature, and the total number of temperatures read. In addition, the program must use the following functions: //precondition: fileName is name of the file to open, inFile is the input file //postcondition: inFile is opened. If inFile cannot...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • This is a java homework for my java class. Write a program to perform statistical analysis...

    This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT