Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Statement of the problem: . There are 100 doors in a long hallway. They are all closed. The first time you walk by each door,

image text in transcribedimage text in transcribedimage text in transcribed
Statement of the problem: . There are 100 doors in a long hallway. They are all closed. The first time you walk by each door, you open it. The second time around, you close every second door (since they are all opened). On the third pass you stop at every third door and open it if it's closed, close it if it's open. On the fourth pass, you take action on every fourth door. . You repeat this pattern for 100 passes. Question: At the end of 100 passes, what doors are opened and what doors are closed? Program #1: Write a small C program Lab04_100.c that simulates this behaviour. The following demonstrates the execution of the program: # . /02_100 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 OOO 00 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 # echo $? In order to successfully complete this program and obtain all the marks, you will need to: 1. Define a SIZE macro in your program with a value of 100. Use SIZE define the size of your array, and in all of your for loops 2. Define a local array to main( ) of size SIZE to represent the state of the doors. Initialize to 0s all location, using the following declaration: char array[ SIZE ] = {0 };3. Write a funtion, with function prototype( exactly named as ) int toggle_door( char a[ ] ) ( A ) toggle_door( ) should make a 100 passes throughe doors ( B ) If the door is open, it should close it ( C ) If the door is closed, it should open it ( D ) toggle_door( ) should return 0 to main( ) 4. Write a function with function prototype ( exactly written ) int doors_state( char a[ ] ) ( A ) Displays the status of the doors. Simple print of the array - values Os and 1s ( B ) doors_state( ) returns to main( ) with value 0 5. Your main( ) functionshould: ( A ) Call toggle_door() ( B ) Call doors_state( ) ( C ) Terminates with a return statement with value EXIT_SUCCESS 6. Check the exit status of your program from the command line echo $? should display a successful exit 7. Your program should be compiled with the flags -Wall -ansi -pedantic Program #2: Copy your last program to a new file Lab04_100.c to a new program Lab04_100A.c and modify your function doors_state( ) to print a table instead of the complete array in one line. Use the function printf( ), but do not use a TAB. Instead use a field modifier to specify the width of the number you want to print. Use a width of 3 or 4. In the output shown below, a table of 10 rows is used. The row and column number are displayed to make finding the positions of the array easier to the user. Position #49 is shown as o ( Open ). 1 2 3 4 5 6 7 8 9 10 10 20 30 40 nonnononon nonoonanno nonannanGo nonoononon nononnonon onnoononon nonnonnoon 90Marking This assignment is out of 10 3 for coding correctness (i.e., correct results) 3 for appropriate and efficient logic (i.e., no spaghetti code!) 2 for sensible distribution of well-contained functions between files 2 for clear comments and coding convention (not necessarily K&R, but it should be clean and consistent)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions