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

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

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

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