Question

In: Computer Science

//Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT...

//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

#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 5:" << endl;
        printData(list, size);
        insertItem(list, size, 215, 12);
        cout << "Inserted in position 5:" << 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
}

Solutions

Expert Solution

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 5:" << endl;
printData(list, size);
insertItem(list, size, 215, 12);
cout << "Inserted in position 5:" << 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)
{
int i;
for(i=size-1;i>=newPos;i--){
           list[i+1]=list[i];       //repositioning the elements in array
}
list[newPos]=newNum;
}

//count the even numbers in the list and output in this function
void evenCount(const int list[], int size)
{
int i,c=0;
for(i=0;i<size;i++){
           if(list[i]%2==0){       //checking number in array even or not
               c++;
           }
}
cout<<"even count is:"<<c;
}

code screenshot:


Related Solutions

**Please fill out the chart post at the bottom and please do not copy and paste...
**Please fill out the chart post at the bottom and please do not copy and paste a previous answer that has been used on CHegg. Lehighton Chalk Company manufactures sidewalk chalk, which it sells online by the box at $24 per unit. Lehighton uses an actual costing system, which means that the actual costs of direct material, direct labor, and manufacturing overhead are entered into work-in-process inventory. The actual application rate for manufacturing overhead is computed each year; actual manufacturing...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: ---the code can be general (can be tested with any int main() test function)--- Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: //...
The file has some functions incomplete/empty. Please read the description from each of those functions and...
The file has some functions incomplete/empty. Please read the description from each of those functions and add your code to complete. You also need to test those functions in the main(). ListDemoHw.java package hw; public class ListDemoHw {    public static void printLinkedList(SLLNode<Integer> node) {        // display all elements in the linked list        while(node != null) {            System.out.print(node.info + " ");            node = node.next; // move to the next node...
PLEASE ACTUALLY DO IT OUT! I TRIED TO DO THE BOTTOM ANSWERS AND KEPT GETTING WRONG...
PLEASE ACTUALLY DO IT OUT! I TRIED TO DO THE BOTTOM ANSWERS AND KEPT GETTING WRONG FOR LAST TWO. The heating coils in a hair dryer are 0.800 cm in diameter, have a combined length of 1.00 m, and a total of 375 turns. (a) What is their total self-inductance assuming they act like a single solenoid? H (b) How much energy is stored in them when 11.5 A flows? J (c) What average emf opposes shutting them off if...
Please fill in the blanks to make the following statements correct. Thanks. Money serves three functions:...
Please fill in the blanks to make the following statements correct. Thanks. Money serves three functions: ______ , ______ , and ______ . Suppose children at a summer camp are each given a credit of $20 at the snack shop, where purchases are recorded but no cash is exchanged. This is an example of money as a(n) ______ and a(n) ______ . Paper money and coins that are not convertible into anything with intrinsic value, but are declared by the...
IN PYTHON PLEASE Create a file lists.py that contains the following functions: sumOfOdd(intList) The parameter intList...
IN PYTHON PLEASE Create a file lists.py that contains the following functions: sumOfOdd(intList) The parameter intList is supposed to be a list of integers. The function returns the sum (addition) of the odd integers from intList, and leaves intList not modified For instance, given [1,2,3,4], the function returns 4. The function performs no I/O. productOfEven(intList) The parameter intList is supposed to be a list of integers. The function returns the product (multiplication) of the even integers from intList, and leaves...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT