Chapter 1 – Computer Science

1.1 Preface

C++ for Geniuses — (pronounced C plus plus for Geniuses minus minus) covers various aspects of the C++ programming language including input and output, variables and constants, if…else statements, loops, functions, strings, and recursion. Graphical programming is not covered – all programs in this book are command line or console programs.

This book was written by a computer science college instructor who watched beginning students struggle through various C++ books. The goal of this book is to provide a clear and understandable C++ textbook that will be tested and refined in the classroom by observing students using the book. The style of the book is to explain concepts as simply as possible using a top – down approach before covering the details. In addition, since most people learn by doing, many examples and exercises have been provided. All program listings in this book are complete and have been tested using the Linux GNU (g++) compiler.

1.2 Programming Languages

A programming language is used to create computer programs. The native language of the computer (CPU) is machine language. Since this is a tedious language to code, high-level programming languages have been created. These languages will either be compiled or interpreted into machine language to execute.

A compiler translates an entire program (source code) into machine language before executing it. Each programming language has its own compiler. A program must be compiled for each CPU or operating system it is executed on. For example, to run a C++ program on both a PC and Mac computer, you will need to obtain a compiler for both operating systems. An exception is Java.

An interpreter translates a program into machine language one line at a time while the program is running. An executable program is not created as with compiled languages. One advantage of an interpreter is that the program will usually halt execution where an error is encountered, helping the programmer discover errors easily. A disadvantage of an interpreter is that the program runs less efficiently than a compiled program. BASIC is a popular interpreted programming language.

When a new CPU is developed (e.g. Intel Pentium), the first programming is done using machine language – the CPU’s native tongue. Machine language is considered a low-level programming language. Low-level languages are usually hardware (CPU) dependent. Because of the tedious programming of a low-level language, high-level programming languages have been developed. These languages allow the programmer to write code using a simpler format (in English) and then compile it into the CPU’s native machine language for execution. High-level languages are generally not hardware dependent. The following table lists a few programming languages.

Programming Languages

Language

Description

Machine Language

The native programming language of the computer’s CPU. This low-level language is tedious – CPUs come with a specified set of very basic instructions and operations. For example, a load instruction will move each piece of data from memory to a register on the CPU for processing. Machine language is written in binary – all 1’s and 0’s.

Assembly Language

A fancier way to write machine language. This low-level language allows instructions to be written in a more understandable format (instead of binary), then converted to machine language.

C

A high-level structured, procedural programming language that is widely used for programming applications as well as operating systems. It was developed on a UNIX system and later used to reprogram UNIX.

C++

A high-level object oriented programming language that is a superset of C. Originally designed by Bjarne Stroustrup of AT&T’s Bell Labs in 1979. It adds better type checking, data abstraction, and object oriented programming to C.

C#

Pronounced C-Sharp, this programming language was developed by Microsoft as part of the .NET Framework. In some ways, it is an upgrade to C++ and was designed to compete with the Java language.

BASIC

An early high-level programming language that is simple to use and widely popular. Many of the first home computers in the 1970’s and 1980’s included this programming language.

COBOL

COBOL (Common Business Oriented Language) was the first widely used high-level language for business applications such as payroll and accounting. This older programming language has been updated and is still used today, mostly on mainframe computers.

FORTRAN

FORTRAN (Formula Translation) is an older high-level language that was designed for scientific applications.

Pascal

Pascal is an older high-level language that was also designed for math and science applications. It was popular as a teaching tool for structured programming before C became popular.

Visual Basic (VB)

Microsoft’s Visual Basic adds object-oriented features and a graphical user interface to the standard BASIC language.

Java

A high-level object-oriented programming language developed by Sun Microsystems for creating applets that are platform independent. A Java applet (.class file) will execute on any operating system that has the Java run-time console installed. Java applets first became popular as programs that execute within a web browser.

JavaScript

A scripting language used in web pages that enhances the limited capabilities of HTML. JavaScript adds many functions to HTML including as cookies and pop-up windows.

For most languages, programming language standards have been written. It is important to standardize a programming language for the sake of portability. Portability means that a C++ program (source code) can be placed on a different system using a different compiler. For example, a program created and compiled using the Linux g++ compiler should be able to be compiled using Microsoft Visual C++ without making changes.

C++ has been standardized by two organizations working together: American National Standards Institute (ANSI) and The International Standardization Organization (ISO). The C++ standard which was adopted in 1998 is referred to as ISO/IEC 14882:1998.

1.3 Software Development Life Cycle

Creating a program or application is often referred to as software development. Developing larger applications might be called systems development. Larger projects require extensive planning, organizing, and management. Example large systems include the Windows XP operating system, the UPS package delivery system, and the American Airlines reservation system. Different organizations may have different software development methodologies. Government agencies usually have documented formal guidelines. The following table lists the general steps involved with software development.

Create a program design from the specifications. Decide on a programming language and algorithm for the program. Describe desired features and operations in detail using screen layouts, business rules, process diagrams, pseudo code and other documentation.

Programming

The system design is implemented as code by the programmers.

Testing

Create a test version of the software (beta version) and implement in a special testing environment. Check for errors, bugs, and interoperability.

Deployment

Put the software into production: install the software and train the users.

Maintenance

After deployment, the software will occasionally need modification due to bugs, desired new features, or if the software is ported to a new platform (e.g. moved to a different operating system). The maintenance step will continue as long as the software is in use.

• Pseudo Code

For small programs such as the ones listed in this book, pseudo code can help layout a problem before you write the actual code. Pseudo code shows a detailed description with how a program should work, but expressed in natural language rather than the programming language. It allows the designer to show the steps needed for a program without actually coding it. The following is example pseudocode for a program that calculates the volume of a pyramid.

1. Declare variables for base, height and width2. Prompt the user to enter the pyramid’s height.

3. Prompt the user to enter the pyramid’s base.

4. Prompt the user to enter the pyramid’s width.

5. Calculate Volume = (base * width * height) / 3

6. Display the volume of the pyramid.

• Flowcharts

Another tool used by some programmers are flowcharts. A flowchart provides a graphic representation of a program’s logical sequence. They describe each processing path in a program. With the advent of object-oriented programming, flowcharts have become less common. Since C++ adds objects (i.e. classes) to the C language, flowcharts are less frequently used in C++.

The diagram on the right shows a simple flowchart for a software installation program. Processes are shown as squares. Decisions (e.g. if statements) are shown as diamonds.

• Application Generators

Many programming environments contain software tools for generating parts of a program automatically. For example, a report generator is a popular tool found in database programming languages that enable you to create a report visually by specifying the needed data fields and layout. A form generator is used by database languages to create data entry forms. Programming languages like Visual Basic allow you to create screens visually