Contents

advertisement
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
8 April 2011
Kiet T. Tran
Contents
1
Announcements .......................................................................................................2
1.1
General ...............................................................................................................2
1.2
Course structure ................................................................................................2
1.3
References ..........................................................................................................2
2
Introduction to Computer ......................................................................................3
3
Introduction to Web ................................................................................................4
4
Introduction to Computer Science ........................................................................5
5
Programming in C ...................................................................................................5
6
5.1
Overview............................................................................................................5
5.2
Setup Cygwin for windows .............................................................................6
5.3
C basics ...............................................................................................................6
5.3.1
hello.c program........................................................................................6
5.3.2
Primitive data types ................................................................................7
5.3.3
Intermediate hello.c program ................................................................7
5.3.4
io.c program .............................................................................................9
5.3.5
Conditional statement in C ....................................................................9
5.3.6
Array in C .................................................................................................9
5.3.7
Reading from file ................................................................................... 10
Exercises ................................................................................................................. 11
6.1
Reverse string .................................................................................................. 11
6.2
Counting .......................................................................................................... 11
6.3
Crypto............................................................................................................... 11
7
References ............................................................................................................... 11
8
Credits ..................................................................................................................... 12
1
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
1
8 April 2011
Kiet T. Tran
Announcements
1.1
General

This course is an introduction course in computer science.

It is CSC 111 at Trident University International (TUI).

CSC 111 has six modules. Module 1 through 5 lasts 2 weeks each and
module 6 1 week.

This course is not quite a hands-on programming; however, I would
like to offer you opportunities to write program early on in the course.
My thinking is that you are in a computer science (CS) degree, soon or
later you will be immersed yourself in solving difficult CS related
problems. The sooner you are familiar with programming tools, the
better you are prepared for a career in computer related career even if
you will not write any program at work.

Contact information: ktran@tuiu.edu. I normally respond to my
students within 24 hours.

Any concern for this course, please contact me via email. Please avoid
use COURSENET to leave me comments or questions. Unfortunately,
COURSENET does not always notify me your comments.

I
use
my
personal
blog
to
put
additional
materials
there
(http://www.bemach.com/CSC111). I will notify you if you need to visit
to download additional materials for the course.
1.2
Course structure

Module 1: introduction to computer science degree – we will learn
about computer, the web, and computer science degree.

Module 2: introduction to networking and the Internet – we will learn
how computer talk with one another and dig a bit deeper into the
Internet.

Module 3: Introduction to computer hardware – We will learn more
about computer hardware and its capabilities.

Module 4: Introduction to computer software – we will learn about
computer applications.

Module 5: Introduction to python programming language – we will
learn writing Python.

Module 6: Reflection – write an essay to reflect what you have learned
during this course.
1.3
References
This course does not require textbooks; however, I use the following books in
preparing my notes and assignments:
Important links:

2
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
2
8 April 2011
Kiet T. Tran
Introduction to Computer

Computer is a data processing device or a programmable machine that
can carry out data processing tasks such as:

o
Gather data
o
Process data into information
o
Output data or information
o
Store data or information
Data and information are two different concepts. Data represents fact
while information has meaning.

A digital computer understands only binary language, namely 0 and 1.
A binary digit or bit has a value of 0 or 1. This language has very little
use for us human; however, it is the foundation of all computers in
existence today.

A byte is made up by 8 bits that can represent a character of data. A
byte can have a value between 0 and 255 inclusively. How can it be?

The next unit of accounting in computer is a kilobyte. A kilobyte has
1024 bytes. It is odd, isn’t it? A kilogram, as we’d learned in high school,
has 1,000 grams. So, why 1 kilobyte (KB) is equal to 1,024 bytes?

Other units of measurement are:
o
1 Megabyte (MB) = 1,024 KB
o
1 Gigabyte (GB) = 1,024 MB
o
1 Terabyte (TB) = 1,024 GB
o
1 Petabyte (PB) = 1,024 TB
o
1 Exabyte (EB) = 1,024 PB
o
1 Zettabyte (ZB) = 1,024 EB

Hardware is any part of the computer that you can physically toched.

Software or application software is a set of programs that you can use to
perform some tasks. For example, you are using Microsoft Word,
application software, to type your term paper.

System software allows your computer system and your application
software to work together to achieve your objectives. An Operating
System is the most commonly used system software.

A computer system normally comes with a system unit and peripheral
devices (input and output)

Input devices are keyboards, mice and pointing devices, and sound
input.

Output devices are monitors, printers, sound output.

System unit incudes power supply, storage devices, hardware
interfaces, and motherboard.
o
Storage devices include hard drive, CD, CD-RW, DVD, DVDRW, blue-ray, flash memory card, and flash drive.
o
Interfaces can be serial ports, parallel ports, USB ports,
Ethernet port, audio port, video ports, modem port, and
FireWire ports.
3
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
o
8 April 2011
Kiet T. Tran
Motherboard contains a set of chips that powers the system.
Motherboard includes central processing unit (CPU), memory
modules (random access memory or RAM) and extension slots.

CPU or processor is the largest and the most important chip. It is the
‘brain’ of the computer

RAM is the primary storage that will be lost when the computer is
powered down. RAM provides quick access to data for the CPU.

Hard drive is one form of permanent storage that remains after the
computer is powered down.
3
Introduction to Web

For many, it is hard to imagine life without the Internet because
everything they do requires it. I am one of these people.

The Internet was born from a US government funded project called
Advanced Research Projects Agency Network or ARPANET for short.
The network connected the research centers of four universities: UCLA,
Stanford, UCSB, and University of Utah in Salt Lake City. The first
established connection occurred in 1969.

Vincent Cerf and Robert Kahn are thought of the fathers of the Internet
because they were primarily responsible for the development of many
of the initial internet protocols.

Worldwide Web (www) is different from the rest of the Internet use this
way:

o
Computer-to-computer communications
o
A hyperlink enables one jump from one computer to another.
In 1989, Tim Berners-Lee from European Organization for Nuclear
Research (CERN) invented the WWW. You can look around the Internet
using a web browser such as FireFox, Google Chrome, Microsoft
Internet Explorer, Opera, and others. A browser is a software
application that enables you to interact with textual data and other
media on the Web.


o
URL
o
Hyperlinks
o
Favorites, live bookmarks, and tagging
o
Search engines
Communicating technologies:
o
Instant messaging (IM)
o
Podcasts and webcasts
o
Weblog (blog)
o
Wikis
o
Voice over IP (VOIP)
o
Emails
Web entertainment allows you to watch movies and listen to music over
the Internet.
4
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science

8 April 2011
Kiet T. Tran
E-commerce enables businesses to conduct commerce activities with
consumers and other businesses over the Internet.

4
All these come with some troubles:
o
SPAM
o
Malware, adware, and spyware
o
Phishing and Internet hoaxes
Introduction to Computer Science

Let us make it clear that computer science is not about programming
languages but about how to solve problems. It is a study of information
and computation.

CS was born in 1940 with confluence of algorithm theory, mathematic
logic, and the invention of stored-program electronic computer
(Denning, 1999).

CS is surrounded by
o
The structure and operation of computer systems
o
Principles
underlying
computer
system
design
and
programming
o
Effective methods for using computers for information
processing tasks
o

Theoretical characterizations of their properties and limitations
Programming language is a tool to instruct the computer to perform
data process tasks for you.
5
Programming in C
Important notes, please read carefully:

Programming in C is not required for this class; however, many
demonstrations are convenient in C.

I encourage all students to program in C in order to begin appreciation
of what computer software can do for us.

Thus, here is my proposal:
o
You continue to do the assignments as defined in the CD
o
You perform additional programming assignments for extra
credit.
o
For those who want more challenging assignment can email
me for more complex programming assignment instead of the
traditional writing case and SLP. In other words, if you want to
get in programming right away, I can assist you.

You may ask, why C? Why do we not use Java, python, or other
languages? It is because C can perform low-level function that is not
available in many high-level languages.
5.1
Overview

C was a popular programming language in 1970 and 80 for UNIX users.
5
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science

8 April 2011
Kiet T. Tran
C is a general-purpose computer language developed by Dennis Ritchie
at Bell lab in 1973. It is hard to imagine the language is nearly 40 years
old! It is still useful for many tasks today.

C is not an object-oriented programming language. It is procedural or
imperative.

The principle design of C was minimalism in that the goal is to do
enough and encourage portable across platforms.

In order to program in C, you need a C compiler. We will use gcc
compiler for the example of this course.
5.2
Setup Cygwin for windows

Cygwin (now owned by redhat) provide a UNIX environment for
Windows.

Visit http://www.cygwin.com and install Cygwin for your windows
(choose setup.exe option from the main menu). The current version is
1.7.9-1. Choose default installation. It will come with gcc compiler and a
basic vi editor. You can use notepad text editor if you wish.
5.3
C basics
5.3.1
hello.c program
A very basic C program is show as follows:
#include <stdio.h>
void main (void) {
printf ("hello, world!\n");
}
Figure 1: hello.c

The above program, when runs, will print a simple line ‘hello, world!’
on screen. That is it. It is not very useful; however, if you get it to work,
you just took the first step in programming a computer. You wrote a
computer program or software that does something for you even
though it is a useless thing.

All C program starts out with one method called ‘main.’ A C program
that does nothing can be written as simply as ‘void main (void){}’.
That is a valid C program!

printf is a library function that print out a sequence of character
ended with a new line (‘\n’).

The following steps instruct you how to write that C program, compile
and link it to make an executable program for Windows or other
operating systems such as a Linux, MAC OS, or UNIX.
1.
Use your favorite basic text editor (notepad or wordpad).
Don’t use MS Word.
2.
Type in the above program and save it as hello.c
3.
Go to Cygwin bash command prompt that you just download.
When you go to a Cygwin bash shell, it places you in
/home/xyz where xyz is your name. The actual location of this
6
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
8 April 2011
Kiet T. Tran
directory is from c:/Cygwin directory (if you takes the default
install of the Cygwin program from steps 5.2).
4.
Go to the directory where you save the hello.c file.
5.
Run this: gcc –o hello hello.c
6.
The output will be ‘hello.exe’ for Windows and hello for other
Unix flavor operating systems.
7.
To run: ./hello from the current directory.
8.
You should see a message ‘hello, world!’ printed on your
screen.
5.3.2
Primitive data types

char – represents single byte (8 bits).

short – represents two byte

int – represents an integer that can be either 16 bit or 32 bit long.

long – represents an integer 32 bit long.

double – a double-precision floating point 64 bit long

float – a floating point 32 bit long

other types can be used:
o
short int (16 bits)
o
unsigned short int (16 bits)
o
unsigned int (16 bits)
o
long int (32 bits)
o
signed char (8 bits)
o
unsigned char (8 bits)
o
long double (96 bits)

In C, you must declare all your variables before use.

In C, you must specify a function prototype prior to use. This can be
done by including a .h file (header file) or by simply declare it before
use. To avoid the function prototype, you can define the entire function
and place it on top of the calling function.
5.3.3
Intermediate hello.c program
Consider the updated version of hello.c program:
#include <stdio.h>
#include "add.h"
void main () {
int val = add(1,2);
int i;
for (i = 0; i < val; i++) {
printf ("hello, world!\n");
}
return;
}
Figure 2: hello.c
int add(int va1, int va2);
figure 3: add.h header file
7
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
8 April 2011
Kiet T. Tran
#include <stdio.h>
int add (int val1, int val2) {
return val1+val2;
}
Figure 4: add.c

The updated version hello.c gets a bit more complicated but it is still
useless. It teaches us several things about C, though.

It shows us how to call a C function namely ‘add’.

It shows us how to define a variable before use, namely val and i.

It shows us how to repeat tasks multiple times using ‘for’ expression.

To increase a value by 1, use ++ after or before an int, short, or long
variable. ++i has different consequence than i++. ++i tell the computer to
add 1 to the variable before use while i++ tells the computer to use the
variable, then add 1 to it.

The function ‘add’ receives two integer values and returns an integer
value.

The header file such as add.h is often used to define the function
prototypes.

These two segment of code does the same thing in different way:
While loop:
1.
2.
3.
4.
5.
int i = 0;
while (i < val) {
printf ("hello, world!\n");
i++;
}
1.
2.
3.
4.
int i;
for (i = 0; i < val; i++) {
printf ("hello, world!\n");
}
For loop:

Which one do you prefer? Perhaps, the while loop is easier to read at
first? You loop through the activity until the value of i I is equal or
larger than val value.

A function is a convenient way to encapsulate some computation. It is
similar to that procedure in Pascal, a subroutine in Fortran.

A function can take input parameters of various data types and return a
variable. A function that does not return anything must be of type
void.
8
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
5.3.4
8 April 2011
Kiet T. Tran
io.c program
Consider this simple input and output program. It reads an input character from
keyboard and put it back on screen until end-of-file (EOF). In windows EOF is
ctrl-d.
#include <stdio.h>
/* anything in here is ignored by the compiler */
// anything after this symbol ignored until the end of the line.
void main (void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
Figure 5: io.c program

!= means not equal.

Function getchar gets a character from the keyboard.

Function putchar puts a character on screen.
5.3.5
Conditional statement in C
Consider this example:
#include <stdio.h>
void main (void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
if (c == 'c')
putchar('*');
else if (c == 'd')
putchar('!');
c= getchar();
}
}
Figure 6: io.c with conditional statement
The result is:
$ ./io
cabd
c*abd!
Figure 7: output of io.c

== is equal to (note that there is TWO =s)

If the input character is ‘c’, I put an extra character ‘*’ on screen.
Similarly if the input character is ‘d’, I put an extra character ‘!’ on
screen.
5.3.6
Array in C
Consider an example that uses array of integers:
#include <stdio.h>
9
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
8 April 2011
Kiet T. Tran
void main (void) {
int myIntList[10];
myIntList[0] = 1;
myIntList[1] = 2;
myIntList[2] = 3;
myIntList[3] = 4;
myIntList[4] = 5;
myIntList[5] = 6;
myIntList[6] = 7;
myIntList[7] = 8;
myIntList[8] = 9;
myIntList[9] = 10;
int i, val = 0;
for (i = 0; i < 10; i++) {
val += myIntList[i];
}
printf ("val = %d\n", val);
}
Figure 8: array.c
$ ./array
val = 55
Figure 9: array.c output

Array a sequence of occurrence of the same data type. The position of
the index always starts at 0.

The program sets up a list of integer values, then add all of them. The
sum is printed at the end.

%d is to print out the value as an integer value. Other formatting
characters are:

5.3.7
o
%c for char
o
%d for int
o
%f for float
o
%lld for long long
o
%s for string.
Escape character is backslash (‘\’).
Reading from file
Consider a simple exercise that reads data from a file and then put it on screen:
#include <stdio.h>
typedef char * string;
void main (void) {
string fname = "/cygdrive/c/tmp/hello.c";
FILE *fp;
fp = fopen(fname,"r");
if (fp == NULL) {
printf ("Cannot open file!");
return;
}
int c;
while ((c = getc(fp)) != EOF) {
putchar (c);
}
fclose(fp);
10
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
8 April 2011
Kiet T. Tran
printf ("Done!");
}
Figure 10: fileio.c program
6

*fp is the file handle that is used for reading data from file.

First, you open a file, read data, and then close it.
Exercises
You can submit exercise as your SLP assignment for module 1.
6.1
Reverse string
Write a program that reads an input string from a keyboard, reverse it and print
the original and the reversed strings on screen.
6.2
Counting
Write a program that reads in a text file, print out the following information of
the input:

Print out the number of lines read

Print out the number of characters read

For each character, print the number of occurrences
6.3
Crypto
Write a program that read in a string of text and encrypt it using Caesar cipher
technique that is explained here http://en.wikipedia.org/wiki/Caesar_cipher.
Make sure you can decrypt the string back to the original string. Print out the
input, the encrypted text, and the encrypted text.
Use the same program but read from a file instead of from a keyboard for a
string. Validate your encryption/decryption functions (hint: read one line at a
time as a string). Use fgets function.
Here is your formula for generating a cypher text:
ci = (pi + k) % 26
where:
pi is the plaintext character at position i.
ci is the cipher character at position i.
k is a non-negative integer.
7
References
Introductory Technology in Action sixth edition
The C Programming Language 2nd edition.
11
CSC 111: Foundations of Computers and Program Design
Module 1: Introduction to computer science
8
8 April 2011
Kiet T. Tran
Credits
If you are my student and catch bugs or errors in the document that I use for
enhancement, I will mention your name here with your permission.
12
Download