CSCI 131 Techniques of Programming--Spring 2014

    Home | | Schedule | | Assignments | | Lectures | | Resources

    Solutions to Lab 5

    Simple input/output and loops

    1. hello.cc - Make a program that prints "Hello World!" to the screen.
    2. bart50sheep.cc - Make a program that prints "I will not bring sheep to class." fifty times (one sentence per line).
    3. bartsheep.cc - Same thing, but ask the user how many times it should be printed, then print it that many times.
    4. bart.cc - Same thing, but first ask the user to enter a sentence, then ask how many times it should be printed, then print that sentence that many times. (Hint: The C++ function getline will be useful for getting the sentence.)
    5. add.cc - Make a program that asks the user to enter two numbers in a row. The program should then get the numbers from cin, add them, and print out the sum. So if the user types "5 7", the program should print out "12".
    6. calc.cc - Same thing, but let the user chose which operation to do. So if the user types "5 + 7" your program should print out 13. And if they type "5 - 3" your program should print out "2. The program should be able to do the four basic arithmetic operations at minimum, and it should print a friendly message if they type some other operation. Congratulations, you made a calculator.
    7. yonson.cc - Make a program that prints the "Yon Yonson" song on and on forever. To kill a program stuck in an infinite loop, press Control-C in the window where the program is running.

    Conditionals and switch statements

    1. french.cc - Make a (very limited) english-french translator program. Ask user to enter a color. If they type "red", the program should print "rouge". If they type "green", the program should print "vert". Add a few other words you know, but if they type anything else, the program should print "Je ne sait pas!".
    2. sign.cc - Make a program that asks the user to enter an integer, then print out "positive", "negative", or "zero", depending on what the user types.
    3. bettersign.cc - Same thing, but print a more useful message that includes the number they typed, e.g. if they enter -13, then print out "-13 is negative."
    4. divisible52.cc - Make a program that asks the user to enter an integer then checks if that integer is evenly divisible by 52. So if they type 104, it should print "Yes, 104 is divisible by 52", but if they type 203, it should print "Nope, 23 is not divisible by 52".
    5. divisibility.cc - Same thing, but instead of divisible by 52, first check if the number they entered is evenly divisible by 2, then check for divisibility by 3, then 4, etc. Stop when you reach one less than the number they entered. So if they entered 17, check for divisibility by 2, 3, 4, 5, ..., 14, 15, and 16.
    6. factors.cc - Same thing, but only print the "Yes, ..." messages. Also at the end of the program print a count of how many "Yes, ..." messages were printed.
    7. prime.cc - Same thing, but only print a single message at the very end saying "The number you entered is prime" or "The number you entered is not prime" as appropriate. (See how quickly your program can produce an answer for a large number like 1172063 or 987643213.

    File input/output

    1. firsthonor.cc - Make a program that opens the file "/home/fac/csci131/labs/lab1/honor.txt", reads in the first word, and prints that word to the screen.
    2. first.cc - Same thing, but ask the user to enter a file name (instead of using the honor.txt file).
    3. counting.cc - Make a program that opens a file "numbers.dat", writes the numbers 1 through 50 into the file, then closes the file.
    4. evens.cc - Same thing, but only write the even numbers.
    5. sum.cc - Make a program that opens a file "numbers.dat", then reads and sums up all of the integers in it, then finally closes the file and prints out the sum. If the file can't be opened, print a helpful message then quit immediately.
    6. wordcount.cc - Make a program that asks the user to enter a file name then counts how many words are in that file. (Hint: Use a string variable s then just keep doing inFile >> s until you get to the end of the file, keeping a count as you go.)
    7. linecount.cc - Same, but counts how many lines are in the file instead. (Hint: Use a string variable s then just keep doing getline(inFile, s) until you get to the end of the file.)
    8. octopus.cc - Make a program that reads each word from the file "/usr/share/dict/words" in turn and for each word checks if that word is "octopus". If it is, then print out "Hurray, octopus was found." and exit the program immediately. If none of the words matched "octopus", then at the very end of the program print out "Sorry, octopus was not found."
    9. spelling.cc - Same thing, but instead of searching for "octopus", ask the user to enter a word and search for that word instead. (Congratulations, you just wrote a spell-checker program! But see below for a better one.)

    Functions

    1. bar.cc - Make a function called RepeatedlyPrintX that takes one parameter called n (an int) and returns nothing. The function should print 'x' to the screen n times, followed by a newline. Test your function by calling it from main using a few different arguments, like this (the first line printed should just be "xxxxxxxxxx"):
        int main(void) {
          RepeatedlyPrintX(10); 
          RepeatedlyPrintX(5); 
          RepeatedlyPrintX(7); 
          return 0;
        }
        
    2. labeledbar.cc - Make a function called LabeledBar that takes two parameters, one called label (a string) and one called n (an int). The function returns nothing. The function should print the label, then a space, then call the RepeatedlyPrintX function so that a bar of width n is printed. Test your function by calling it from main using a few different arguments, like this (each line printed should look like "dog xxxxxxxxxx", etc.):
        int main(void) {
          LabeledBar("dog", 10); 
          LabeledBar("cat", 5); 
          LabeledBar("rat", 7); 
          return 0;
        }
        
    3. bargraph.cc - Make a program that asks the user to enter a file name (e.g. like this one), opens that file, then repeatedly does the following (until the end of file is reached): read a word and an int from from the file, then call the LabeledBar function. The goal is for a nice bar graph to be printed. Be sure to print a warning message if the file could not be opened, and make sure you close the file at the end of the program.
    4. prompt.cc - Make a function called PromptForAnInt. It takes no parameters and it returns an int. The function should print out "Please enter an integer: ", then get an integer from the keyboard, then return that integer. Test your program like this:
        int main(void) {
          int x = PromptForAnInt(); 
          int y = PromptForAnInt(); 
          cout << "You entered " << x << " and " << y << endl;
          return 0;
        }
        
    5. primefunction.cc - Make a function called IsPrime. It takes one parameter called n (an int), and returns a bool. The function should return true if n is prime, otherwise it should return false. (Hint: The code for the body of this function is nearly identical to the code from the prime.cc program above... just replace the cout statements with "return true;" or "return false;" statements. Test your program by calling it from main like this:
        int main(void) {
          int x = PromptForAnInt();
          if (IsPrime(x))
            cout << x << " is prime!" << endl;
          else
            cout << x << " is not prime!" << endl;
          return 0;
        }
        
    6. primefinder.cc - Make a program that asks the user to enter a number, then finds and prints the lowest prime number greater than or equal to that integer. So if they type 20, the program would check if 20 is prime (it's not), 21 (nope), 22 (not this either), then finally 23 (aha, a prime). Use the PromptForAnInt and IsPrime functions as appropriate.
    7. smallrect.cc - Make a function called Print3x5Rectangle that takes no parameters and returns nothing. The function should call RepeatedlyPrintX(5) three times so that a rectangle is printed to the screen. Test your function by calling it from main.
    8. rect.cc - Make a function called PrintRectangle that takes two parameters called width (an int) and height (an int) and returns nothing. It should call RepeatedlyPrintX in a loop so that a rectangle of the given width and height is printed. Test your function by calling it from main a few times.
    9. square.cc - Make a function called PrintSquare that takes one parameter called size (an int) and returns nothing. It should call PrintRectangle so that a square of the given size is printed. Test your function by calling it from main a few times.
    10. draw.cc - Make a program that opens a file of the user's choice (e.g. like this one) and repeatedly does the following (until the end of the file is reached): Read a word from the file. If the word is "square", then read an int and call the PrintSquare function so that a square gets printed. If the word is "rectangle", then read two ints and call the PrintRectangle function. If the word is "skip", then print out a blank line. Congratulations, you have made an ascii-art drawing program!
    11. change.cc - Make a function called MakeChange that returns nothing but takes one int parameter called price and five int reference parameters called dollars, quarters, dimes, nickles, pennies. The function should assign the five reference variables to the appropriate values based on the price. So if price is 527, then dollars should be assigned 5, quarters should be 1, pennies should be 2, and nickles and dimes should be assigned 0. Test your function with a main program like this:
          int main(void) {
            int d, q, m, n, p;
            int price = PromptForAnInt();
            MakeChange(price, d, q, m, n, p);
            cout << "That is "
      	  << d << " dollars "
      	  << q << " quarters "
      	  << m << " dimes "
      	  << n << " nickles and "
      	  << p << " pennies"
      	  << endl;
            return 0;
          }
        
    12. spellcheck.cc - Make a function called SpelledCorrectly that takes one parameter called word (a string) and returns a boolean. The function should search through the "/usr/share/dict/words" file, and it should return true if the word was found in that file, otherwise it should return false. (Hint: The body of this function is almost identical to the spelling.cc program above.). Now write a main function that asks the user to enter a file name, opens that file, then repeatedly does the following (until the end of the file is reached): read a word from the file, call the SpelledCorrectly function, and prints a friendly message if it returned false.

    Home | | Schedule | | Assignments | | Lectures | | Resources



    CSCI 131: Techniques of Programming
    Last Modified: February 23, 2014
    Page Expires: October 12, 2014