Please enable JavaScript to use CodeHS

Karel Internship

In this project, students create a program that will read in a file and analyze it to find a buggy loop.

Medium

3 Hours

High School

Project Description

Background

You have been hired by Karel Inc, a new dog training company, to help build out a dog training simulation. In this simulator, we are trying to teach Karel to sit. As he sits, he accumulates treats.


Unfortunately, the previous intern didn’t get a chance to debug his code and he left it in a state of an infinite loop. Your job is to figure out how many treats Karel has accumulated before the program starts looping.


Your Task

You will complete this in two parts. In part one, you are going to create a struct that can store an instruction, including information on whether the instruction has already been visited. Once you hit an instruction that has already been visited, you will know that you are in a loop.


For part 2, you are going to take the struct you built in part 1 and use it to analyze the program.


You’ll start by prompting the user for a name of an input file. Once they provide the name, you should open the file and read in each line as a new instruction and save to a vector. Make sure you use error handling in case the user enters a file name that is not found!


After reading in all the instructions, you will process them to determine how many treats Karel accumulates before the program loops. Once the program visits the same instruction a second time, you know you are looping and should output the number of treats accumulated.

Project Demo

Explore this program before assigning it!

Project Overview

Here is an outline of the project activities: