Question

//This program is your final practice exam. //Please fill in the functions at the bottom of...

//This program is your final practice exam.
//Please fill in the functions at the bottom of the file.  (sum and removeItem)
//DO NOT CHANGE ANYTHING ELSE.
//main has all the code needed to test your functions.  Once your functions are written, please build and make sure it works fine
//Note that in this case, the list is not sorted and does not need to be.  Your goal is to insert the number in the given position.

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

//constants
const int CAP = 100;

//function prototypes
bool openFile(ifstream &);
void readData(ifstream &, int [], int &);
void printData(const int [], int);
void sum(const int[], int);
void removeItem(int[], int &, int);


int main()
{
        ifstream inFile;
        int list[CAP], size = 0;

        if (!openFile(inFile))
        {
                cout << "Program terminating!! File not found!" << endl;
                return -1;
        }
        //read the data from the file
        readData(inFile, list, size);
        inFile.close();
        cout << "Data in file:" << endl;
        printData(list, size);
        //remove a few items
        removeItem(list, size, 9);
        printData(list, size);
        removeItem(list, size, 200);
        printData(list, size);
        removeItem(list, size, 15);
        printData(list, size);
        //call the sum function
        sum(list, size);
        //end program
        cin.ignore(100, '\n');
        cout << "Press any key to continue...";
        getchar();

        return 0;
}

//function to open file
bool openFile(ifstream &inFile)
{
        inFile.open("numbers.txt");
        if (!inFile)
        {
                return false;
        }
        return true;
}

//reads the data from the file
void readData(ifstream &inFile, int list[], int &size)
{
        while (!inFile.eof())
        {
                inFile >> list[size++];
        }
}

//print the contents of the array
void printData(const int list[], int size)
{
        for (int i = 0; i < size; i++)
        {
                cout << list[i] << endl;
        }
        cout << endl;
}


//remove an item (delNum) and return the number removed to main.  If number does not exist, return -1.
void removeItem(int list[], int &size, int delNum)
{
        //insert code here
}

//count the even numbers in the list and output in this function
void sum(const int list[], int size)
{
        //insert code here
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

//This program is your final practice exam.

//Please fill in the functions at the bottom of the file.  (sum and removeItem)

//DO NOT CHANGE ANYTHING ELSE.

//main has all the code needed to test your functions.  Once your functions are written, please build and make sure it works fine

//Note that in this case, the list is not sorted and does not need to be.  Your goal is to insert the number in the given position.

#include <iostream>

#include <fstream>

using namespace std;

//constants

const int CAP = 100;

//function prototypes

bool openFile(ifstream &);

void readData(ifstream &, int[], int &);

void printData(const int[], int);

void sum(const int[], int);

void removeItem(int[], int &, int);

int main()

{

    ifstream inFile;

    int list[CAP], size = 0;

    if (!openFile(inFile))

    {

        cout << "Program terminating!! File not found!" << endl;

        return -1;

    }

    //read the data from the file

    readData(inFile, list, size);

    inFile.close();

    cout << "Data in file:" << endl;

    printData(list, size);

    //remove a few items

    removeItem(list, size, 9);

    printData(list, size);

    removeItem(list, size, 200);

    printData(list, size);

    removeItem(list, size, 15);

    printData(list, size);

    //call the sum function

    sum(list, size);

    //end program

    cin.ignore(100, '\n');

    cout << "Press any key to continue...";

    getchar();

    return 0;

}

//function to open file

bool openFile(ifstream &inFile)

{

    inFile.open("numbers.txt");

    if (!inFile)

    {

        return false;

    }

    return true;

}

//reads the data from the file

void readData(ifstream &inFile, int list[], int &size)

{

    while (!inFile.eof())

    {

        inFile >> list[size++];

    }

}

//print the contents of the array

void printData(const int list[], int size)

{

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

    {

        cout << list[i] << endl;

    }

    cout << endl;

}

//remove an item (delNum) and return the number removed to main.  If number does not exist, return -1.

void removeItem(int list[], int &size, int delNum)

{

    for(int i=0; i<size; i++){

        if(list[i]==delNum){

            for(int j=i; j<size-1; j++){

                list[j] = list[j+1];

            }

            size--;

        }

    }

}

//count the even numbers in the list and output in this function

void sum(const int list[], int size)

{

    int result = 0;

    for(int i=0; i<size; i++){

        if(list[i]%2==0){

            result += list[i];

        }

    }

    cout<<"Sum: "<<result<<endl;

}

Add a comment
Know the answer?
Add Answer to:
//This program is your final practice exam. //Please fill in the functions at the bottom of...
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
  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • c++ program that prints joke and its punchline

    nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

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