Question

Suppose the corridor in which you live has n tiles in a row from one end...

Suppose the corridor in which you live has n tiles in a row from one end
to the other. There is a lot of junk strewn in the corridor.
E.g Suppose the corridor has only 5 tiles, and the junk consists of a solitary
shoe on Tile 2 and a chips packet and a piece of paper on Tile 5. The
junk may be represented in array form as [0, 1, 0, 0, 2], where each array
element a[i] represents the number of pieces of trash on the corresponding
tile.
Suppose you start from Tile s and you want to go to Tile e (s < e). The
junk blocks whatever tiles they are on, so you have to pick them up in
order to go down the corridor. Write programs in C and Python that will
return the array after you have cleared the trash in your path.
Enter your code in the indicated sections below and do not change the pre-existing code:
(a) cleaner.c
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(){
int n; //Number of tiles in the corridor
int s; //start position
int e; //end position (s < e)
int i;
scanf("%d",&n);
scanf("%d", &s);
scanf("%d", &e);
int *hall = malloc(n*sizeof(int)); //Array with quantities
8
of litter
for(i=0;i<n;i++){
scanf("%d",&hall[i]);
}
//printf("%d\n", hall[3]);
//Your code here
for(i=0;i<n;i++){
printf("%d\n", hall[i]);
}
return 0;
}
(b) cleaner.py
n = int(input()) #Number of tiles in the corridor
s = int(input()) #start position
e = int(input()) #end position (s < e)
hall = [] #List with quantities
for i in range(n):
hall.append(int(input()))
#Your code here
for i in range(n):
print(hall[i])

(c) Implement this program in Coffeescript as cleaner.coffee.

Additional requirements:
(1) Your program should run for e < s as ell, i.e when you want to move
from right to left in the corridor.
(2) Your program should clear the trash on the start and end tiles as well.

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

a) Cleaner.c

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(){
int n; //Number of tiles in the corridor
int s; //start position
int e; //end position (s < e)
int i;
scanf("%d",&n);
scanf("%d", &s);
scanf("%d", &e);
int *hall = malloc(n*sizeof(int)); //Array with quantities 8 of litter
for(i=0;i<n;i++){
scanf("%d",&hall[i]);
}
if(s<n+1&&s>0&&e<n+1&&e>0){ //to check whether the index is available
if(s>e){ //to arrange start and end in order
s=s+e;
e=s-e;
s=s-e;
}
for(i=s-1;i<e;i++){
hall[i]=0; //cleaning the trash
}
for(i=0;i<n;i++){
printf("%d\n", hall[i]); //printing tiles after cleaning process
}
}
else{
printf("oops tile not found"); //informing user that the specified tile is not found
}
return 0;
}

input :

5
4
2
1 0 5 6 2

Output :

1
0
0
0
2

b) cleaner.py

n = int(input()) #Number of tiles in the corridor
s = int(input()) #start position
e = int(input()) #end position (s < e)
hall = [] #List with quantities
for i in range(n):
hall.append(int(input()))
if(s in range(n) and e in range(n)) : #to check the tiles availabillity
if(s>e) : # arranging start and end in order
s=s+e
e=s-e
s=s-e
for i in range(s-1,e):
hall[i]=0 #cleaning trash
for i in range(n):
print(hall[i]) #display tiles after cleaning trash
else :
print ("oops tile not found")

Input

5
2
4
1
0
8
6
5

Output

1
0
0
0
5

c) cleaner.coffee:

(->
  
  n = prompt('Enter total no. of tiles')
  s = prompt('Enter the start position')
  e = prompt('Enter the End position')
  if s < n + 1 and s > 0 and e < n + 1 and e > 0
    if s > e
      ref1 = s
      s = e
      e = ref1
  hall = []
  i = 0
  len = n
  while i < len
    hall[i] = prompt('enter tile dust')
    i += 1
  x = s - 1
  while x < e
    hall[x] = 0
    x++
  i = 0
  while i < n
    x = hall[i]
    console.log x
    i++
  return
).call this

Input

3
1
2
1
10
7

Output

0
0
7

NOTE: Explanation done step by step.

Add a comment
Know the answer?
Add Answer to:
Suppose the corridor in which you live has n tiles in a row from one end...
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
  • 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...

  • Use only C Program for this problem. Must start with given codes and end with given...

    Use only C Program for this problem. Must start with given codes and end with given code. CHALLENGE ACTIVITY 9.4.3: Input parsing: Reading multiple items. Complete scanf( to read two comma-separated integers from stdin. Assign userlnt1 and userInt2 with the user input. Ex: "Enter two integers separated by a comma: 3,5", program outputs: 3 + 5 = 8 1 #include <stdio.h> HNM 1 test passed int main(void) { int user Int1; int user Int2; All tests passed 000 printf("Enter two...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • I am writing a program that reads ten integers from standard input, and determines if they...

    I am writing a program that reads ten integers from standard input, and determines if they are going up or down or neither. Can I get some help? I have supplied what I have already. #include <stdio.h> #include <string.h> #include <stdlib.h> int isGoingUp(int [a]); int isGoingDown(int [b]); int main(void) { int array[10]; printf("Enter ten integers : "); for (int a = 0; a < 10; a++) scanf("%d", &array[a]); if (isGoingUp(array) == 1) printf("The values are going up\n"); else if (isGoingDown(array)...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • What is wrong with the following program? Explain how you will fix it in the code....

    What is wrong with the following program? Explain how you will fix it in the code. #include int main() { int i; int *ptr = &i; scanf("%d", &ptr); printf("The value of i is: %d\n", *ptr); return 0; }

  • The following program sorts the array of numbers entered by the user and prints out the...

    The following program sorts the array of numbers entered by the user and prints out the elements in descending order. It has many errors. Debug the code and show the output. #include <stdio.h> #define N 4 void desc(float *a, float *b) float tp; tp=a; a=b; b=tp; int main() int i, j, ii, mark[N] ; for (ii==0;ii<N;ii++) printf("Enter the %d th element of your array: \n",ii); scanf("%d", mark[ii]); printf("You have entered the following array: \n"); for (ii=0,ii<N,ii++) printf("%d ", mark[ii]); printf("\n...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • Create another program that will prompt a user for input file. The user will choose from...

    Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr;    int accountNumber;    if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...

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