# Arrays

**Question description**

1) Write a program hot.c that prompts the user for a word and reads it into a 50-character array. If the word contains the lower-case letters ’h’, ’o’, and ’t’, in order though not necessarily consecutively, print “Hot”, otherwise print “Not”.

EX:

(˜)$ a.out

Word: hotelier

Hot

(˜)$ a.out

Word: hypnotist

Hot

(˜)$ a.out

Word: shoplifTer

Not

(˜)$ a.out

Word: PHILANThRoPISt

Hot

(˜)$ a.out

Word: philanderer

Not

2. Write a program rotate.c that prompts the user for the size of a matrix and then for each of its rows. Rotate the matrix one notch clockwise around its center. Use a 2-dimensional array of the same size as the matrix, and make sure you rotate all elements correctly as in the size-4 example below.

EX:

a.out

Matrix size: 1

Row 1 digits: 9

After clockwise rotation:

9

(˜)$ a.out

Matrix size: 2

Row 1 digits: 8 4

Row 2 digits: 2 7

After clockwise rotation:

2 8

7 4

(˜)$ a.out

Matrix size: 3

Row 1 digits: 5 0 4

Row 2 digits: 7 3 5

Row 3 digits: 4 1 2

After clockwise rotation:

7 5 0

4 3 4

1 2 5

(˜)$ a.out

Matrix size: 4

Row 1 digits: 7 2 3 8

Row 2 digits: 4 7 2 1

Row 3 digits: 9 1 4 0

Row 4 digits: 3 6 2 5

After clockwise rotation:

4 7 2 3

9 1 7 8

3 4 2 1

6 2 5 0

Hint: Note that the matrix rotation consists of “circles” inside the matrix. These circle rotations can be performed separately, where each circle rotation can be accomplished by four loops, one for each side of the square. Also remember that when you move elements around, you may need to temporarily save one of the elements and then restore it.

3. Write a program escape.c that reads a 5 by 5 ﬂoor plan, one row at a time. Each row consists of ’#’, ’.’, and ’S’, where ’#’ represents a wall or an obstacle, ’.’ represents a clear location, and ’S’ represents a student who needs to escape the space. There is exactly one student on the whole ﬂoor, and he/she can walk horizontally or vertically, but not diagonally. Determine if the student can escape the building in case of ﬁre. For a realistic view, after asking for “5X5 layout”, print a new line.

EX:a.out

5X5 layout:

###.#

###.#

#S..#

#####

#####

Escape!

(˜)$ a.out

5X5 layout:

#####

##S.#

#...#

#####

#####

Sorry.

(˜)$ a.out

5X5 layout:

#####

###.#

S...#

#####

#####

Escape!

(˜)$ a.out

5X5 layout:

#####

###..

#.S.#

###.#

###.#

Escape!

(˜)$ a.out

5X5 layout:

.###.

#.#.#

#.S.#

.##.#

####.

Sorry.

Hint: One relatively simple approach is to iteratively consider the square and mark more and more locations that the student get reach. In the ﬁrst iteration, consider the 3X3 interior of the square (ex-3cluding the boundary). If the student is in the interior, mark all its immediate neighbors that are free (namely ’.’, but not ’#’) as ’S’ (meaning the student can go there). Then repeat the process and in each subsequent iteration again consider just the 3X3 interior, and mark all neighbors of any ’S’ (either the original student, or those marked ’S’ in previous iterations) as ’S’ as well. It is easy to see that if the student can escape, then after a small number of iterations (you can ﬁgure out how many), there will be an ’S’ on the boundary, and you could check if that has occurred.

## Tutor Answer

## 8 Reviews

**Quality**

**Communication**

**On Time**

**Value**

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors