Chapter 01 - Problem Solving Through Computers & Introduction To C
Chapter 01 - Problem Solving Through Computers & Introduction To C
◼ Algorithms
◼ Programming
◼ Structured Programming
◼ Introduction to C
◼ Structure of a C Program
◼ Assignments
3. Programming / coding
➢ Algorithms are for humans (written in human-readable languages) not
for computers.
➢ If we want to solve a problem through computers, an algorithm must be
translated to a program (a language that the computers understand).
➢ Different programming languages can be used for this.
We discuss programming in detail later in this chapter.
➢ The errors found in the testing phases are debugged or rectified and the
program is tested again. This process continues until all the errors are
removed from the program.
FindAverage (x, y, z)
/* This algorithm takes as input three numbers, computes their average and displays it. */
1. Set Sum = x+y+z /* ‘Sum’ is a real number that represents the sum of x, y, z */
2. Set Avg = Sum/3 /* ‘Avg’ is a real number that represents the average of x, y, z */
3. Write: Avg
FindLargest(x, y, z)
/* This algorithm takes as input 3 integers x, y and z. It finds the largest among them and
displays the result. */
1. Set largest = x /* ‘largest’ represents the largest number */
2. If y > largest, then:
Set largest = y
3. If z > largest, then:
Set largest = z
4. Write: largest
FindAverage (A, n)
/* This algorithm takes as input one array ‘A’ with ‘n’ elements, indexed from 0. It
computes the average of the ‘n’ numbers in the array and displays it*/
1. Set Sum = 0 /* ‘Sum’ is an integer. It represents the sum of all ‘n’ numbers*/
2. Set i = 0 /* ‘i’ is an integer. It is used as a counter variable */
3. While i < n, repeat steps 4 to 5
4. Set Sum = Sum + A[i]
5. Set i = i+1
6. Set Avg = Sum / n
7. Write: Avg
Symbols Use
Start / Stop
Input / Output
Processing
Condition
Data flow
Connector
Start
Input x, y, z
Largest = x
Yes
y > largest ? Largest = y
No
Yes
z > largest ? Largest = z
No
Display largest
Stop
Start
sum = 0, i = 0
No
i<n?
Yes
Read number
average = sum / n
Display average
Stop
◼ The control flow depicts the order in which the instructions in an algorithm are
executed.
◼ The control flow can be a
➢ Sequence: All the instructions are executed one after another, starting
from the 1st instruction to the last, e.g., ALG1_1.
➢ Selection / Decision / Branch: One set of instructions is executed if a
particular condition becomes true and another set of instruction is
executed if the condition becomes false, e.g., ALG1_2.
▪ Generally specified through an ‘if’ clause.
➢ Loop / Repetition / Iteration: A set of instructions is executed repeatedly
until a particular condition becomes false, e.g., ALG1_3.
▪ Generally specified through an ‘while’ clause.
• Programming with procedure-oriented languages is OK, when the lines of codes are limited to few
thousand. However, once the lines of codes exceed this limit a procedure-oriented program also
become complex. Object-oriented programming is designed to overcome this problem.
• Examples: Smalltalk, C++, Java, C#
[NOTE]: Some old compilers produces assembly-level language as output, which in turn
should be passed through an assembler to produce the desired machine-level codes (Most of
the modern compilers - all C compilers - produce machine-level language as output.)
Though, both compiler and interpreter does the same thing, there are some
differences between these two (given in next slide).
◼ The single-entry and single-exit property helps in reducing the number of paths
of flow of control, which makes the program simple to design and understand.
➢ If there are arbitrary paths for flow of control, the program will be
difficult to write, understand, debug, and maintain.
◼ B:
➢ Created by Ken Thompson in 1970.
➢ Used the features of BCPL.
➢ Used to write the early version of UNIX in the Bell laboratories.
◼ C:
➢ Created by Denis Ritchie at Bell laboratories in 1972.
➢ He used the features of ALGOL, BCPL, and B and added the concept of
data types and some more powerful features to formulate this language.
◼ ANSI C:
➢ The rapid growth of C led to the development of different versions of C,
that were similar but often became incompatible. This proposed a
serious problem for system development.
➢ To assure that the C language remains a standard, in 1983, ANSI
(American National Standards Institute) appointed a technical committee to
define a standard for C.
➢ This committee approved a version of C in December 1989, which is
known as ANSI C.
◼ C99:
➢ During 1990’s, C++, a language based upon C, underwent a number of
changes.
➢ During the same period, Sun Microsystems of USA created a new
language JAVA modeled on C++.
➢ In 1999, the standardization committee of C felt that some new features
of C++/JAVA, if added to C, would enhance the usefulness of the
language.
➢ The result was C99.
6. C has the ability to extend itself: The user can write and add his own
functions to the C library or even can create his own library.
Output
Welcome to Sea
main()
int main() All are same (Takes no arguments, returns an integer value)
main(void)
int main(void)
void main()
Both are same (Takes no arguments, returns nothing)
void main(void)
Used for accepting command line
int main(int argc, char* argr[])
arguments (discussed in Chapter “Files
void main(int argc, char* argr[]) and Command Line Arguments.)
◼ Line No. 7: This is the 2nd executable statement in our program. getch() is also
a pre-defined function. The details about this are discussed in Chapter 4..
Many steps are involved between writing a C program and it gets executed. The
figure in the next slide shows these steps.
[NOTE]: Most of the IDEs (Integrated Development Environments) like TC++, VC++,
CodeBlocks etc. hide (does automatically) many of the steps for us. We will study how to perform
these steps in different environments (OS) later, but before this let’s 1 st understand what is done at
each step.
[NOTE]: Here the word “relocatable” means the program is ready to execute
except for one thing - no specific memory address have yet been assigned to
the code. All the addresses are relative offsets.
➢ All most all C programs use a set of pre-defined functions like printf, scanf etc.,
called the library functions. To increase the efficiency, these set of library functions
are pre-complied and stored as object files (binary format), called the library files.
➢ The information about these library files is contained in various header files. For
example, the header file “stdio.h” actually contain the information about all the
I/O library functions like printf, scanf etc.(their prototypes, and in which library
file they are actually located).
➢ We include these header files in our source code (PR1_1.c) through the pre-processor
directive “#include” (e.g., #include <stdio.h>).
➢ The linker on getting this information, searches the corresponding library functions
(e.g., printf) in the mentioned library file. If the library function is found, it links /
joins that library file with the object file (PR1_1.obj) to produce the desired executable
file. On the other hand, if the library functions is not found in that library file or the
library file itself is missing a link error is displayed.
◼ STEP 1: Open a text editor (e.g., vi, emacs etc.) and create a new file PR1_1.c.
This is done by writing the following command in the command prompt:
vi PR1_1.c
This is the command for calling the vi editor and creating the file PR1_1.c. If
the file existed before, it will be loaded, otherwise a new file with this name will
be created. Once the editing is done, save the file.
◼ STEP 2: Compile the file (In UNIX pre-processing and linking is automatically done
during compilation). This is done by writing the following command in the
command prompt:
cc PR1_1.c
Every UNIX system has an inbuilt C compiler, called cc (c compiler). Once this
step is successful, the linker will create the default executable file a.out.
a.out
#include <stdio.h>
void main()
{
int num1, num2, num3; /* variable declaration */
float avg; /* variable declaration */
avg = (num1+num2+num3)/3;
/* Program Ends */
/* PR1_3.c: Program that computes and displays the average of 3 numbers by using
user-defined function*/
#include <stdio.h>
void main()
{
int num1, num2, num3;
float avg;
/* Program Ends */
Output
Documentation section