Week 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSC101-s12-Lecture 1 Page 1

CSC 101 Lecture Notes Week 1


Introduction to the Course
Introduction to Programming and Problem Solving
Relevant Reading: Chapters 1 and 2

I. Introductory course materials


A. Course syllabus
B. Lecture notes week 1 (these notes)
C. Lab 1 writeup
D. Program 1 writeup

II. What is a program?


A. In the most simple terms, a program is a list of instructions.
B. In this sense, humans follow programs frequently, for example
1. a cooking recipe
2. a set of directions to get to someone’s house
C. The programs that we write for computers will differ from such human-level programs in a number of impor-
tant and fundamental ways:
1. Computer programs are written in a vastly simpler language than human programs; specifically.
a. Humans communicate with each other using natural languages, like English, Spanish, etc.
b. 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 natu-
ral language communication.
a. When writing a computer program, one cannot assume the large amount of human knowledge that is
generally assumed when communicating with another person.
b. 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 mun-
dane than the range of problems solvable by humans.
a. Except in certain specialized areas of knowledge, computers are not (yet) intelligent, in the human
sense of intelligence.
b. 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.
c. 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.
a. 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.)
b. When humans communicate in a programming language to a computer, absolutely no grammatical
errors can be made.
c. 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.

III. What is problem solving?


CSC101-s12-Lecture 1 Page 2

A. 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
B. When humans solve problems by themselves, they use a vast array of knowledge and understanding to per-
form these problem solving steps.
C. 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.
D. 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.

IV. What is a computer?


A. 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.
B. 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.
C. 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 solu-
tion in a higher-level programming language.
4. Programmers take on faith the following:
a. the program can be translated into the lower-level machine language that the computer understands
b. everything that goes on inside the computer when we tell it to run a translated program
D. 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
CSC101-s12-Lecture 1 Page 3

languages, and this will figure into our programming discussions throughout quarter.

V. More on programming languages and natural languages


A. 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?
B. 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?
a. Probably not.
b. It is large, irregular, and difficult to master.
c. 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.
a. Like English, C has a confusing syntax, and is rather difficult to master.
b. When combined with the UNIX C library, C is a very large language and somewhat irregular lan-
guage.
C. 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 ele-
gant languages.
2. If we do so, the number of humans and computers with whom we could communicate would be very lim-
ited indeed.
D. The bottom line -- an elegant and well-structured language is not always a durable communication language.

VI. A very simple introductory problem -- determining if a number is positive or negative.


A. 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".
B. 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?
C. Such questions all relate to getting the problem statement clearly defined, that is the problem specification.
D. Once we understand the problem clearly and thoroughly, we can write an algorithm, which can be thought of
as a simplified form of program.
E. To get a concrete feel for the kind of steps involved in an algorithm, here is one for our positive-number prob-
lem, presented in a language similar to but somewhat simpler than a real programming language like C:
CSC101-s12-Lecture 1 Page 4

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

F. 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 "{") */

G. 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.
a. The common syntactic form of a conditional in most programming languages is an "if" statement.
b. It is used above to decide the basic problem here -- whether the number is > 0.
c. Based on the condition (x > 0) in the if statement, the program decides which one of two possible
actions to take.

You might also like