Question

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 number 
such that the new number is one less than the even number.  Then return the new size.  
You do not need to worry about size going past CAP at this time.  
Please see my samples in the document and run the test a few times 
since my list is randomly generated.*/

int insertNum(int list[], int size);

/*This function counts the number of odd numbers in the list and returns to main.*/

int oddCount(int array[], int size);

int main()
{
        //DO NOT CHANGE MAIN
        int list[CAP], size = 0, count = 0;
        size = buildList(list, size);
        cout << "Original List!" << endl;
        printList(list, size);
        size = insertNum(list, size);
        cout << "List after inserts!" << endl;
        printList(list, size);
        count = oddCount(list, size);
        cout << "Number of odds = " << count << endl;
        return 0;
}

//function to build list.  DO NOT CHANGE THIS
int buildList(int list[], int size)
{
        srand(time(NULL));
        size = 10;
        for (int i = 0; i < size; i++)
        {
                list[i] = rand() % 100;
        }
        return size;
}

//function to print list.  DO NOT CHANGE THIS
void printList(int list[], int size)
{
        for (int i = 0; i < size; i++)
        {
                cout << list[i] << endl;
        }
}

//implement the missing functions here.

int insertNum(int list[], int size)
{
        return 0;
}

//deletes number at given position and returns new size.
int oddCount(int array[], int size)
{
        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// do comment if any problem arises

// code

#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 number

such that the new number is one less than the even number.  Then return the new size.  

You do not need to worry about size going past CAP at this time.  

Please see my samples in the document and run the test a few times

since my list is randomly generated.*/

int insertNum(int list[], int size);

/*This function counts the number of odd numbers in the list and returns to main.*/

int oddCount(int array[], int size);

int main()

{

    //DO NOT CHANGE MAIN

    int list[CAP], size = 0, count = 0;

    size = buildList(list, size);

    cout << "Original List!" << endl;

    printList(list, size);

    size = insertNum(list, size);

    cout << "List after inserts!" << endl;

    printList(list, size);

    count = oddCount(list, size);

    cout << "Number of odds = " << count << endl;

    return 0;

}

//function to build list.  DO NOT CHANGE THIS

int buildList(int list[], int size)

{

    srand(time(NULL));

    size = 10;

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

    {

        list[i] = rand() % 100;

    }

    return size;

}

//function to print list.  DO NOT CHANGE THIS

void printList(int list[], int size)

{

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

    {

        cout << list[i] << endl;

    }

}

//implement the missing functions here.

int insertNum(int list[], int size)

{

    // iterate over array

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

    {

        // if odd found

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

        {

            // shift list to right

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

                list[j] = list[j - 1];

            size++;

            // add 1 less than even

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

            i++;

        }

    }

    return size;

}

//deletes number at given position and returns new size.

int oddCount(int array[], int size)

{

    int odd = 0;

    // iterate over array and count odd

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

        if (array[i] % 2 != 0)

            odd++;

    return odd;

}

Output:

Add a comment
Know the answer?
Add Answer to:
c++ /*This is the starter file for your final proficiency test This program has a function...
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 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>...

  • //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++ Create a function called countOccurences that will count how many times a certain number occurs...

    C++ Create a function called countOccurences that will count how many times a certain number occurs in the array. The function should take an array, a size and a variable, which is what you are searching for. Heres what I got so far: #include <iostream> #include <string> #include <vector> using namespace std; void fillArray(int a[], int size); void printArray(int a[], int size); int countEvens(int ar[], int size); int main() {    int ar[10];    fillArray(ar, 10);    printArray(ar, 10);   ...

  • Please help me finish my code. Before the final pause in the main function, create an...

    Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

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