Title: CS101E Introduction to Computer Science with Engineering Applications
1CS101EIntroduction to Computer Science with
Engineering Applications
- Professor Jim Calvin
- GITC 4311
- calvin_at_cis.njit.edu
- x3378
2Today
- What are computers?
- Programming Languages.
- Rudiments of C.
3Computers are
- Systems (made up of two or more components acting
together ) - Contains electrical mechanical components
- Electrical main memory (RAM), Central
Processing Unit (CPU), and some output devices
like monitors - Secondary memories (floppy, disk and CD-ROM
drives) are the mechanical devices.
4(No Transcript)
5(No Transcript)
6Random Access Memory (RAM)
7Storage Terminology
- Byte (B) 8 bits
- Kilobyte (KB) 1024 bits
- Megabyte (MB) 1,048,576 bits
- Gigabyte (GB) approx. 1.07 billion bits
- Terabyte (TB) approx. 1.1 trillion bits
8Computers are simply tools (like paper, pencil,
eraser, etc.) used for problem solving. We will
be discussing problem solving later.
9In order to solve problems we must instruct
the computer, providing the necessary
information including how to solve, what data to
manipulate, what to output for our use,
etc. Computers do not (yet) fully
understand human language our instructions must
be written in a special computer language.
10Programming Languages
- High-level Languages
- Assembly Language
- Machine Language
11Low Level Languages
12High-Level Languages
- FORTRAN (Formula translation) scientific
programming. - Lisp (List processing) artificial intelligence
- Pascal instruction
- C systems programming
- C object-oriented programming
- Java computer networks
13A Simple C Program
- include ltiostream.hgt
- int main()
-
- int x,y,z
- x 3
- y 7
- z xy
- cout ltlt z // output is 21.
- Return 0
14High-level language programs are usually
translated to machine language programs by a
program called a compiler. Sometimes, high-level
languages may be converted to an
intermediate-level code that is interpreted by a
special program called an interpreter. We will
be using a compiler.
15The Edit-Compile-Link-Execute (Run)-Cycle
16So far we have briefly looked at the hardware
components of a computer and seen the various
steps in a Edit-Compile-Link- Execute-Cycle.
Next we will take a first look at the
high-level language C. .
17Chapter 2 - C Programming Basics
- Understanding your first program
- Identifiers
- Variables
18First.cpp
- //Filename first.cpp
- //Your first C program.
- include ltiostream.hgt
- int main()
-
- cout ltlt "Let us have fun writing C!"
- return 0
-
19- Function Type
- Function Name
- int main()
- //beginning of main function
- .......... //Program statements
- //start here
- ..........
- ..........
- return 0 //0 is used to indicate
- // normal completion
- //end of main function
20Program Statements The most fundamental unit of
a programming language and usually instructs the
computer to do something. Each of them end with
a semicolon E.g. cout ltlt "..."
21cout for output Notice from the previous
example that cout ltlt "..." is used to output to
the standard device, namely, the monitor. Do
not worry about how cout works except to know
that cout ltlt ... can be used to output to the
monitor. DON'T USE the C command printf(...)
although many books and progrmmers still do!
22Double quotes "" are used to output the
enclosed string of characters. The symbol \n
causes a new line. For example, cout ltlt Roses
are red,\nViolets are blue\n gives Roses are
red, Violets are blue
23 Use white space (blanks, blank lines, tabs) to
make programs easily readable.
24Comments Comments are used to clarify programs
use them freely. Comments are for you as well as
for others because after a few days even you
won't understand what your program does without
the comments. Take my word for it and you won't
be sorry (often marks are taken away if you do
not have sufficient comments)!
25Comments in Industry There are many industries
where a software engineer (programer)'s work is
checked automatically everyday! If they do not
have enough comments (usually 30 to 60 of the
program lines written) then either their
programs are not accepted or the programer is
simply fired! Comments are considered heavily
in evaluating how maintainable a program is . Rx
Appendix E (section on maintainance).
26C Comments Styles Style 1 Double slash
// //This is a comment The above style is used
to write short comments and anything written
until the end of a line after a // is considered
a comment and is ignored by the compiler.
27Style 2 For long comments that span several
lines, use / at the start and / at the
end. E.g. / This is a long comment to save
my job in this company.... ..... /
Note that C compiler will ignore
anything written between / and /
28Preprocessor Directives Anyline starting with
is a preprocessor directive. E.g. include
ltiostream.hgt simply asks the preprocessor to
include the file iostream.h at this line before
compilation. This file is a header file required
if you want to use cout, etc. For now do not
worry about these and simply learn to use them as
our example programs do.
29Identifiers Names given to variables,
functions, classes, objects, etc are considered
user-defined identifiers. Can be as long as you
want (although most compilers will recognize
only the first 32 characters) and must start with
one of the following. _ (underscore) or an
alphabet (lower or uppercase) Digits 0 to 9 can
be used for other than the first character. NO
SPECIAL CHARACTERS (e.g. - "space" ....) ARE
ALLOWED!
30We will refer to these as variable names
or function names or class names or .... Also
you can not use names (or words) from the
reserved word list as your identifiers because
they have special meanings to the compiler.
Consult a reserved word or keyword list (APPENDIX
B) if you are in doubt.
31VARIABLES Computers store data as bits (0s and
1s) but the most common method to acces them in a
program is through variables. Each variable has
a unique type and name (identifier). Common
types Integers (int in C) E.g. 254 is an
integer constant (0254 is not equivalent to 254
because any number starting with 0 is considered
an octal (base 8) number!
32Examples real number or floating point
number (in C float) 3.141 0.0001 1.3e5, 1.3e05
(engineering or exponential
or scientific notation) Character constants
(in C char) A, B, and Z are different from a,b,
and z
33Any acceptable identifier can be a variable
name. But to make the variable names meaningful
in the context of the program you are writing is
very important! John_Smith is more specific and
preferable to a_guy or me As a convention use
all capitals if you are referring to a constant
which does not change during the execution of the
program. e.g. MAXSIZE
34Variable Types We have seen so far
int float char and there are others (you can
refer to the table in the text). Each type
requires different number of bytes to store them
in computer memory.
35(No Transcript)
36We will use the following convention to denote
the memory allocation of variables in the
computer memory. This will help us understand the
state of the program and its correspondance to
variables and their contents better.
37(No Transcript)
38Defining Variables In C variables can be
accessed only if they have been defined as
in //Filename variable.cpp //Demonstration of
how to define different //variable types. int
main() char characterA, tabchar int
studentnumber float average return 0
39CPR (class room programming) Exercise Show the
memory contents at the time of the execution of
this program just before it exits. Definition
of a variable does not guarantee any legal
values as its contents!
40Homework 1. Due email to cs101e_at_cis.njit.edu
by midnight next Sunday. Write a C program
that prints out (on separate lines) your name,
major or main field of interest, and your student
number.