Question

//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 namespace std;

//constants
const int CAP = 100;

//function prototypes
bool openFile(ifstream &);
void readData(ifstream &, int [], int &);
void printData(const int [], int);
void evenCount(const int[], int);
void insertItem(int[], 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);
//insert a few items
insertItem(list, size, 15, 5);
cout << "Inserted in position 5:" << endl;
printData(list, size);
insertItem(list, size, 101, 2);
cout << "Inserted in position 2:" << endl;
printData(list, size);
insertItem(list, size, 215, 12);
cout << "Inserted in position 12:" << endl;
printData(list, size);
//call the evenCount function
evenCount(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;
}


//insert an item (newNum) in the given position (newPos)
void insertItem(int list[], int &size, int newNum, int newPos)
{
//insert code here
}

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

// do comment if any problem arises

// code

#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 evenCount(const int[], int);

void insertItem(int[], 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);

    //insert a few items

    insertItem(list, size, 15, 5);

    cout << "Inserted in position 5:" << endl;

    printData(list, size);

    insertItem(list, size, 101, 2);

    cout << "Inserted in position 2:" << endl;

    printData(list, size);

    insertItem(list, size, 215, 12);

    cout << "Inserted in position 12:" << endl;

    printData(list, size);

    //call the evenCount function

    evenCount(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;

}

//insert an item (newNum) in the given position (newPos)

void insertItem(int list[], int &size, int newNum, int newPos)

{

    // move list to right

    for (int i = size; i > newPos; i--)

        list[i] = list[i - 1];

    // insert

    list[newPos] = newNum;

}

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

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

{

    int even = 0;

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

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

            even++;

    cout << "Even: " << even << endl;

}

Output:

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

  • #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:" <<...

  • 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...

  • 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...

  • 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:    ...

  • C++ Modify this program (or create your own) so that it performs the following tasks: 1....

    C++ Modify this program (or create your own) so that it performs the following tasks: 1. Insert the numbers into the list in sorted order (ascending). This will require you to keep the list sorted as you continue to insert new numbers. 2. When adding a new number, first check to see whether it is larger than the last number currently in the list. If it is, add it directly to the end of the list, i.e., do not traverse...

  • 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...

  • Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the...

    Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the source code (shown below) to output the following: Inserting elements to array list: The list contains 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Deleting elements: The list contains: 2 4 6 8 10 12 14 16 18 20 22 24 this is a programming problem in...

  • 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...

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