• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • You Can Communicate

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 4

You Can Communicate - all with Video Answers

Educators


Chapter Questions

Problem 1

Use your text editor to write a file of names, one per line. (Use Quincy to do this by creating a new ASCII text file - it is at the bottom of the list of new file types.) Make the last entry END and do not forget to complete that last line by pressing Return. Please do not use a word processor for this because they add all kinds of formatting information that will confuse your programs.

Now write a program that will read in that file, sort it and write the result out to another file. If you have understood how streams work in $\mathrm{C}++$, this a fairly straightforward task.

Check back soon!

Problem 2

Write a program that will get a line of text from std: : cin and count the number of times the letter " $\mathrm{a}$ " is used. Bonus credit if your program correctly handles " $A$ " as well as " $\mathrm{a}$ ".

Check back soon!

Problem 3

There is a very simple free function that takes a char argument and returns the uppercase equivalent if the argument represents a lowercase letter. If the char argument represents anylhing else the argument is simple returned unchanged. The declaration of this function, provided in the <cctype> header is:
char toupper(char);
If you have understood the substripting of a std: : string object, you should be able to write a simple program that gets a line of text from the keyboard and then displays that text with all lowercase letters converted to uppercase.

Check back soon!
04:01

Problem 4

In the early days of computing when the main output device was only capable of printing letters, creative programmers used to write programs that produced pictures entirely composed of letters and punctuation marks. Produce a simple picture on the screen in the same way.

AG
Ankit Gupta
Numerade Educator

Problem 5

Modity the program you wrote for Exercise 4 so that the user is prompted for the name of the file with the data in it. Use a std: : string object called filename to capture the response.

You will need to use one of my utility functions to open the std: : ifstream object with a file name that is stored in a std: :string variable because the $\mathrm{C}++\mathrm{Standard}$ Library does not provide such a function (that is no great problem to experienced C++ programmers, just a minor irritant).

In my header tgw_text.h there are declarations for functions to open files for ifstream and of stream objects given the filename in a std: :string variable. The declarations are:
void open_ifstrean(std::ifstream \& input_file, std: string const \& filename);
void open_ofstream(std::ofstreall $\&$ output_file, std::string const \& filenane);
These declarations are in namespace fgw; so you will need to add fgw : : to the function name when you use it. (Or put a using namespace fgw directive after you have included the header file tgw_text.h.) A typical call would be:
fgw: : open_ifstrean(input, filename);
where input and filename have been suitably defined. You do not need to include fgwlib. $\alpha$ into the project because the definition is directly available in the header file. (I used a little trick called inline delegation which allows me to get away with putting a definition in a header file.)

Check back soon!

Problem 6

Use your text editor (Quincy will do fine) to create a file of whole numbers. Do not use any punctuation; just separate the numbers with spaces or with newlines (either will work). Make 0 the last number in the file.

Now write a program that will open the file, read in the numbers one by one and display each number with its square (number multiplied by itself) and cube (the square of the number multiplied by the number, so the cube of 2 is four times two, which is eight) on the screen, one line of output for each number in the file. Stop when the number read in from the file is zero.

Check back soon!
02:03

Problem 7

Use a suitable sequence container to store all the numbers from the file in Exercise 6. Find the arithmetic mean of the numbers (the total divided by the number of items, frequently called the average) and display the answer on the screen. Now calculate and display the median of the collection of numbers. The median is the middle number when they are arranged in order of size. Strictly speaking if there is no single middle item (because you have an even number of items) it is the average of the middle two.

Gregory Higby
Gregory Higby
Numerade Educator
14:49

Problem 8

Use the numbers in the file you created for Exercise 6 as data for a bar chart. What I want is for you to create a display with bars that represent the numbers in the file. For example it the first three numbers in the file are $13,5,9$, I want three horizontal bars of 13 units, 5 units and 9 units in length.

You can write a program that creates the bars from repeated symbols ("*"" is often used for this) of you can use the graphics resources of playpen.

If you feel up for a challenge, ensure your program can deal with negative numbers with bars going to the left.

Finally attempt the same task using asterisks but in vertical columns. See if you can arrange that the positive values are represented by upward columns and negative ones with downward columns.

Note that I do not expect readers to complete all parts of Exercise 8. It isn't that you need fancy programming, but you need to see how to use the tools you have to achieve the objective. That is what programming is about and it takes a lot of practice to achieve simple programs for tasks such as the above. Sometimes I will give you a tough exercise as a challenge to the very brightest of my readers.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator