1. Introduction to Programming

What We Will Cover

1.1: Introduction to Programming Concepts and Methodology, C++

Learner Outcomes

At the end of the lesson the student will be able to:

  • Describe what they will learn in this course

1.1.1: Why Study Computing and Computer Science?

  • There are many reasons to study and understand computing
  • The following are a few good ones

Computing Puts You in Control

  • Why merely create a FaceBook page when you can create the next FaceBook?
  • Programming gives you the power to solve problems and eliminate repetitive, boring tasks
  • Studying computers lets you bring into existence new worlds that you imagine
  • Computing gives you new ways of improving our lives by putting better ideas into practice

Computing Opens Doors

  • There are over one billion personal computers in use (source: WikiAnswers.com, Forester Research)
  • In addition, there are many billions more computers embedded in everything from cell phones to car engines, iPods and video games
  • Because the number of computers is growing so fast, few careers open as many doors as computing

Computing Empowers You to do Good

  • Computing lets you connect technology to your community and make a world of difference
  • Computers can let us improve the environment by reducing energy consumption and pollution
  • With computers we can improve health care and reduce medical costs
  • The human genome is analyzed and searched for patterns using computers
  • Also, computers give us new ways to learn things, some of which you will experience in this course

More Information

1.1.2: Computers and Programming

  • What is a computer?

    Computer: a machine that can manipulate data following a list of instructions

  • The lists of instructions are called programs

    Program: a list of instructions carried out by a computer

  • A computer cannot function without a program
  • Guess who develops computer programs? People do.

Developing Programs

  • In this course we learn how to develop programs for computers
  • We will start with algorithms, which are the essential first step in developing programs

    Algorithms: A sequence of precise instructions which leads to a solution

  • Then we will learn how to translate algorithms into programs
  • Once we have a basic understanding of algorithms and programs, we will learn how to develop larger programs
  • As programs become larger, you need to organize your programs into modules

    Module: a self-contained component that can be combined with other components to make up a complete system

  • Using a modular approach we can solve larger computing problems
  • There are many programming languages for computers
  • In this course we learn how to program using C++

More Information

1.1.3: Examples of What You Will Learn

  • As we learn how to program, we will solve some interesting problems
  • Let us take a look at some examples of what you will learn in this course
  • First we have some samples from homework assignments:
    • printsquares: Draw shapes with ASCII text
    • postal: Explore how postal bar codes work
    • store: Develop our own virtual business
    • madlibs: Play a game of Mad Libs

More Examples

  • During the course you will learn how to develop programs like these:
  • While these are not assignments this semester, you will be capable of solving these types of programming problems

Student Projects

  • Toward the end of the course you will complete a project
  • The purpose of the project is to utilize what you learned during the course to produce your own unique program
  • In previous semesters students have completed many fascinating projects including:
    • 3D shape displays
    • Address books
    • Ant farm simulation
    • Astronomy calculations and simulations
    • Bank account programs
    • Battleship games (text and graphical versions)
    • Dart games
    • Database programs (store and retrieve selected data)
    • Card games
    • Digital photo manipulation (like Photoshop)
    • Fortune tellers
    • Enhanced Mad Libs game programs
    • Mine sweeper
    • Spectrographic analysis
  • You too will be able to create programs like these

1.1.4: Summary

  • We are surrounded by computers in our daily lives
  • Computers are used to solve many problems in the real world
  • Yet a computer is just a machine that follows a list of instructions
  • The list of instructions are called programs
  • To develop a program you need the intelligence and creativity of a person
  • To solve a problem with a computer, you start with one or more algorithms
  • An algorithm is a sequence of precise instructions which leads to a solution
  • Once you have an algorithm, you translate an algorithm into a program
  • During this semester, we will learn how to computer programs using C++
  • We looked at some examples of the programs you could solve after completing this course

Exercise 1.1: Introductions and Questions

Use the next 10 minutes to complete the following.

  1. Join a group of 3-4 people and introduce yourself to each other.
  2. As a group, develop one "burning question" about the course.
  3. Write the single question on the board.
  4. Select one member from your group as a spokesperson to describe your single question to the class.

Note: You do not have to turn in the answers to this exercise. However, you are expected to submit answers to subsequent exercises.

1.2: Exploring the Course

Learner Outcomes

At the end of the lesson the student will be able to:

  • Navigate the course web site
  • Discuss how the course is administered
  • Know when assignments are due

1.2.1: Some Useful Information and Resources

Email

  • All students are requested to have an email account
  • Please make sure your email address is correct in WebAdvisor
  • When sending me an email please state your name, course number and if you are in the day or evening section
  • Please make sure your emails are professional, that words are spelled correctly and that you use good grammar
  • If you expect a reply, make certain your spam filter is not removing my emails
  • If you have not received my reply within a day, then check your spam folder before sending me another email

Online Lab Hours

  • The schedule of classes lists, "4 hr 5 min online lab per week".
  • These online lab hours are there to complete the exercises in CodeLab
  • You must sign up for and later purchase a CodeLab subscription
  • If you do not have access to a computer, you may use one in the Computer Technology Center (CTC)
  • The CTC is open, supervised and available for use as shown here: Computer Technology Center Hours

Exercise 1.2: Treasure Hunt

In this exercise we explore the course web site to see how many of our questions we can answer. We will discuss the questions and answers after you have a chance to explore. To find the answers, look at the section we just covered: Some Useful Information and Resources

Specifications

  1. Work with no more than one other person on this exercise.
  2. Prepare a text file named: questions.txt.
  3. Search the course web site to find answers to the following questions.
  4. Record the question and its answer in the text file.
  5. Review your answers with at least one other student in the class and correct any mistakes.
  6. Add a comment to the top of the file that contains the name of the person with whom you reviewed the code, like:
    Reviewed with Jane Programmer
    
  7. When finished, do not forget to upload your file to Blackboard as part of assignment 1

Exercise Questions

  1. What is your personal Blackboard login (NOT your password)?
  2. What do you do if you are late to class?
  3. Where is the instructor's contact information located?
  4. What is the closest building next to the instructor's office?
  5. What is the URL where all lab and programming assignments are listed?
  6. Where do you turn in homework assignments?
  7. What course should you take before this one if you are not familiar with computers?
  8. How much time should you expect to spend on this course outside of the classroom?
  9. What is CodeLab and why must you spend $25 to purchase it?
  10. What compiler must you use for this course?
  11. What is scholastic dishonesty?
  12. What is an example of acceptable help and an example of unacceptable help on assignments?
  13. Why is eating and drinking prohibited in the classroom?
  14. What are three things you should do to succeed in this course?

1.2.2: Suggestions for Success in This Course

  • You can learn the course material in many different ways
  • However, by following the guidelines below you will get a clearer understanding of Internet programming
  • Also, you will get the best possible grade

Understanding the Material

  • Note the Learner Outcomes for each lesson and concentrate on learning them
  • Skim the lecture notes to get an overview of what you need to learn
  • Read the assigned pages of the textbook, focusing on the Learner Outcomes
    • Note the relationships between the lecture notes and the textbook
    • Lecture notes may highlight the text or may present information in a new way
  • Attend the lecture, ask questions and participate in the discussions
  • After the lecture, review the summary and answer the Check Yourself questions
    • This helps to reinforce your learning of the material
  • If you cannot answer the questions, then follow up and find the answers
  • If you cannot find the answer in the text, then ask a classmate or the instructor
  • Suggestion: make a note and ask a question at the start of the next class

The Assignment Cycle

Most assignments (after the first) have three parts:

  1. Warm Up Exercises:
    • The first part is the warm-up exercises, which help prepare you for the problem-solving portion of the assignment
    • Warm-up exercises start with exercises we do in class and turn in to Blackboard
    • Then you complete several short review exercises, usually in CodeLab
    • CodeLab will tell you if you answered correctly and give you some hints if you make a mistake
    • Also, after you attempt the problem, you can try again and usually look at the answer in the Solutions tab
  2. Problem Solving Program:
    • The problem solving section lets you explore the concepts in a different way than the exercises
    • You will be given a problem and need to develop a solution
    • Acceptable solutions must meet a list of specifications

      specification: A condition or restriction that is insisted upon

  3. Tutorial Exercises:
    • The tutorial exercises introduce the new material
    • First read the assigned reading in the textbook to understand how to solve the problems
    • Then complete the tutorial exercises as specified
    • Also, you can refer to the online lecture notes as they become available

Preparing for Exams

  • Exams provide an opportunity to demonstrate what you have learned
  • If you read the textbook, attend lectures, complete the exercises and assignments, and study for each exam, you should do well
  • You will need to spend time studying for exams
  • I will discuss study techniques before the exams

1.2.3: Setting Up Your Programming Environment

  • Most of your homework assignments will be to solve problems by developing a computer program
  • To develop the programs, you will need to use a computer running software programs called software tools
  • All the software you need runs on almost any computer
  • One of the software tools you need is a compiler
  • A compiler is a program that translates C++ (or other programming languages) into the language of the computer
  • We will discuss how compilers work next week
  • However, the compiler you must use is called g++ and is free from GCC
    • If you use a different compiler, your code may not run when I test it
    • If your code does not run you will get a low score
  • For Windows, you can install Cygwin, which contains the g++ compiler
  • For Linux/UNIX users, you can install GCC and g++ compilers from your distribution CD
  • If you do not have a computer at home (or work) to use for assignments, then you can use the CTC
  • To use the CTC, you will need a login and password
  • You are automatically assigned a login and password after you enroll in this course

1.2.4: Summary

  • www.edparrish.com has a link to the course web site and has contact information for the instructor
  • Syllabus describes the course and it grading policies
  • Home Page lists the online content of the course
  • Schedule lists the planned lesson-topics and assignments
  • WebCT/Blackboard is where you turn in assignments, take tests and can look at your grades and get feedback on assignments
  • Class Expectations describes what you can expect from me and what I expect from you during the course
  • To succeed in this course, you should attend lectures, ask questions and participate in the discussions
  • Also, you need to complete your assignments and turn them in before the due date

1.3: Programming and Problem Solving

Learner Outcomes

At the end of the lesson the student will be able to:

  • Describe the activity of programming
  • Develop algorithms to solve simple computational problems

Exercise 1.3

  • The heart of computer programming is developing algorithms for solving problems using computers
  • Algorithm: A sequence of precise instructions which leads to a solution
  • In this exercise we explore how to develop an algorithm
  • Take about 10 minutes to complete the following specifications

Specifications

  1. Join a group of 3-4 people for this exercise.
  2. Prepare a text file named: walk.txt.
  3. Analyze the following problem and find an algorithm to solve it.
  4. Record your algorithm in the text file as a numbered list of steps that will solve the problem.
  5. Select one member from your group as a representative to describe your answer to the class.
  6. Do not forget that everyone in your group must upload the text file to Blackboard when finished.

Walking Problem

Develop a precise list of instructions for walking from a spot in front of the instructor's computer to the door of the classroom. You can only use the following commands to describe the algorithm:
  • face direction
  • forward amount
  • backward amount
  • turnLeft amount
  • turnRight amount
Where:
  • direction is a compass heading like north or south
  • amount is a number and a unit of measure like feet, paces, degrees or turns

1.3.1: Computers and Programming

  • A computer is a machine that can manipulate data following a list of instructions
  • Each of the instructions are extremely primitive
  • For example, computers can follow instructions like:
    1. Get a number from a location in memory
    2. Get a number from a keyboard
    3. Add up these two numbers
    4. If the sum of these two number is positive, put a dot at a specific screen location
  • These lists of instructions that computers follow are called programs
  • The process of writing programs is called programming
  • Computers sometimes appear intelligent because they process huge numbers of these simple instructions
  • However, the intelligence you see is from the programmers that developed the list of instructions

Why Develop Programs?

  • People develop programs because they want to use a computer to solve a problem
  • A computer is well suited to solving a wide range of problems
  • Computers can handle repetitive chores without becoming bored or exhausted
  • To change what a computer does, you change its list of instructions

1.3.2: The Activity of Programming

  • Programming is about developing a list of instructions for a computer to follow in solving a problem
  • The people who develop programs must have expertise in many areas including:
  • A problem domain is the body of knowledge for which you are interested in solving problems about
  • For example, you may solve problems in domains like:
    • Accounting
    • Business
    • Engineering
    • Graphic Design
  • As a programmer, you become an expert in the problem domain in which you program
  • More information: Why Choose CSE?

Developing Programs

  • Computers can only mechanically follow simple and precise instructions
  • All the intelligence for a computer program comes from the programmer
  • Thus, programming is a creative process
  • There is no complete set of rules for creating a program
  • However, we know from experience that programs are best developed in two phases

Program Development Phases

  • Programs are developed in two general phases:
    1. Problem-solving phase
      • End result is one or more algorithms that solves the problem
    2. Implementation phase
      • End result is the algorithms translated into a programming language
  • Each of these phases has several general steps to them

Program development flow

  1. Problem definition
    • First step is to analyze and understand the problem
    • We will look at this step in more detail in the next section
  2. Algorithm design
    • Next step is to define an algorithm
    • Set of steps that a computer can use to perform a task
    • We will look at this step in more detail as well
  3. Desktop testing
    • After developing an algorithm, you need to verify it is correct
    • To test, you develop a sample problem
    • Walk through each step mentally or manually
    • If there is an error, change the algorithm
  4. Translate to C++
    • After you are sure the algorithm works, then write the program code
    • Code is written using a programming language
    • The language we use for this course is called C++
    • As you write the program code, you translate human-readable source code into a language understood by the computer
    • You check your translation using a program called a compiler
  5. Testing
    • After code compiles, you test it to make sure it:
      1. Does what it is supposed to do
      2. Does not do what it is not supposed to do
      3. Does what it used to do
  6. Working program
    • When it works well, you document and deliver the program

1.3.3: Analyzing Problems

  • The first step in solving a problem is to understand it
  • You need to examine the data and conditions of the problem
  • The topics in this section suggest some ways to help you analyze the problem and gain an understanding

Write Down the Problem

  • A good way to start analyzing a problem is to write it down in your own words
  • Often times writing down the problem will lead to other ways of analyzing it
  • See: Feynman Problem-Solving Algorithm

Ask Questions

  • If you are given a problem, you naturally ask questions like: What? When? Why? Where? How?
  • Here are some questions that are often useful to ask when faced with a programming problem:
    1. What output is expected?
    2. What format is required for the output?
    3. What calculation must be performed?
    4. What data is needed for the calculations?
    5. What data do I have to work with?
    6. Where will I get the data I do not have?
    7. What do the data items look like?
    8. How will I know when I have processed all the data?
    9. How many times is the process going to repeat?
    10. What special error conditions might happen?
  • Start by reviewing this list and answering the questions that seem pertinent to the problem
  • Ask more questions of your own as your analysis continues

Draw a Diagram

  • You have heard the old saying, "a picture is worth a thousand words"
  • Sometimes a visualization of the problem can help you understand it
  • For example, if the problem is about a rectangle then you draw a rectangle

Look for Similarities

  • Often a problem reminds you of other problems you have seen before
  • Recognizing similarities can give you insight into understanding the current problem
  • For example, if you have seen a problem for a list of numbers then you may see similarities when given a problem dealing with a list of names

Separate the Parts

  • If you have a large or complex problem, then you can often break it into smaller pieces that are easier to understand
  • For example, if you have to travel from one location to another, you can plan the journey in steps

More Information

1.3.4: Developing Computer Algorithms

  • A computer algorithm is a set of steps that a computer can use to perform a task
  • Coming up with a step-by-step method for solving a problem is not always easy
  • It is often a trial-and-error process requiring many attempts and refinements
  • This is made more complicated because the computer can only do certain things
  • Our algorithm must only contain steps that a computer can perform

Operations a Computer Can Perform

  1. Get data from a keyboard or other input device
  2. Display data on a screen or other output device
  3. Move data from one location to another
  4. Store data into and retrieve data from memory
  5. Perform arithmetic, such as addition and subtraction, very quickly
  6. Compare two data values for equality or inequality
  7. Process instructions in a sequence (in order)
  8. Process different instructions depending on conditions that are met (if this is true then do that)
  9. Repeat a set of instructions while certain conditions are met

Devising a Plan

  • The plan for solving a problem with a computer is the algorithm
  • The following section describes how to develop a computer algorithm

1.3.5: How to Develop a Computer Algorithm

  • In this section let us look at a step-by-step prcedure for developing a computer algorithm
  • As an example, let us solve the problem:

    You are a business person and need to know the average salary for a group of retail clerks.

  • To solve the problem we will go through five steps as shown below

1. Start with an understanding of the problem

  • The first thing we need to do is write down the essence of the problem in our own words
  • For our example, we can state the problem as, "Compute the average of a list of salaries"
  • Once we have stated the problem we ask questions and refine our understanding
  • Also, we can use the other tools we discussed for Analyzing Problems

2. Identify the inputs and the desired output

  • Most computer problems have inputs and outputs
  • The inputs are the data your algorithm uses
  • The output is the answer you want to the problem
  • In our example we have as input a list of numbers
  • The output we want is the average of the list of numbers

3. Work out the problem by hand

  • Work through an example problem by hand and understand the steps for solving the problem
  • For our example, we need to compute an average of a list of numbers
  • To compute an average we need the sum of all the numbers and a count of the numbers
  • Thus for each number input we need to:
    • Add the number to a total
    • Add one to the count
  • When we are done, we need to divide the total by the count
  • Note that we have broken the main problem down into smaller tasks or steps

4. Describe the steps in psuedocode

  • To solve the problem by hand, we worked through a series of steps
  • To record the steps, we list the steps in psuedocode
  • Psuedocode is an informal description of the steps for solving a problem
  • Psuedocode is often used to describe computer algorithms because you can omit details the computer needs but humans do not
  • This makes puedocode easier for humans to work with than a programming language
  • Working through the problem we have steps like:
    Set total and count to 0
    Get the first number
    Add the number to total
    Add 1 to count
    Get the second number
    Add the number to total
    Add 1 to count
    Get the third number
    Add the number to total
    Add 1 to count
    
  • By this time we see a trend and realize we need to repeat a set of steps indefinitely
  • Thus we rewrite our algorithm to show repetition:
    Set total and count to 0
    For each number
        Add the number to the total
        Add 1 to the count
    Divide total by count
    
  • Note that we are only using operations that a computer can perform

5. Test your psuedocode by working a problem

  • To test our algorithm, we use example data for which we can compute the answer easily
  • For instance, we may use the numbers:
    100
    200
    300
    
  • The average is 200, so we work through our algorithm step-by-step and verify the output

Exercise 1.3b

In this exercise we analyze a problem and develop an algorithm that solves the problem. The algorithm can only use Operations a Computer Can Perform.

Specifications

  1. Work with no more than one other person on this exercise.
  2. Prepare a text file named: algorithm.txt.
  3. Analyze the following problem and find an algorithm to solve it.
  4. Record your analysis and algorithm in the text file.
  5. Do not forget to upload your file to Blackboard when finished.
What is the paycheck amount (before taxes) for an employee who is paid hourly? You may assume that the employee does not get paid extra for overtime.

Check your answer

1.3.6: Summary

  • Programming is about solving problems using a computer program
  • Programs are best developed in two phases:
    1. Problem-solving phase
    2. Implementation phase
  • In this section we looked at the first phase: problem solving:
    • We will look at the second phase in the next section
  • First step to take when solving a problem is to analyze the problem
  • Second step is to develop an algorithm that solves the problem
  • Only after verifying that the algorithm works do you translate it to C++

Check Yourself

Answer these questions to check your understanding. You can find more information by following the links after the question.

  1. What is a computer? (1.3.1)
  2. What is a program? (1.3.1)
  3. What expertise must you develop to write programs well? (1.3.2)
  4. What are the two main phases of the program design process? (1.3.2)
  5. What is the output of the problem solving phase? (1.3.2)
  6. What is the first step to take when developing a program? (1.3.3)
  7. Why is the problem-solving phase important? (1.3.3)
  8. What is an algorithm? (1.3.4)
  9. What are the steps for developing a computer algorithm? (1.3.5)

Wrap Up

Due Next:
A1-Introductions and Algorithms (2/18/10)
  • When class is over, please shut down your computer
  • You may complete unfinished exercises at the end of the class or at any time before the next class.
Home | Blackboard | Day Schedule | Eve Schedule
Syllabus | Help | FAQ's | HowTo's | Links
Last Updated: February 07 2010 @12:14:39