1. Introduction to Programming

What We Will Cover


1.1.11: Succeeding in This Course

  • You can learn the course material anyway you like
  • However, by following the guidelines below you will get a clearer understanding of 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

Completing and Submitting Exercises

  • To help you learn the material, I have developed two sets of exercises
  • One set you complete in class and submit to WebCT
    • Make sure you submit your exercises before the due date
    • Late exercises are not accepted
  • Another set of exercises, CodeLab, you need to complete at home
  • Your exercise score is the combination of both sets of exercises each week

Setting Up Your Programming Environment

  • All the software you need runs on almost any computer
  • 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

Completing and Submitting Assignments

  • You should complete all exercises, including CodeLab, before starting an assignment
  • Read the instructions carefully before attempting the assignment
  • If the assignment directs you to web sites, then read that information as well
  • Think about the design of your program and work out the algorithms in advance
  • When you have analyzed the problem and developed a plan, then start writing the code
  • Make sure you submit your assignments before the due date
  • Late assignments are not accepted
  • Submitting anything earns at least partial credit

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.1.12: Summary

  • www.edparrish.com has a link to the course web site and has contact information for the instructor
  • Course information 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 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 first need to understand the material
    • Each lesson has Learner Outcomes that you should focus on while reading the book and attending lectures
    • When the lecture is finished, I suggest that you review the summary and answer the Check Yourself questions
  • To help you learn the materials, you need to complete the exercises
    • You complete some exercises in the classroom during class time
    • You complete the CodeLab exercises outside of class time
  • To make assignment easier to complete, I suggest that you set up a programming environment
    • 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 need to read assignments carefully and developed a plan before you start writing the code
  • Also, make sure you submit your assignments before the due date
    • Late assignments are not accepted
    • Submitting anything earns at least partial credit
  • And finally, you need to study before taking an exam
    • I will discuss study techniques before the exams

Check Yourself

  1. Does the lecture alone cover everything you might need in a homework assignment?
  2. How much time should you expect to spend on homework?
  3. Where can you work on your programs while you are at Cabrillo?
  4. What software do you need to use for your programming assignments?
  5. Where do you go to find the software to install?
  6. How much should a student expect to pay for the software needed for homework?

Exercise 1.1

Use the next 10 minutes to complete the following.

  1. Join a group of 3-4 people and prepare an answer to one of the following Exercise Questions.
  2. Select one member from your group as a representative to describe your answer to the class.

Exercise Questions

  1. What "burning questions" do you have about the course outline?
  2. What do you hope to learn by the time you complete this course?
  3. How do you plan to balance the work required by this course with the other activities in your life?

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: Programming and Problem Solving

Learner Outcomes

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

  • Describe the programming process
  • Solve simple computational problems

1.2.1: What Programming is About

  • Programming is about solving problems using a computer program
  • A few terms and definitions you need to learn:
  • Algorithm: A sequence of precise instructions which leads to a solution

    Computer programming: An algorithm expressed in a language the computer can understand

  • 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
      • Result is an algorithm that solves the problem
    2. Implementation phase
      • Result is the algorithm translated into a programming language
  • Each of these phases has several general steps to them

  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.2.2: 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

Paraphrase 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

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?
  • Try reviewing the list and answering the questions that seem pertinent

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.2.3: Developing 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

  • Get data from a keyboard or other input device
  • Display data on a screen or other output device
  • Move data from one location to another
  • Store data into and retrieve data from memory
  • Perform arithmetic, such as addition and subtraction, very quickly
  • Compare two data values for equality or inequality
  • Process instructions in a sequence (in order)
  • Process different instructions depending on conditions that are met
  • Repeat a set of instructions while certain conditions are met

Devising a Plan

  • There are several approaches to solving problems algorithmically
  • We will look at several during this course
  • One approach you may find useful is: Using a Formula

1.2.4: Using a Formula

  • Formulas are a powerful tool we can borrow from mathematics
  • Oftentimes, you can easily solve a computing problem using a formula
  • For an example, we will use the problem:
  • Find out how big around a rectangle is.

Starting the Analysis

  • We first start by rephrasing the problem in our own words
  • We state our problem as precisely as we can:
  • Calculate the perimeter of a rectangle.

Understanding the Problem

  • We continue our analysis by trying to understand the problem in more depth
  • We select some of the questions that seem pertinent to the problem
    1. What output is expected?
    2. What calculation must be performed?
    3. What data is needed for the calculations?
    4. Where will I get the data I do not have?
  • What are the answers to these questions for our example problem?
  • Thinking about these questions improve our understanding of the problem
  • We can see that we can solve the problem by using a formula

Defining the Algorithm

  • Once we understand our problem sufficiently, we develop an algorithm
  • Remember that we must define our steps using only the operations that a computer can perform
  • For our example problem, we might define our algorithm as:
    1. Get the length of the rectangle
    2. Get the width of the rectangle
    3. Calculate the perimeter using the equation:
      perimeter = 2 * (length + width)
    4. Display the result of the calculation
  • We must test the algorithm to be certain it is correct

1.2.5: Processing a List

  • Let us look at another problem-solving example
  • One problem that computers are good for solving is to process a list of data
  • For an example, we will use the problem:
  • Count the number of times that a name occurs on a list.

Analyzing the Problem

  • What is a good problem statement?
  • Next, we ask ourselves some questions to improve our understanding
    1. What output is expected
    2. What data do I have to work with?
    3. What do the data items look like?
    4. How will I know when I have processed all the data?
    5. How many times is the process going to repeat?
  • As we ask these questions, more questions occur to us like:
    1. How many names are on the list?
    2. What name are we looking for?
  • After answering these, we should be ready to develop a solution

Defining the Algorithm

  • One possible algorithm is:
    1. Get the list of names
    2. Get the name to check
    3. Set a counter to zero
    4. For each name on the list
      1. Compare the name being checked to the name on the list
      2. If the names are the same then add one to the counter
    5. When done, display the value of the counter
  • Once we have an algorithm, we need to test it for correctness

1.2.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

  1. What is the first step to take when creating a program?
  2. What are the two main phases of the program design process?
  3. What is the importance of the problem-solving phase?
  4. What is an algorithm?

Exercise 1.2

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 WebCT when finished.
  6. 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

Wrap Up

Due Next:
A1: Information Please! (2/15/07)
Exercise 1 and CodeLab Lesson 1 (2/15/07)

Home | Blackboard | Announcements | Day Schedule | Eve Schedule
Course info | Help | FAQ's | HowTo's | Links

Last Updated: February 16 2007 @16:16:14