Question

C++

INFO1112 A11

Lab 6

March 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 proper layout and correct indentation.

 

 

1.       Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   You will add to the program.  Complete the missing parts, add new function prototypes and function definitions, and test the program several times.  Do not do all at once.

 


Add the following to the program:

 

a)       Write a function to display some heading with useful information which will display on the screen for the user

 

b)       Write a void function that prints the list of nonnegative integers in reverse.

 

c)       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.

 

d)       Write a function that determines the largest value stored in the array and returns that value to the calling function main. 

 

e)       Write a function that determines the number of even integers stored in the array and returns that value to the calling function main. 

 

f)        Write a function that calculates the average of the values stored in the array and returns that value. to the calling function main. 

 

g)       Write the function calls with the appropriate arguments in the function main.  Any values returned will be output.

 

h)       Write appropriate block comments for each function header similar to the ones provided.  Show your understanding of the program and thus provide useful information to the reader.

 

i)        Test all the functions using appropriate test data.  Write the documentation and include the screenshots.  Add the source code by copy and paste to the document.

 

 

j)        Submit the files to Moodles:   .cpp file, .exe file, and the Word document.

 

 

1.PNG

 2.PNG


2 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream> // for cin, cout


using namespace std;


const int MAX_SIZE = 20;


void ReadList(int[], int&);

void PrintList(const int[], int);

void display_heading();

void print_reverse(int[], int);

void greater_than_ten(int[], int);

int largest(int[], int);

int smallest(int[], int);

double average(int[], int);




int main(){

    

    int list[MAX_SIZE];

    int numberOfIntegers;

    ReadList(list, numberOfIntegers);

    PrintList(list, numberOfIntegers);

    

    display_heading();

    print_reverse(list, numberOfIntegers);

    greater_than_ten(list, numberOfIntegers);

    cout<<largest(list, numberOfIntegers)<<endl;

    cout<<smallest(list, numberOfIntegers)<<endl;

    cout<<average(list, numberOfIntegers)<<endl;

    

    

    system("pause");

    

    return 0;

}


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

//The function definition of ReadList.

// The function reads non negative numbers from keyboard into array.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

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

    int number;

    int index = 0;

    

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

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

    cin>> number;

    

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

        list[index] = number;

        index++;

        cin>>number;

    }

    length = index;

}

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

//The function definition of PrintList.

// The function prints the non negative integers in array.

// and number of integers in array.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

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

    int index = 0;

    cout<<"The list contains "<<length

    <<" nonnegative integers as follows :\n";

    

    while(index < length){

        cout<<list[index]<<" ";

        index++;

    }

    cout<<"\n";

}


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

//The function definition of display_heading.

// The function prints important information for user.

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

void display_heading(){

    cout<<"Welcome! This code demonstrates list operations"<<endl;

}



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

//The function definition of print_reverse.

// The function prints elements of array in reverse order.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

void print_reverse(int list[], int length){

    

    for(int index = length - 1; index >= 0; index--){

        cout<<list[index]<<" ";

    }

    cout<<"\n";

}


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

//The function definition of greater_than_ten.

// The function prints all elements greater than 10 and

// the number of integers greater than 10.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

void greater_than_ten(int list[], int length){

    int count = 0;

    

    for(int index = 0; index < length; index++){

        if(list[index] > 10){

            cout<<list[index]<<" ";

            count++;

        }

    }

    cout<<"\n";

    cout<<"Count of numbers greater than 10 is "<< count<<endl;

}


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

//The function definition of largest.

// The function returns largest number in the list.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

int largest(int list[], int length){

    int ans = -1;

    for(int index=0; index<length;index++){

        if(list[index] > ans){

            ans = list[index];

        }

    }

    return ans;

}


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

//The function definition of smallest.

// The function returns smallest number in the list.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

int smallest(int list[], int length){

    int ans = list[0];

    for(int index = 1; index<length; index++){

        if(list[index] < ans){

            ans = list[index];

        }

    }

    return ans;

}


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

//The function definition of average.

// The function returns average of the numbers in the list.

// The parameter list is an array to hold non negative numbers

// The parameter length holds length of the array.

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

double average(int list[], int length){

    double total = 0;

    for(int index=0;index<length;index++){

        total+=list[index];

    }

    

    double av = total/length;

    return av;

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
C++
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
  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Please answer in C++, and Please consider ALL parts of the question, especially where it asks the...

    Please answer in C++, and Please consider ALL parts of the question, especially where it asks the user for V. So there should be a "cin >> V" somewhere. The last guy did not answer it correctly so please make sure you do! Thank You!! Question 1 Write the following two functions. The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

    You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

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

  • Write a C or C++ program a pseudo C program (based on chapter 2 in the...

    Write a C or C++ program a pseudo C program (based on chapter 2 in the book) and then use these programs to develop a MIPS program that gets an integer input from the user, and based on the input returns the minimal or maximal value stored in a static initialized array. The following are detailed instructions: 1) The program generates a static initialized array with 10 integers (using the .word directive). 2) The program prompts the user to enter...

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