Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function read_list() which prompts for the list of non-negative numbers

ID: 3804398 • Letter: W

Question

Write a function read_list() which prompts for the list of non-negative numbers terminated by -99. Read the non-negative integers into an array which stores integer numbers. The number of elements in the input list may be less than 20. Stop reading when either the input number is the sentinel value -99 or when 20 (valid) numbers have been read into the array. An invalid number( any negative integer) entered will be ignored. The function takes three parameters, the array holding integers, the number of elements stored in the array (which is defined as pass by reference), and maximum size of the array . Define the first two parameters so that they are modifiable. The function does not return any value. If the user only imput negative numbers , print out " The list is empty"

Explanation / Answer

Ans:-

The pseudo code for read_list function is as below:-

void read_list (int array [], int & numElements, const int ARRAY_SIZE)
{
// Declare variables
int n; // Variable to read in positive integers
int index=0;
cout << "Enter positive numbers (ints) terminated by -99" << endl;
cin >> n;
while (index < ARRAY_SIZE && n != -99)
{
if (n > 0)
{
array[index] = n;
numElements=numElements+1;
}
if (index>20)
break;
else{
index++;
cin >> n;
}
}
if(index==0)
cout<<" The list is empty"<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote