Uploaded by mphulla twambilire

Introduction to Programming-C

advertisement
Introduction to Programming
C
PART A
– Algorithms , continued
Discussion content
• Review of Algorithms :
• Flowchart and Pseudocode
• Approaches to algorithms design
• Randomized Algorithms
• Divide-and-conquer Algorithms
• Greedy Algorithms, Approximation Algorithms:
• Why algorithms in programming
• Advantages of algorithms
• Determining the best algorithm for a task
• Basic concepts in C programming
Flowchart and Pseudocode
• Flowchart :
• A diagrammatic representation that illustrates a solution model to a given
problem.
• Pseudocode:
• An informal high-level description of the operating principle of an algorithm
• Underlining concepts in algorithm development
• Procedure: A set of well defined sequential instructions which is followed to
complete a tack
• Algorithm: A the step by step procedure to solve a problem
Examples
Pseudocode
Flowchart
Approaches to algorithms design
• Randomized Algorithms:
• Rely on the statistical properties of random numbers.
• For example old, even or greater or less than or average (the median)
• So numbers divided into 2 based on the randomly selected number / the
median
• Repeat the process till the list is sorted.
Divide-and-conquer Algorithms
• Revolve around 3 steps:
• Divide : Breaks the problem into 2
(say manageable parts)
• Conquer : Solve each problem
separately
• Combine: Combine the solution
Why algorithms in programming
• Helps in the identification of the processes, major decision points,
and variables necessary to solve the problem
• Enhances a rational process which makes the process more efficient
and more consistent.
• Efficient: Ability to properly analyse a problem with much ease
• Consistent: Use of the same specified process and increased skill in applying
the process.
• Prevents / minimises inefficient algorithm which can significantly
impact system performance. i.e.
• Gives ability to predict the performance of different algorithms in order to
guide program design decisions.
Determining the best algorithm for a task
• Speed of completing a task {time}
• Resource demand {cost}
• RAM
• Processor
• Also its ability to address the problem at hand
• Abstraction: Hide the complexity behind the problem {simplifies}
• Reusability: Algorithms are often reusable in many different
situations.
PART B
– Basic concepts in C programming
Computer Programme
• Source Code:
• applications written in a humanreadable programming language.
• Compiling:
• The modified source code is compiled
into binary object code.
• Linking:
• Combining the object code with
required supporting code to make an
executable program. Links object code
to existing libraries
Introduction to C Programming: Building bloc
• C programs consist of pieces called functions.
• You can program all the functions you need to form a C program, but
most C programmers take advantage of the rich collection of existing
functions called the C Standard Library.
• Using existing pieces of code / functions—this is called software reuse.
• When programming in C you’ll typically use the following building
blocks:
• C Standard Library functions
• Functions you create yourself
• Functions other people (whom you trust) have created and made available to
you
Building bloc-continued
• C systems generally consist of several parts:
• a program development environment; The editor where the
programmer write the program: e.g. Visual studio, Notepad ++ Net
beans, Eclipse etc.
• the language ; the syntax and semantics {the rules and the
meanings}
• the C Standard Library: The exiting reusable functions {e.g. the
standard input and output functions}
• C program file names should end with the .c extension.
Why reuse?
• The advantage of creating your own functions is that
you’ll know exactly how they work.
• You’ll be able to examine the C code.
• The disadvantage is the time-consuming:
• Requires a lot of effort into designing, developing and
debugging new functions.
Phases that a program goes
• These are: edit, preprocess, compile, link, load and execute.
Standard Input, Standard Output and Standard
Error Streams
• Most C programs input and/or output data.
• Certain C functions take their input from stdin (the standard input
stream), which is normally the keyboard, but stdin can be
connected to another stream.
• Data is often output to stdout (the standard output stream), which
is normally the computer screen, but stdout can be connected to
another stream.
• When we say that a program prints a result, we normally mean that
the result is displayed on a screen.
Example of c program
• // Fig. 2.1: fig02_01.c
// A first program in C
• begin with //, indicating that these two lines are comments.
• document programs and improve program readability.
• do not cause the computer to perform any action when the
program is run.
• are ignored by the C compiler and do not cause any machinelanguage object code to be generated.
• help other people read and understand your program.
#include Preprocessor Directive
• #include <stdio.h>
• is a directive to the C preprocessor.
• Lines beginning with # are processed by the preprocessor
before compilation
• Line 3::: #include <stdio.h>
• tells the preprocessor to include the contents of the standard
input/output header (<stdio.h>) in the program.
• This header contains information used by the compiler when
compiling calls to standard input/output library functions such as
printf().
The main Function
• int main( void )
• is a part of every C program.
• The parentheses after main indicate that main is a program
building block called a function.
• Every program in C begins executing at the function main.
• The keyword int to the left of main indicates that main “returns”
an integer (whole number) value.
• The void in parentheses here means that main does not receive
any information.
Data Types in C
• A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
• By variable we mean a storage location
Types
Data Types
asic Data
Type
int, char, float, double
Derived Data array, pointer, structure, union
Type
Enumeration
Data Type
enum
Void Data
Type
void
The function block
• A left brace, {, begins the body of every function
• A corresponding right brace ends each function
• This pair of braces and the portion of the program between
the braces is called a block.
An Output Statement
• printf( "Welcome to C!\n" );
• instructs the computer to perform an action, namely to print on
the screen the string of characters marked by the quotation marks.
• A string is sometimes called a character string, a message or a
literal.
The function block
• The entire line, including the printf function (the “f” stands for
“formatted”)
• its argument within the parentheses and the semicolon (;), is called a statement.
• Every statement must end with a semicolon (also known as the statement
terminator).
• When the preceding printf statement is executed, it prints the message
Welcome to C! on the screen.
• The characters normally print exactly as they appear between the double
quotes in the printf statement.
Escape Sequences
• Notice that the characters \n were not printed on the screen.
• The backslash (\) is called an escape character.
• It indicates that printf is supposed to do something out of the ordinary.
• When encountering a backslash in a string, the compiler looks
ahead at the next character and combines it with the backslash
to form an escape sequence.
• The escape sequence \n means newline.
• When a newline appears in the string output by a printf, the
newline causes the cursor to position to the beginning of the
next line on the screen.
• Some common escape sequences are listed in Fig. 2.2.
Some common escape sequences
The Linker and Executables:
• Standard library functions like printf and scanf are not part of the C
programming language.
• For example, the compiler cannot find a spelling error in printf or scanf.
• When the compiler compiles a printf statement, it merely provides space in
the object program for a “call” to the library function.
• But the compiler does not know where the library functions are—the linker
does.
• When the linker runs, it locates the library functions and inserts the
proper calls to these library functions in the object program.
• Now the object program is complete and ready to be executed.
• For this reason, the linked program is called an executable.
• If the function name is misspelled, it’s the linker that will spot the error,
because it will not be able to match the name in the C program with the
name of any known function in the libraries.
Reference
• Deitel & Deitel
• https://www.javatpoint.com/data-types-in-c
Download