CSC 101 Lecture Notes Week 1

CSC 101 Lecture Notes Week 1
Introduction to the Course
Introduction to Programming and Problem Solving


  1. Introductory course materials
    1. Course syllabus
    2. Choosing a first CSC course
    3. Lecture notes week 1 (these notes)
    4. Lab notes week 1

  2. What is a program?
    1. In the most simple terms, a program is a list of instructions.
    2. In this sense, humans follow programs frequently, for example
      1. a cooking recipe
      2. a set of directions to get to someone's house
    3. The programs that we write for computers will differ from such human-level programs in a number of important and fundamental ways:
      1. Computer programs are written in a vastly simpler language than human programs; specifically.
        1. Humans communicate programs to one another using natural languages, such as English, French, and Spanish.
        2. Humans must communicate programs to computers using programming languages, such as C, C++, and FORTRAN.
      2. Computer programs must be written in very simple and precise steps, that are much more basic than natural language programs.
        1. When writing a computer program, one cannot assume the large amount of human knowledge that is generally assumed when writing a human-level program.
        2. For this reason, computer programs are considerably more tedious to write than human programs.
      3. The range of problems that can be solved with computer programs is generally simpler and more mundane than the range of problems solvable by humans.
        1. Except in certain specialized areas of knowledge, computers are not (yet) intelligent, in the human sense of intelligence.
        2. Without such intelligence, computers must be programmed to solve problems at a level well below how humans can be programmed (or taught) to solve problems.
        3. Despite a lack of what could be considered human-level intelligence, computers do surpass humans in certain forms of problem solving, most particularly with problems that involve a large amount of numeric computation or other forms of clerical data processing.
      4. Computer programs must always be 100% grammatically correct
        1. When humans communicate with one another in natural language, a certain amount of grammatical error can easily be tolerated.
        2. When humans communicate in a programming language to a computer, absolutely no grammatical errors can be made.
        3. While this is certainly one of the more mundane aspects of computer programming, getting a program completely correct grammatically often occupies a large amount of programming time, particularly for beginning programmers.

  3. What is problem solving?
    1. Problem solving is the process of logically breaking down a problem into three major phases
      1. Stating clearly what the problem is
      2. Defining how the problem can be solved in a logical sequence of steps
      3. Verifying that the solution really does solve the stated problem
    2. When humans solve problems by themselves, they use a vast array of knowledge and understanding to perform these problem solving steps.
    3. When humans use a computer to solve a problem, the solution must be coded in a programming language which, as we noted above, is much simpler than the natural language humans use with one another.
    4. Further, if we think of the human and the computer as a problem solving team, the computer is definitely the junior partner in the team effort; specifically:
      1. The human does all of the problem statement phase of problem solving
      2. The human then outlines a solution in the form of an algorithm, which can be thought of as a high-level program.
      3. The human then encodes the algorithm in a computer programming language.
      4. The computer compiles (i.e., translates) the program into an electronic form that can be executed directly by the computer hardware; this form is called the machine code.
      5. The computer runs the machine code to produce an answer.
      6. The human validates that the answer is correct.

  4. What is a computer?
    1. A computer is an electronic device capable of following programmed instructions of a very rudimentary form, called machine language.
      1. Machine language is even simpler than the kinds of programming languages, such as C++, in which modern programs are written.
      2. The machine-language level of a program is stored in an electronic form directly in the computer memory.
    2. The major components of a computer are the following:
      1. A central processing unit (CPU); this is the component that executes the machine code instructions.
      2. A memory unit, where the instructions are stored when they are being executed.
      3. Peripheral memory, where programs and other data are stored for long-term, i.e., not when they are being executed.
      4. Peripheral input and output devices, which the humans use to communicate with the computer.
    3. From the perspective of CSC 101, we will delve no further into the structure of a computer or into the form the low-level machine language it uses to store programs.
      1. We will use a higher-level programming language, namely C++, which is translated into machine language by a program called a compiler.
      2. We will use both the compiler and the computer as "black boxes".
      3. That is, it is our job in CSC 101, and in general it is the job of most programmers, to code a problem solution in a higher-level programming language.
      4. Programmers take on faith the following:
        1. that the program can be translated into the lower-level machine language that the computer understands
        2. all that goes on inside the computer when we tell it to run a translated program

  5. More on programming languages and natural languages
    1. Why do we speak and program in the languages that we do?
      1. E.g., why is English (at present) the seemingly dominant language around the world?
      2. Why is C++ (at present) the seemingly dominant programming language around the world?
    2. The reasons a particular language rises to (and falls from) popularity may have little to do with the inherent quality of the language, and even less to do with how easy it is to learn and use.
      1. Can anyway really say that English is the "best" language for human communication?
        1. Probably not.
        2. It is large, irregular, and difficult to master.
        3. It has many ways to say the same thing.
      2. By the same token, most computer scientists are hard pressed to argue that C++ is the best programming language.
        1. Like English, C++ is large, irregular, and difficult to master.
        2. It too has many ways to say the same thing.
    3. Unfortunately, as dwellers in the real world we must learn to deal with such languages as English and C++, or we run the risk of not being able to communicate as widely as we would like to.
      1. We might bury our heads in the sand, and choose to speak Latin and Lisp.
      2. If we do so, the number of humans and computers with whom we could communicate would be very limited indeed.
    4. The bottom line about languages is that a good teaching and learning language is not always a durable communication language.

  6. A very simple introductory problem -- determining if a number is positive or negative.
    1. A frequently useful strategy for program problem solving is to consider how we would solve the problem ourself, before we code it in a programming language.
      1. In this case, the problem is sufficiently simple so as to seem almost too easy for a person to solve.
      2. I.e., if one were to tell someone how to solve this problem, the answer might be "just look at the number and tell me if it's positive".
    2. In order to solve this problem with a computer program, we must look at it in more fundamental terms, and address such questions as:
      1. What do you mean by "look at" -- are you going to write the number on a piece of paper, tell it to me orally?
      2. How do you want me to "tell you" the answer -- should I write it down, say it orally?
      3. Are we clear what "positive" means -- is it >= 0 or strictly > 0.
    3. Such questions all relate to getting the problem statement clearly defined.
    4. Once we understand the problem clearly and thoroughly, we can write an algorithm, which can be thought of as a simplified form of program.
    5. To get a concrete feel for the kind of steps involved in an algorithm, here is one for our positive-number problem, presented in a language similar to but somewhat simpler than a real programming language such as C++:

      PROGRAM INSTRUCTIONS:               EXPLANATORY COMMENTS:
      _______________________________________________________________________________
      
      program                               Begin the program
      
          let x be an integer variable      Declare a variable to hold the input
      
          read x                            Read the number typed on the screen
      
          if x > 0 then                     Check if the number is > 0 (i.e., positive)
              print "yes"                     If so, print "yes" on the screen
          else                                If not,
              print "no"                        print "no" on the screen
      
      end                                   End the program
      
    6. Here's the program in C++
      #include <iostream.h>           // The C++ input/output library
      
      int main() {                    // C++ programs always start with this header
      
          int x;                      // Declare a variable to hold the input number
      
          cin >> x;                   // Input the number typed on the screen
      
          if (x > 0)                  // Check if the number is > 0 (i.e., positive)
              cout << "yes";          //   If so, output "yes" to the screen
          else                        //   If not,
              cout << "no";           //      output "no" to the screen
      
      }                               // End of main program (matches the "{")
      
    7. In class, we'll dissect the algorithm and program to see exactly what's going on; the dissection will discuss the following fundamental aspects of a program:
      1. Programs have an explicit beginning and ending, that are clearly delineated.
      2. Programs use variables to hold data values, such as numbers; these variables are similar to variables used in mathematical formulas.
      3. Programs must define precisely how data values are input (i.e., read in) and output (i.e., printed out).
      4. Programming languages have most of the normal forms of arithmetic expressions for adding, multiplying, comparing, etc.
      5. One of the most fundamental constructs in a programming language is the conditional statement, which is used to define how the program makes decisions.
        1. The common syntactic form of a conditional in most programming languages is an "if" statement.
        2. It is used above to decide the basic problem here -- whether the number is > 0.
        3. Based on the condition in the if statement ("x > 0"), the program decides which one of two possible actions to take.


index | lectures | labs | handouts | assignments | solutions | grades | help