Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. Write a C program, named last10.c, whose input is an unknown number of integers. The program stops reading when it encounters a zero. The
2. Write a C program, named last10.c, whose input is an unknown number of integers. The program stops reading when it encounters a zero. The program then prints the last ten numbers it read (not including the zero), in the order they were given. If less than 10 numbers were input, the program should print them all. For example: adminuser@adminuser-VirtualBox -/Desktop/SP2020-MWS/HW2 S last 10 1 2 4 55 7 9 23 1 2 4 55 7 9 23 adminuser adminuser-virtualBox -/Desktop/5P2020-MWS/HW2 s last 10 1 2 3 4 5 6 7 8 9 10 11 12 O 3 4 5 6 7 8 9 10 11 12 adminuser adminuser VirtualBox -/Desktop/SP2020 - HWS/HW2 ST Your program must not assume anything about the number of input values, which means that you can't declare an array to hold all of them. Instead, you should declare an array of 10 integers, and use it like a circular queue. (You do remember data structures, right?) In particular, keep a pointer to the next open slot of the array. Use scanf to read an integer. If it is not zero, write it to that slot and increment the pointer. If incrementing the pointer puts it beyond the end of the array, set it back to the first slot. When you read a zero, print the contents of the array. Note that you need to begin with the number that was entered least recently, which is not necessarily the number at the beginning of the array. See the second example in the above screenshot Some people will want to implement the queue like this: Once the array is full, vou shift the values in the array one position to the left in order to make room the next input value, which you insert at the far right of the array. This is awkward and inefficient, and a bad idea. Please DON'T do it this way
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started