Question

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

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

  1. Write a void function that prints the list of nonnegative integers in reverse.
  1. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the number of such numbers found.

  1. Write a function that determines the largest value stored in the array and returns that value.

  1. Write a function that determines the smallest values stored in the array and returns that value.
  1. Write the function calls with the appropriate arguments in the function main.
  1. Write appropriate comments for each function similar to the ones provided.

//-----------------------------------------------------------------------

//

//add the block comment for the program

//

//-----------------------------------------------------------------------

#include <iostream> //for cin, cout

using namespace std;

const int MAX_SIZE = 20;         // maximum size of array

//function prototypes

void ReadList( int[], int&);  

void PrintList(const int[], int);     

int main()

{

    int list[MAX_SIZE];         // array of nonnegative integers

    int numberOfIntegers;       // number of nonnegative integers in array

    ReadList(list, numberOfIntegers);

    PrintList(         ,                 );

                    .

        to be completed

         

   Call the functions     

    //output your name etc.

    system(“pause”);

    return 0;

}

//***************************************************************************

// Definition of function ReadList.

// This function reads nonnegative integers from the keyboard into an array.

// The parameter list is an array to hold nonnegative integers read.

// The parameter length is a reference parameter to an int. It holds the

// number of nonnegative integers read.

//***************************************************************************

void ReadList( int list[], int& length)

{

       int number;        

       int index = 0;

       cout << "Enter nonnegative integers each separated by a blank space,\n"

            << " and mark the end of the list with a negative number: ";

       cin >> number;    //read the first integer entered

       //check that the number is nonnegative and

       // the size of the array is not exceeded

       while (number >=0 && index < MAX_SIZE)

     {

            list[index] = number;   //store the integer in the array

                         ;        // increment the index

                          ;    // read the next integer         

       }

       length = index; // length is the number of nonnegative integers

                        //    in the list

}

//***************************************************************************

// Definition of function PrintList.

// The function prints the nonnegative integers in the array list and the

// number of integers in the array.

// The parameter list holds the nonnegative integers

// The parameter length holds the number of nonnegative integers.

//***************************************************************************

void PrintList(const int list[], int _______)

{

   int index;

   cout << "\nThe list contains " << length

         << " nonnegative integer(s) as follows: \n";

       To be completed

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//-----------------------------------------------------------------------
// This program reads some non negative inetegrs, store them in array
// and then print them in sequence.
//-----------------------------------------------------------------------

#include <iostream> //for cin, cout

using namespace std;

const int MAX_SIZE = 20;          // maximum size of array

//function prototypes
void ReadList( int[], int&);  
void PrintList(const int[], int);     

void NonNegReverse(const int arr[], int size) {
        for(int i=size-1; i >=0; i--) {
                if(arr[i] >= 0) {
                        cout << arr[i] << " ";
                }
        }
        cout << endl;
}
void GreaterThan10(const int arr[], int size) {
        int count = 0;
        for(int i=0; i <size; i++) {
                if(arr[i] > 10) {
                        cout << arr[i] << " ";
                        count++;
                }
        }
        cout << endl;
        cout << "Count: " << count << endl;
}
int Largest(const int arr[], int size) {
        int max = arr[0];
        for(int i=0; i <size; i++) {
                if(arr[i] > max) {
                        max = arr[i];
                }
        }
        return max;
}

int Smallest(const int arr[], int size) {
        int min = arr[0];
        for(int i=0; i <size; i++) {
                if(arr[i] < min) {
                        min = arr[i];
                }
        }
        return min;
}



int main() {

        int list[MAX_SIZE];      // array of nonnegative integers
        int numberOfIntegers;       // number of nonnegative integers in array

        ReadList(list, numberOfIntegers);

        PrintList(list, numberOfIntegers);
        //output your name etc.
        //system(“pause”);

        cout << "Non negative numbers in reverse: ";
        NonNegReverse(list, numberOfIntegers);

        cout << "Numbers greater than 10: ";
        GreaterThan10(list, numberOfIntegers);

        cout << "Largest number: " << Largest(list, numberOfIntegers) << endl;

        cout << "Smallest number: " << Smallest(list, numberOfIntegers) << endl;

        return 0;

}

//***************************************************************************
// Definition of function ReadList.
// This function reads nonnegative integers from the keyboard into an array.
// The parameter list is an array to hold nonnegative integers read.
//The parameter length is a reference parameter to an int. It holds the
// number of nonnegative integers read.
//***************************************************************************
void ReadList(int list[], int& length) {
        int number;
        int index = 0;

        cout << "Enter nonnegative integers each separated by a blank space,\n"
        << " and mark the end of the list with a negative number: ";

        cin>> number;    //read the first integer entered

        //check that the number is nonnegative and
        // the size of the array is not exceeded
        while (number >=0 && index < MAX_SIZE){
                list[index] = number;   //store the integer in the array
                index += 1;        // increment the index
                cin>> number;    // read the next integer         
        }

        length = index; // length is the number of nonnegative integers
                        //    in the list
}

//***************************************************************************
// Definition of function PrintList.
// The function prints the nonnegative integers in the array list and the
// number of integers in the array.
// The parameter list holds the nonnegative integers
// The parameter length holds thenumber of nonnegative integers.
//***************************************************************************

void PrintList(const int list[],int length) {

        int index;

        cout<< "\nThe list contains " << length     << " nonnegative integer(s) as follows: \n";

        for(index=0; index<length; index++) {
                cout << list[index] << " ";
        }
        cout << endl;


}
Add a comment
Know the answer?
Add Answer to:
Consider the following program that reads a number of nonnegative integers into an array and prints...
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 has an array of floating point numbers as a private data member of a...

    This program has an array of floating point numbers as a private data member of a class. The data file contains floating point temperatures which are read by a member function of the class and stored in the array. Exercise 1: Why does the member function printList have a const after its name but getList does not? Exercise 2: Fill in the code so that the program reads in the data values from the temperature file and prints them to...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • Consider the following program that reads students’ test scores into an array and prints the contents of the array. You will add more functions to the program. The instructions are given below.

    Consider the following program that reads students’ test scores into an array and prints the contents of the array.   You will add more functions to the program.  The instructions are given below.              For each of the following exercises, write a function and make the appropriate function call in main.Comments are to be added in the program. 1.       Write a void function to find the average test score for each student and store in an array studentAvgs. void AverageScores( const int scores[][MAX_TESTS],                       int...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • C++

    INFO1112 A11Lab 6March 18, 2021 Note:  It is important that you do this lab exercise properly.  Please read and follow the instructions very carefully.  You need to understand what you are required to do, and complete the program.  It uses a one-dimensional array.  You can refer to the PowerPoint slides, many of the tasks can be found there, but you need to apply them for this program. Note that the array will be filled with the integers when the user enters them. Consider...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

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