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


Relevant Reading: Chapters 1 and 2




  1. Introductory course materials

    1. Course syllabus

    2. Lecture notes week 1 (these notes)

    3. Lab 1 writeup

    4. Program 1 writeup


  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 with each other using natural languages, like English, Spanish, etc.

        2. Humans communicate with computers using programming languages, such as C, Java, etc.

      2. Computer programs must be written in very simple and precise steps, that are much more basic than natural language communication.

        1. When writing a computer program, one cannot assume the large amount of human knowledge that is generally assumed when communicating with another person.

        2. For this reason, computer programs can be quite tedious to write correctly.

      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 (e.g, the game of Jeopardy! ).

      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. (Actually, some studies have found that humans can tolerate quite a lot of grammatical error in discourse.)

        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 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, but writing testing programs.


  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, when not 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 not delve much further into the structure of a computer, or the 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 treat both the compiler and the computer mostly 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. the program can be translated into the lower-level machine language that the computer understands

        2. everything that goes on inside the computer when we tell it to run a translated program

    4. It is worth noting that C is a somewhat lower-level language, compared to other languages, like Java.

      1. C is certainly higher-level than machine language, however on a relative scale, it is lower-level than many other programming languages.

      2. This means that programmers are "closer to the machine" when programming in C.

      3. In particular, the way in which C programs deal with computer memory is more basic than many other languages, and this will figure into our programming discussions throughout quarter.


  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) a dominant language around the world?

      2. Why is C (at present) a 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, computer scientists are hard pressed to argue that C is the best programming language.

        1. Like English, C has a confusing syntax, and is rather difficult to master.

        2. When combined with the UNIX C library, C is a very large language and somewhat irregular language.

    3. 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 , since these are both quite elegant languages.

      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 -- an elegant and well-structured 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 our first example, 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 "Duh -- 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. First off, are we completely we clear what "positive" means -- is it >= 0 or strictly > 0?

      2. What do you mean by "look at" -- are you going to write the number on a piece of paper, tell it to me orally, point to it on the board?

      3. What does "tell me" the answer mean -- write it on paper, say it orally, write on the board?

    3. Such questions all relate to getting the problem statement clearly defined, that is the problem specification.

    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 like C:

      PROGRAM INSTRUCTIONS:               EXPLANATORY COMMENTS:
      
      begin
      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 <stdio.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 */
      
          scanf("%d", &x);          /* Input the number typed on the screen */
      
          if (x > 0)                /* Check if the number is > 0 (i.e., positive) */
              printf("yes");        /*   If so, output "yes" to the screen */
          else                      /*   If not, */
              printf("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 (x > 0) in the if statement, the program decides which one of two possible actions to take.




index | lectures | handouts | programs | labs | examples | solutions | grades