Lecture-1-Computing Systemsengg Prob Methodologyintro To C 11783 CS26 27-02-2019 PDF

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

Lecture-1 Overview and Problem Solving with C Unit-1

This Lecture covers


 Computing Systems
 Hardware and Software
 An Engineering Problem-Solving Methodology
 Introduction to C

 Computing Systems:
A computer is a programmable machine. It allows the user to store all sorts of
information and then „process‟ that information, or data, or carry out actions with the
information, such as calculating numbers or organizing words.
Computer Hardware: Computer hardware is the physical parts or components of a computer,
such as the monitor, keyboard, computer data storage, graphic card, sound card and motherboard.
Internal organization of a computer:

CPU - central processing unit: Where decisions are made, computations are performed,
and input/output requests are delegated
Memory: Stores information being processed by the CPU
Input devices: Allows people to supply information to computers
Output devices: Allows people to receive information from computers

Computer Software: Computer software, or simply software, is a part of a computer system that
consists of data or computer instructions, in contrast to the physical hardware from which the
system is built. In computer science and software engineering, computer software is all
information processed by computer systems, programs and data.

1
Lecture-1 Overview and Problem Solving with C Unit-1

Operating Systems: An operating system (OS) is system software that manages computer
hardware and software resources and provides common services for computer programs. All
computer programs, excluding firmware, require an operating system to function.
System Software: System software is a type of computer program that is designed to run a
computer's hardware and application programs. If we think of the computer system as a layered
model, the system software is the interface between the hardware and user applications. ... The
OS manages all the other programs in a computer.
Application Software: Application software can be divided into two general classes: systems
software and applications software. Applications software (also called end-user programs)
include such things as database programs, word processors, Web browsers and spreadsheets.

“What are the different types of languages?”


 Machine level language: Machine level language is nothing but a set of instructions given to
the computer in the form of binary values (0‟s and 1‟s).
Example: To perform sum of two numbers, the machine instruction may be 11001111. It is
difficult to understand by the user. But it is easy for computers to understand machine
language. To overcome this difficulty of machine language, assembly language is
introduced.
 Assembly level language: Assembly level language is nothing but a set of instructions given
to the computer in the form of symbolic (mnemonics) names.
Example: ADD R1, R2 // Adds the value of R1 and R2
SUB R1, R2 // Subtracts the value of R1 and R2
MUL R1, R2 // Multiplies the value of R1 and R2
DIV R1, R2 // Divides the value of R1 and R2
Here the ADD instruction tells that the values of R1 and R2 are to be added. Like that the
other operations can be performed. We can write the program using symbolic names as
ADD, SUB, MUL and DIV. hence, it is called symbolic language; these symbolic names are
called mnemonics. Instead of using 0‟s and 1‟s to represent an instruction, we can use
symbolic names in assembly language; it is easy to remember the symbols than machine
language. But computer cannot understand assembly language except machine language. So

2
Lecture-1 Overview and Problem Solving with C Unit-1

we need translator to convert from assembly level language to machine level language is
called assembler.
 High level language: A high level language is written using symbols and words like English
language. HLL enables the programmer to write machine independent code. The programs
written in HLL‟s are portable and it is easy to read and understand by the user.
Example: void main( )
{
int a=5, b=10, sum=0; // Initializing values of a=5 and b=10
sum=a+b; // Adding value of a and b and store in sum
printf(“Sum of two number=%d”, sum); // Display the value of sum
}
We can easily read and understand this program since all the instructions are similar to
English like language. But computer understand only machine instructions, so we need
translator to convert high level language to machine level language called compiler.

 Executing a C program:
The various steps involved in creating and running in a program are:
Step 1: Writing and editing programs: The software that we use to type a program is called
editor. Using vi editor we can type the program, change the program and store the program. Save
the program by giving the name of the file as filemane.c. Now the program is saved in the hard
disk. This file which is input to the compiler is called “source file”.
Step 2: Compiling a C program: After finish typing the program, we have to compile it by
command cc filename.c, it generates the object file which is a executable file a.out.
Compiler: a program that converts instructions into a machine-code or lower-level form so that
they can be read and executed by a computer.
Bugs: A software bug is an error, flaw, failure or fault in a computer program or system that
causes it to produce an incorrect or unexpected result, or to behave in unintended ways.
Compile time errors: A compile time error is an error that is detected by the compiler. Common
causes for compile time errors include: Syntax errors such as missing semi-colon or use of a
reserved keyword (such as 'class'). When you try and access a variable that is not in scope.
Debugging: identify and remove errors from (computer hardware or software).

3
Lecture-1 Overview and Problem Solving with C Unit-1

Step 3: Linking a Program: After compilation we have to run the program by ./a.out.
Linker: In computing, a linker or link editor is a computer program that takes one or more object
files generated by a compiler and combines them into a single executable file, library file, or
another 'object' file.
Step 4: Program execution: now the program is executed and we get the result on the output
screen.

 An Engineering Problem Solving Methodology:


The problem Solving Methodology presented here works for many engineering problems that
can be tailored to solve problems in other areas also.
The process is defined in five steps:
1. State the problem clearly
2. Describe the input and output information
3. Work the problem by hand (or with calculator) for a simple set of data
4. Develop a solution and convert it to computer program.
5. Test the solution with variety of data.

4
Lecture-1 Overview and Problem Solving with C Unit-1

Example: To compute Straight line distance between two points


1. State the problem clearly: Compute the straight line distance between two points in a
plane.
2. Describe the input and output information
point1
Distance
point2
3. Work the problem by hand (or with calculator) for a simple set of data
dist((x, y), (a, b)) = √(x - a)² + (y - b)²
dist((2, -1), (-2, 2)) = √(2 - (-2))² + ((-1) - 2)²
= √(2 + 2)² + (-1 - 2)²
= √(4)² + (-3)²
= √16 + 9
= √25
=5

4. Develop a solution and convert it to C


program
4.
/*----------------------------------------------------------------------------*/
/* Program Chapter 2 Modify */
/* */
/* This program calculates the */
/* distance between two points */

#include <stdio.h>
#include <math.h>

int main(void)
{
/* Declaration and initialization of variables.*/
double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance;

/* Compute the sides of a right triangle. */


side_1 = x2 - x1;
side_2 = y2 - y1;
distance = sqrt(side_1*side_1 + side_2*side_2);

/* Print results. */
printf("The distance between the two points is ""%5.2f \n", distance);

/* Exit program. */
return 0;

5
Lecture-1 Overview and Problem Solving with C Unit-1

}
/*----------------------------------------------------------------------------*/
5. Test the solution with variety of data.
The output of a program should match with result obtained which was done in the
work by hand step.

 Introduction to C:

C is a general-purpose high level language that was originally developed for the Unix
operating system. It was first implemented on the Digital Equipment Corporation PDP-11
computer in 1972. C has emerged as the most widely used programming language for software
development. C language supports the powerful low level features like pointer, memory
allocation, bit manipulations etc. The features of C language make it possible to see the language
for system programming like the development of compiler, interpreter, operating system, system
utilities etc.

Why to use C?
C was initially used for system development work, in particular the programs that make-
up the operating system. C was adopted as a system development language because it produces
code that runs nearly as fast as code written in assembly language. Some examples of the use of
C might be:

 Operating Systems
 Language Compilers
 Assemblers
 Text Editors
 Print Spoolers
 Network Drivers
 Modern Programs
 Data Bases
 Language Interpreters
 Utilities

6
Lecture-1 Overview and Problem Solving with C Unit-1

History of C:
C language was developed by Dennis Ritchie & Ken Thompson at Bell Laboratories
(now part of AT & T) at USA. In 1968, Bell and MIT were doing a joint project on MULTICS
(multiplexing information and computing service). Operating system for multi-user-time sharing-
system. They are BCPL (Basic combined programming language) developed by Martin Richards
at Cambridge University. Around the same time the language called „B‟ was written by Ken
Thompson at Bell Laboratories. Denis Ritchie inherited the features of „B‟ and BCPL, added
some features of his own and developed „c‟.

Advantages/Features of C
C language has become the language of choice of two decades among system
programmers and application programmers. Reason for its popularity can be summarized as
follows:
1. Powerful and flexibility: The power and popular UNIX as was written in C. The
complier and interpreter for FORTAN, PASCAL, LISP, and BASIC are written in C.
2. Portability: C program written in one system can be run on other system with little
modifications.
3. Modularity: C program can be modularized for step wise refinement. The complex
program can be modularized into simple programs.
4. Efficiency: The program written in C language is highly efficient like assembly
language in speed and memory management.
5. Programmer oriented: It has the flexible control structure and gives access to hardware
and enables to
manipulate individual bits of memory.

7
Lecture-1 Overview and Problem Solving with C Unit-1

Exercise:
1. What is computer?
2. Explain the architecture of Computer in detail?
3. What are storage devices? What are their types?
4. What are the input and output devices?
5. What is machine level language?
6. What is assembly level language?
7. What is high level language?
8. What are the differences between machine level and high level language?
9. What are the differences between machine level and assembly level language?
10. What are the differences between assembly level and high level language? Explain with
example.
11. What are the different types of programming languages?
12. What is compiler?
13. What is assembler?
14. What are the various steps to create and run the C program?

Self Test Questions:


1. A computer converts data into this -------------- (JUNE/JULY 2014)
a) Information b) charts c) software d) input or output
2. A nibble is ----------------------- (JUNE/JULY 2014)
a) 4 bits b) 8 bits c) 16 bits d) 32 bits

You might also like