29-05-2013, 03:19 PM
Programming and Data Structures
Programming and Data.pdf (Size: 2.26 MB / Downloads: 464)
Problem solving
Harder part of the course
Requires creative thinking
One writes a program to make the computer carry out the steps
identified to solve a problem
The solution consists of a set of steps which must be carried out in
the correct sequence – identified manually (by you)
This is a “programme” for solving the problem
Codification of this “programme” in a suitable computer language,
such as ‘C’ is computer programming
Solution to the problem must precede writing of the program
A few shell commands
When a new terminal window is opened, a command shell is run
inside it
This command shell usuall provides a (shell) prompt which is
often a short string ending with ‘$’ or ‘>’
The command shell can run shell commands, such as “ls”, “mkdir
dirName”, “cd targetDir”, “cd ..”, “rm fileName”
It can also run other programs, such “gvim fileName.c &”, “gcc
fileName.c -o fileName”
The ‘&’ at the end of the command causes the command to run in
the background and the shell prompt re-appears so that a new
command can be executed
An analogy to scanf
Suppose that you wish to place an order to purchase a sack of
rice from a shop
You supply the shop keeper the address of your house for
delivering (or putting) the product there
How about supplying scanf the address of a so that it can put an
integer there
&a is simply the address of the variable a, which is supplied to
scanf for reading in an integer into a
More on scanf
The format string consists of a sequence of directives which
describe how to handle the sequence of input characters
If processing of a directive fails, no more input is read, and scanf
returns
A directive can be:
WS space, tab, etc.; results in skipping any amount (0 or
more) of white space (used to skip white space)
ordinary (not WS or %); which should be matched exactly (not
commonly used)
conversion heavily used
man 3 scanf for more details
options are rich to enable reading of data from formatted outputs
few of those options to be visited later
Routines
An important concept – a sequence of steps to perform a specific
task
Usually part of a bigger program
While programs are run, routines are invoked – from within the
program or from other routines
Routines are a often invoked with parameters
Recursive routines may even invoke themselves, either directly or
via other routines
Routines often return a value after performing their task
Routines accepting parameters and returning values are called
functions in ‘C’
In ‘C’ routines are also recursively callablrecursively callableitem
In ‘C’, the program is treated as the “main” routine or function
Formal and actual parameters
At the time of developing a routine, the actual values to be worked
upon are not known
Routine must be developed with placeholders for the actual values
Such placeholders are called formal parameters
When the routine is invoked with placeholders for values to be
added, say as sum (4, 5+3) or sum (a, b), where a and b
are variables used in the routine from where sum() is called, e.g.
main()
Parameters actually passed to the function at the time of
invocation are called actual parameters
For ‘C’ programs, values resulting from evaluation of the actual
parameters (which could be expressions) are copied to the formal
parameters
This method of parameter passing is referred to as call by value
Passing 1D Arrays to functions
1D arrays are passed to functions with or without their
dimensions, as int A[10] or int A[]
Only the address of the array, as available in the calling function
(caller) is passed
There is no new allocation of memory to store arrays passed as
formal parameters
A[i] is obtained as *(A+1), where the dimension does not play
any role
Formal parameters of functions declared as arrays are always
arrays declarations