Computer Science 1621
Computer Science I

Laboratory Assignment 1
Introduction to C at UMD (5 points)
Due at the end of Lab

Introduction

This lab will familiarize you with the use of Turbo C for Windows on the UMD PC network. In this lab you will create a project, create files, compile, debug and run a program.

Using the Lab

If you are not in the lab for your scheduled lab time you will need to give your ID to a lab consultant to obtain a machine. Please check to see if someone is using the machine you were given. If so, look for a different machine. If the lab is full during your scheduled lab time, please ask your TA or the consultant to announce that it is a scheduled lab time for your class only.

Finding and Starting Turbo C

From the toolbar at the bottom of the Windows 95 desktop select the Start button. This will pop up a menu. Now from the menu select Programs - Specialized Academic Software - Programming languages - C++ - Turbo C++ which brings up the Turbo C software. Your TA will help you with this during your first lab section.

Writing Your Program

Compiling the Program

Select the Project-Compile command from the menu to compile the program. A Compile status dialog box appears on the screen, giving information about the compilation. If no errors occur, the message ``Successfully completed'' is displayed in the Status dialog box. Choose OK to put the dialog box away.

If an error (compile-time errors) occurred during compilation, compilation stops, the compile status dialog goes away automatically, an error message appears on the status bar, and the line that cause the error is highlighted in the edit window. Edit your C file to correct the errors, save it, and then attempt to compile your program again.

Running the Program

To run the program select Debug-Run menu item. Follow the instructions and see how long the program needs to guess your number.

If an error (Run-time error) occurs while you execute your program, note the name of the run-time error. To find out more about this error, go to the Search option under the Help menu (or press the F1 key). Here you could submit the name of the error to be searched for. Under the heading Error and Warning Messages is a complete listing of all Turbo C++ error (both compile-time and run-time) messages. If you neither got an error nor your program output looks like the one shown above then there are some errors in your program which the compiler did not detect.

These must be corrected before going further. Once the program is working try to figure out how many guesses it will take the computer to find the number you are thinking of. Change the line of the program

  highest = 100;
to
  highest = 1000;
See how many guesses it takes the program to find your number in that case.

Printing Your Program

What To Hand In

Hand in the following items:

The Program

/*
 * Your Name
 * CS 1621 Section ?
 * Today's date
 * Lab #1
 */

#include <stdio.h>
 
int main() {
  int lowest;
  int highest;
  int found;
  int guess;
  int guesscount;
  char answer;

  lowest = 1;
  highest = 100;

  printf("Think of a number between %d and %d",lowest,highest);
  printf(" and I will try to guess it.\n");

  printf("Type return when you are ready.\n");
  scanf("%c",&answer);

  found = 0;
  guesscount = 0;
  while (!found) {
    guess = (lowest + highest) / 2;
    printf("Is the number %d (y/n)? ", guess);
    guesscount++;
    fflush(stdin);
    scanf("%c",&answer);
    if ((answer == 'Y') || (answer == 'y')) {
      found = 1;
      printf("\nHooray!  Got it in %d guesses!\n",guesscount);
    }
    else {
      printf("Is the number you are thinking of higher (h) or lower (l)? ");
      fflush(stdin);
      scanf("%c",&answer);
      if ((answer == 'H') || (answer == 'h'))
        lowest = guess + 1;
      else
        highest = guess - 1;
    }
  }
}