Write a function read_list() which prompts for the list of non-negative numbers
ID: 3804397 • 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
Dear Student,
Here i have written the read_list() according to the requirement of the question...
-----------------------------------------------------------------------------------------------------------------------------------------
read_list() function....
void read_list(int arr[], int &num_of_elements,int SIZE)
{
int i=0;
int count = 0;
printf("Please enter the array elements: ");
// informs the user how many elements they have entered and the max elements they can enter before the list is full
do
{
printf("Enter any non-negative integer, or -99 to stop: ");
scanf("%d",&arr[num_of_elements]);
if(arr[num_of_elements] >= 0) // accept non-negative
{
arr[num_of_elements++] = arr[num_of_elements]; // insert at end of array and increment element count
}
if(arr[num_of_elements] == -99)
{
break;
}
if(num_of_elements==20)
{
break;
}
if(count==20)
{
printf("List is empty: ");
break;
}
}while(num_of_elements < 20 || arr[num_of_elements] != -99 || count< 20);
}
----------------------------------------------------------------------------------------------------------------------------------------
Kindly Check and Verify Thanks....!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.