CSC 223-Computer Programming I
CSC 223-Computer Programming I
Course Description
This course will introduce you to the field of computer science and the fundamentals of
computer programming. CSC 223 is specifically designed for students with little or no prior
programming experience, and touches upon a variety of fundamental topics.
We begin this course with a brief explanation of program (software) development, and show how
human thought and computer programming are related. We build upon these general concepts to
concepts of programming like algorithm, variables flowcharts, etc. During this process, we use a
programming language to show how those fundamentals are implemented in a real programming
environment. We do this by demonstrating major primitive data types, relational operators,
control statements, exception handling, and file input/output.
By the end of the course, you will understand the basics of computer science and the basics of
programming using C# programming language. The principles you learn here will be developed
further as you progress through the computer science discipline.
Program Development Processes
One of the main of computer science is to develop computer programs to solve real life problems.
Computers cannot solve problems on their own. One has to providestep by step solutions of the
problem to the computer. In fact, the task of problem solving is not that of the computer. It is the
programmer who has to write down the solution to the problem in terms of simple operations which the
computer can understand and execute. In order to solve a problem by the computer, one has to pass
though certain stages or steps. They are:
1. Understanding the problem: Here we try to understand the problem to be solved in totality. Before
we proceed with the next stage or step, we should be absolutely sure about the objectives of the given
problem.
2. Analyzing the problem: After understanding thoroughly the problem to be solved, we look at
different ways of solving the problem and evaluate each of these methods. The idea here is to search an
appropriate solution to the problem under consideration. The end result of this stage is a broad
overview of the sequence of operations that are to be carried out to solve the given problem.
3. Developing the solution: Here the overview of the sequence of operations that was the result of
analysis stage is expanded to form a detailed step by step solution to the problem under consideration.
4. Coding and implementation: The last stage of the problem solving is the conversion of the detailed
sequence of operations in to a language that the computer can understand. Here each step is converted
to its equivalent instruction or instructions in the computer language that has been chosen for the
implantation.
Algorithm
An algorithm is a sequence of instructions which are designed in such a way that if they are executed in
the specified sequence, the desired goal is achieved. It is important that we obtain a result after an
execution of as many steps as possible. In an algorithm each instructions has to be precise and clear.
When the algorithmterminates and the desired result should be achieved. The example
heredemonstrates that the order of specifying the order of actions to execute isimperative.
Example
This step gets the tea done exactly how it was intended. Suppose the same steps were performed in a
different order:
Example
In this case we would have a cold tea. Specifying the order each step or actions are carried out is very
important. Consider some examples algorithms for Simple problem.
Example
Solution:
Some of the operations that can be carried out on an algorithm include arithmetic, logic and relational,
before writing an algorithm for a problem, one should find out what is/are the inputs to the algorithm
and what is/are expected output after running the algorithm. Now let us take some exercises to develop
an algorithm for some simple problems: While writing algorithms we will use following symbol for
different operations:
‘=’ for assignment. For example A= X*3 means A will have a value of X*3.
Example
Solution:
Step 1: Read a, b, c
Step 2 sum = a+ b+ c
Example
Write an algorithm for the following 1. Write an algorithm to calculate the simple interest using the
formula. Simple interest = (P*N* R)/100. Where P is principle Amount, N is the number of years and R is
the rate of interest.
Step 4: Stop.
Arithmetic Operators
Computer programs are widely used for mathematical calculations. We can write a computer program
which can do simple calculation like adding two numbers (2 +3) and we can also write a program, which
can solve a complex equation like P(x)= x4 + 7x3 - 5x + 9. If you have been even a poor student, you must
be aware that in the first expression 2 and 3 are operands and + is an operator. Similar concepts exist in
Computer Programming. Take a look at the following two
Examples:
2 + 3 P(x) = x4
7x3 - 5x + 9
These two statements are called arithmetic expressions in a programming language and plus, minus
symbols used in these expressions are called arithmetic operators and the values used in these
expressions e.g 2, 3 and x, etc., are called operands. Intheir simplest form, such expressions produce
numerical results. Similarly, a programming language provides various arithmetic operators. The
following table lists down a few of the important arithmetic operators available in C programming
language. Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example
Relational Operators
Consider a situation where we create two variables and assign them some values as follows:
A = 20
B = 10
Here, it is obvious that variable A is greater than B in values. So, we need the help of some symbols to
write such expressions which are called relational expressions.
If we use C programming language, then it will be written as follows:
(A > B) Here, we used a symbol > and it is called a relational operator and in their simplest form, they
produce Boolean results which means the result will be either true or false. Similarly, a programming
language provides various relational operators. The following table lists down a few of the important
relational operators available in C programming language. Assume variable A holds 10 and variable B
holds 20, then:
== Checks if the values of two operands are equal (A == B), if yes then condition becomes true.
!= Checks if the values of two operands are equal or (A!= B) is true, if values are not equal then condition
becomes true.
> Checks if the value of left operand is greater than the (A > B) value of right operand, if yes then
condition becomes true.
< Checks if the value of left operand is less than the value (A < B) of right operand, if yes then condition
becomes true.
>= Checks if the value of left operand is greater than or (A >= B) equal to the value of right operand, if
yes then condition becomes true.
<= Checks if the value of left operand is less than or equal (A <= B) is true. to the value of right operand,
if yes then condition becomes true.
Logical Operators
Logical operators are very important in any programming language and they help us take decisions
based on certain conditions. Suppose we want to combine the result of two conditions, then logical AND
and logical OR operators help us in producing the final result. The following table shows all the logical
operators supported by the C language. Assume variable A holds 1 and variable B holds 0, then:
Operator Description
Most programming languages provide the following relational operators (sometimes with slightly
different syntax) to make mathematical comparisonsbetween data within your conditions:
>Greater than
<Less than
>= greater than/equal to
== Equal to
!= not equal to
Also, the following logical operators (or similar) are usually provided: && and || or!
Though the relational operations (>, >=, etc.) are self-explanatory, the AND,OR and NOT operations
deserve a brief explanation. Both AND and OR allow you to create compound conditions. AND implies
that a condition is true only if all of its components are true.OR implies that a condition evaluates to true
if any of its components are true. NOT reverses the truth value of a condition. This will be discussed in
greater depth later. All conditions in computer programming must evaluate to a Yes/No (or True/False)
question.
Variable
Data Type
Data types are used in all programming languages to create a data type in a program it is required to
inform the complier about which data type you wish to use every time you declare a variable.
There are different types of data types. This section will take a look at some of the most used data types.
Bool is one of the simplest data types. It can contain only 2 values - false or true. The bool type is
important to understand when using logical operators like the ifstatement.
Int is short for integer, a data type for storing numbers without decimals. When working with numbers,
int is the most commonly used data type. Integers have several data types within C#, depending on the
size of the number they are supposed to store.
String is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that
strings are never changed after they have been created. When using methods which change a string, the
actual string is not changed - a new string is returned instead.
Float is one of the data types used to store numbers which may or may not contain decimals.
Flow chart
Flowcharts are used to represent algorithm in pictorial form. It represents the steps involved in the
procedure and shows the logical sequence of processing using boxes of different shapes. The instruction
to be executed is mentioned in the boxes.
These boxes are connected together by solid lines with arrows, which indicate the flow of operation. The
first step in the design of a program is the algorithm. The algorithm is then represented in the form of a
flowchart and the flowchart is then expressed in the computer language to actually prepare the
computer program.
Advantages of Flowcharts
1. The flowchart shows the logic of a problem displayed in pictorial fashion which felicitates easier
checking of an algorithm.
2. The Flowchart is good means of communication to other users. It is also a compact means of
recording an algorithm solution to a problem.
3. The flowchart allows the problem solver to break the problem into parts. These parts can be
connected to make master chart.
4. The flowchart is a permanent record of the solution which can be consulted at a later time.
Algorithm Flowchart
1 A method of representing the step-by-step Flowchart is Diagrammatic representation of an
logical procedure for solving a problem algorithm. It is constructed using different types
ofboxes and symbols
2 It contains step-by-step English descriptions, The flowchart employs a series of blocks and
each step representing a particular arrows, each of which represents a particular step
operation leading to solution of problem in an algorithm
3 These are particularly useful for small These are useful for detailed representations of
problems complicated programs
4 For complex programs, algorithms prove to For complex programs, Flowcharts prove to be
be Inadequate Flowchart. Adequate.
a) Terminal (Start/Stop):
This indicates the ending. Beginning and when the process should halt. It is usually the first and last
symbols in the flow chart diagram.
b) Input/output:
The input/output symbols are used to indicate when an input or output operation is performed in the
program process.
c) Processing:
Processing indicates arithmetic computations like multiplication, division etc. logical operations are
represented using the decision box. Processing also represent the movement of data in the process for
example, store an integer value in a variable.
d) Decision:
This is used to represent a decision point i.e. where there are alternatives to choose from. The condition
for making a decision is written in the decision box diagram.
When the computer is executing a program, the appropriate path that satisfied the condition in the
decision box is followed.
e) Flow lines:
The flow lines are used to show the flow of operation i.e. the order or sequence of executing
instructions. Flow lines are normally represented as an arrowhead which indicates the flow of the
program.
Pseudocode
Pseudocodes are artificial and informal i.e. they have no specific standard. Pseudocodes are not
necessary used to represent algorithm, rather they are used to develop algorithm without the need to
understand the detail of a programming language and they are also known as fake code. Pseudocodes
do not execute on the computer. It helps you understands a problem before attempting to write it in a
programming language, such as Java.
Pseudocodes normally describes only executable statements, which cause specific action to occur it
does not include declaration (creation of a variable).A carefully prepared pseudocodes program can be
easily converted to a corresponding Java, C, C++ e.t.c. program. This simply requires replacing
Pseudocodes statement with the programming language statement.
Example
6 Display result
Consider examples of a Pseudocode that can be written to help create a program that will request two
inputs from the user (the year of birth and the current year), subtracts these integers and displays the
user’s age.
Example
Read n1, n2
Sum = n1 + n2
Diff = n1 – n2
Mult = n1 * n2
End.
Example
Write a Pseudo code for finding the greatest of 3 numbers represented as A, B, and C.
Solution
Begin process
Input A,B,C
Else big = B
Else bigst = C
Example
Write an Algorithm to determine a student’s final grade and indicate whether it is passing or failing. The
final grade is calculated as the average of four marks.
Solution
Data types
As its name indicates, a data type represents a type of the data which you can process using your
computer program. It can be numeric, alphanumeric, decimal, etc. for example, suppose we want to add
two whole numbers 40 and 55, which can be simply one as follows;
40 + 55
Let’s take another problem where we want to add two decimal numbers 10.50 and 50.20, it will be
written as follows:
10.50 + 50.50
The two examples are straightforward. Now let's take another example where we want to record
student information in a notebook. Here we would like to record the following information:
Name:
Class:
Section:
Age:
Sex:
Now, let's put one student record as per the given requirement:
Class: 6th
Section: J
Age: 13
Sex: F
The first example dealt with whole numbers, the second example added two decimal numbers, here as
the third example is dealing with a mix of different data.
Student name "Zara Ali" is a sequence of characters which is also called astring.
Student class "6th" has been represented by a mix of whole number and astring of two
characters. Such a mix is called alphanumeric.
Student section has been represented by a single character which is 'J'.
Student age has been represented by a whole number which is 13.
Student sex has been represented by a single character which is 'F'.
This way, we realized that in our day-to-day life, we deal with different types of data such as strings,
characters, whole numbers (integers), and decimal numbers (floating point numbers). Similarly, when
we write a computer program to process different types of data, we need to specify its type clearly;
otherwise the computer does not understand how different operations can be performed on that given
data.
Variables
When writing a program it is important to keep track of pieces of information. A computer game might
need to know the current score, the player’s position on the screen. A loan calculator program has to
keep track of the loan, the number of months, and the interest rate. Also for our make-hot-a-tea
program we need to know the current temperature of the water in the kettle these are all data and
variable are like storage room that holds that data.
Variables are simply containers. Variable are locations in the computer memory the name assigned to
that memory location (Variable) is known as the identifier.
Variable changes, hence the name, as there are various kinds of containers for specifically particular
kinds of food item, variable can be created to contain a particular type of data this is known as data
types. In Java, a variable is declared like this
<Data type><identifier>;
Example
Intyear_of_birth; identifier are the names you give to computer memory locations (variables) which are
used to store values in a computer program. For example, assume you want to store two values 10 and
20 in your program and at a later stage, you want to use these two values. Let's see how you will do it.
Here are the following three simple steps:
Creating variables
Creating variables is also called declaring variables in C programming. Different programming languages
have different ways of creating variables inside a program. For example, C programming has the
following simple way of creating variables:
main()
int a;
int b;
The above program creates two variables to reserve two memory locations with names a and b. We
created these variables using int keyword to specify variable data type which means we want to store
integer values in these two variables. Similarly, you can create variables to store long, float, char, or any
other data type. For example:
long a;
float b;
You can create variables of similar type by putting them in a single line but separated by comma as
follows:
main()
int a, b;
Listed below are the key points about variables that you need to keep in mind:
A variable name can hold a single type of value. For example, if variable a has been defined int
type, then it can store only integer. C programming language requires a variable creation, i.e.,
declaration before its usage in your program. You cannot use a variable name in your program
without creating it, though programming language like Python allows you to use a variable name
without creating it.
You can use a variable name only once inside your program. For example,if a variable a has been
defined to store an integer value, then you cannotdefine a again to store any other type of
value.
There are programming languages like Python, PHP, Perl, etc., which donot want you to specify
data type at the time of creating variables. So youcan store integer, float, or long without
specifying their data type.
You can give any name to a variable like age, sex, salary, year1990 or anything else you like to
give, but most of the programming languagesallow using only limited characters in their
variables names. For now, wewill suggest to use only a....z, A....Z, 0....9 in your variable names
andstart their names using alphabets only instead of digits.
Almost none of the programming languages allow to start their variable names with a digit, so
1990year will not be a valid variable name whereas year1990 or ye1990ar are valid variable
names.
Every programming language provides more rules related to variables and you will learn them when you
will go in further detail of that programming language.
You have seen how we created variables in the previous section. Now, let's store some values in those
variables:
int a;
int b;
a = 10;
b = 20;
The above program has two additional statements where we are storing 10 in variable a and 20 is being
stored in variable b. Almost all the programming languages have similar way of storing values in variable
where we keep variable name in the left hand side of an equal sign = and whatever value we want to
store in the variable, we keep that value in the right hand side.
Now, we have completed two steps, first we created two variables and then we stored required values
in those variables. Now variable a hasvalue 10 and variable b has value 20. In other words we can say,
when above program is executed, the memory location named a will hold 10 and memory location b will
hold 20.
Control structures
All computer programs can be presented using only three logic structures (or programs) or combinations
of these structures:
1. Simple sequence
2. Selection
3. Repetition
1. The program is simplified. Only the three building blocks are used, and there is a single point of entry
into the structure and a single point of exit.
2. The three coding structures allow a program to be read from top to bottom making the logic of the
program more visible for checking and for maintenance.
Simple Sequence
The simple-Sequence structure consists of one action followed by another. In other words, the flow of
control is first to perform operation A and then to perform operation B. A simple sequence is
flowcharted as two process symbols connected by a flowline.
Selection
The selection structure consists of a test for condition followed by two alternative paths for the program
to follow. The program selects one of the program control paths depending on the test of the condition.
After performing one of two paths, the program control returns to a single point. This pattern can be
termed IF .. ELSE because the logic can be stated (for condition P and operations C and D):
IF P (is true),
perform C;
ELSE
perform D .
Let’s look at the following problem: We need to obtain the temperature expressed in Fahrenheit
degrees and convert it to degrees Celsius. An algorithm to solve this problem would be:
3. Display result in degrees Celcius In this example, the algorithm consists of three steps. Also, note that
the description of the algorithm is done using a so-called pseudocode. A pseudocode is a mixture of
English (or any other human language), symbols, and selected features commonly used in programming
languages. Here is the above algorithm written in pseudocode:
READ degrees_Farenheit
A flowchart for the selection structure consists of a decision symbol followed by two paths, each with a
process symbol, coming together following the operation symbols.
IF structure]
(b) Double alternative: This structure has the form ie IF condition holds, then module A executed;
otherwise module B is executed.
Condition, then: [Module A] Else [Module A] [End of IF structure : Else if condition (m), then; [module
Am] Else [module B]
(END OF IF structure)
Example 1. This is an algorithm that decides whether a student should receive a distinction. READ grade
IF (grade >= 95) THEN student receives a distinction. This is the simplest case of selection control. In fact,
in essence it is a sequential form but the second step is only taken when the condition contained in the
brackets is true, that is the last step (starting at THEN) is selected for execution only when the condition
is satisfied.
Example 2.
Control access to a computer depending on whether the user has supplied a username and password
that are contained in the system database. IF (username in database AND password corresponds to
user) THEN Accept user ELSE Deny user
In this case, there is also a decision to be made but a more complex one. In the condition is true only the
Accept user step is executed and if the condition is false only the Deny user step is executed. That is, the
condition helps us make a selection which controls which of the two steps will be executed.
The repetition structure can also be called a loop. In a loop, an operation or a set of operation is
repeated until some condition is satisfied. The basic form of repetition is termed DO WHILE in the
literature of structured programming. In some languages, the repetition structure might be termed
PERFORM UNTIL. In the perform until pattern, the program logic tests a condition; if it is true, the
program executes the operation and loops back for another test. If the condition is true, the repetition
ceases. This structure has the form: Repeat for K = R to S by T:
[Module] [End of loop] Algorithm example for iteration or repetition For example let us take 10 sets of
numbers each set containing three. The problem is to get the biggest number in each set and print it.
Algorithm Step 1: Read the total number of sets Step 2: Initialize the number of the set as N=1 Step 3:
Read three numbers of a set say A, B, C. Step 4: Compare A with 3 and choose the bigger. Step 5:
Compare the bigger number with C and Choose the biggest Step 6: Print the biggest number,
Step 7: Increment the number of the set by 1 (N=N+1) Step 8: Check whether we have exceeded 10. If
not Go – To step 3 otherwise. Step 9: STOP The flow chart for the same in given below
Example 1.
Total = 0
Average = 0
ENDFOR
Average = total / 10
Example 2.
Read numbers and add them up until their total value reaches (or exceeds) a set value represented by S.
WHILE (total < S) DO Read number total = total + number
ENDDO
Data Structure
It is important to understand the concept of Information and how it is organized or how it can be
utilized. If we arrange some data in an appropriate sequence, then it forms a Structure and gives us a
meaning. This meaning is called Information. The basic unit of Information in Computer Science is a bit,
Binary Digit. So we found two things in Information: One is Data and the other is Structure
Data Structure is a way of collecting and organizing data in such a way that we can perform operations
on these data in an effective way. Data Structures is about rendering data elements in terms of some
relationship, for better organization and storage. For example, we have data player's name "Virat" and
age 26. Here "Virat" is of String data type and 26 is of integer data type.
We can organize this data as a record like Player record. Now we can collect and store player's records in
a file or database as a data structure. For example: "Dhoni"
In simple language, Data Structures are structures programmed to store ordered data, so that various
operations can be performed on it easily. It represents the knowledge of data to be organized in
memory. It should be designed and implemented in such a way that it reduces the complexity and
increases the efficiency.
As we have discussed above, anything that can store data can be called as a data structure, hence
Integer, Float, Boolean, Char,etc, all are data structures. They are known as Primitive Data Structures.
Then we also have some complex Data Structures, which are used to store large and connected data.
Some examples of Abstract Data Structure are:
Linked List
Tree
Graph
Stack, Queue etc.
All these data structures allow us to perform different operations on data. We select these data
structures based on which type of operation is required.