0% found this document useful (0 votes)
4 views

Introduction First Class

The document outlines a course on Programming and Data Structures (PDS), focusing on writing programs using the C programming language. It covers fundamental concepts such as binary representation, variable types, flowcharts, and the structure of C programs, along with practical examples. Key topics include the use of libraries, problem-solving steps, and the importance of variables and data types in programming.

Uploaded by

jeetmukherjee381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Introduction First Class

The document outlines a course on Programming and Data Structures (PDS), focusing on writing programs using the C programming language. It covers fundamental concepts such as binary representation, variable types, flowcharts, and the structure of C programs, along with practical examples. Key topics include the use of libraries, problem-solving steps, and the importance of variables and data types in programming.

Uploaded by

jeetmukherjee381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

• Basically we will learn how to write programs

• Programs? Set of instructions written for a computer. Tell it what to do


What is this course?
PDS: Programming and Data structure
• Basically we will learn how to write programs
• Programs? Set of instructions written for a computer. Tell it what to do
What is this course?
PDS: Programming and Data structure
• Basically we will learn how to write programs
• Programs? Set of instructions written for a computer. Tell it what to do
What is this course?
PDS: Programming and Data structure
• Basically we will learn how to write programs
• Programs? Set of instructions written for a computer. Tell it what to do
Books:
1. Programming with C, Byron Gottfried
2. The C Programming Language, Brian W Kernighan, Dennis M Ritchie
3. Programming in ANSI C, E. Balaguruswamy
4. Data Structures, S. Lipschutz, Schaum’s Outline Series
… and many more options
Super powers of computer

• This machine can do some tasks blazingly fast,


tirelessly
• Thousands of complex mathematical operations
in a second

• But…
• You need to speak its language
• Tell computers exactly what operations to do,
step by step
• Programming…
Three steps in writing programs
Step 1: Write the program in a high-level language (HLL - in your case, C)
Step 2: Compile the program using a C compiler
Step 3: Run / execute the program (ask the computer to execute it)

HLL
program Executable
Compiler code

gcc compiler will be used in the lab classes


But, you can use instructions written by others: Libraries

Libraries: Pre-written instructions for complicated tasks


You can include these library names in your program and use their functionalities
Don’t need to write those instructions yourself
Executable
HLL program
code
with
Compiler Object code Linker
Library name

Library

gcc compiler will be used in the lab classes


This executable code is in binary
Computer understands only numbers, in binary (0 and 1)
Binary Representation

The decimal number system we use is base 10


• 10 digits, from 0 to 9, Positional weights 100, 101, 102,…from right to left for integers
• Example: 723 = 3x100 + 2x101 + 7x102

Numbers are represented inside computers in the base-2 system (Binary Numbers)
• Only two symbols/digits 0 and 1
• Positional weights of digits: 20, 21, 22,…from right to left for integers
• Example: 1101 in binary = (1 x 20) + (0 x 21) + (1 x 22) + (1 x 23) = 13 in decimal
Bits and Bytes
• Bit – a single 1 or 0

• Byte – 8 consecutive bits


• 2 bytes = 16 bits
• 4 bytes = 32 bits

• Maximum integer that can represented


• in 1 byte = 255 (=11111111)
• In 4 bytes = 4294967295 (= 32 1’s)

• Number of integers that can be represented in 1 byte = 256 (the integers 0, 1, 2, 3,….255)
Problem solving: typical flow

Step 1:
• Clearly specify the problem to be solved.
Step 2:
• Draw flowchart or write algorithm (represent the sequence of operations needed).
Step 3:
• Convert flowchart (algorithm) into program code.
Step 4:
• Compile the program to get executable code.
Step 5:
• Execute the program on a computer.
Flowchart: represent the sequence of operations as a diagram

Computation

Input / Output

Decision Box

Start / Stop

Flow of control

Connector
Flowchart: Example 1: adding three numbers

START

READ A, B,
C

S=A+B+C

OUTPUT S

STOP
Flowchart: Example 2: find larger of two numbers

START

READ X, Y

YES IS
NO
X>Y?

OUTPUT X OUTPUT Y

STOP STOP
Flowchart: Example 3: sum first N integers

START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT

COUNT = COUNT + 1

NO IS YES
COUNT > N? OUTPUT SUM

STOP
Flowchart: Example 4: compute factorial of a number

START

READ N

PROD = 1
COUNT = 1

PROD = PROD * COUNT

COUNT = COUNT + 1

NO IS YES
COUNT > N? OUTPUT PROD

STOP
Fundamentals of C
First C program – print on screen

#include <stdio.h>
int main()
Output
{
Hello, World!
printf ("Hello, World! \n") ;
return 0;
}
A Simple C program
#include <stdio.h>
int main()
When you run the program
{
int x, y, sum, max;
scanf(“%d%d”, &x, &y);
sum = x + y;
if (x > y) max = x;
else max = y; Output after you type 15 and 20

printf (“Sum = %d\n”, sum); 15 20


Sum = 35
printf (“Larger = %d\n”, max);
Larger = 20
return 0;
}
Structure of a C program
• A collection of functions (we will see what they are later)

• Exactly one special function named main( ) must be present.


Program always starts from there.

• Each function has different types of statements


• variable declarations, assignment, condition check, looping etc.

• Statements are executed one by one in top-down order (though some special instructions
differ from this top-down order)
Things you will see in a C program
(we will look at all these one by one)

• Variables
• Constants
• Expressions (Arithmetic, Logical, Assignment)
• Statements (Declaration, Assignment, Control (Conditional/Branching, Looping))
• Arrays
• Functions
• Structures
• Pointers
The C Character Set
The C language alphabet
• Uppercase letters ‘A’ to ‘Z’
• Lowercase letters ‘a’ to ‘z’
• Digits ‘0’ to ‘9’
• Certain special characters:

! # % ^ & * ( )
– _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ?
whitespace characters (space, tab, …)

A C program should not contain anything else


Variables
• Very important concept for programming
• Can store any temporary result while executing a program
• Can have only one value assigned to it at any given time
• The value of a variable can be changed during the execution of the program
• Variables are stored in the computer’s memory
• Memory is a list of consecutive storage locations, each
0
having a unique address
1
• Four aspects of a variable:
2
• The value stored in the variable
3
• The type of the variable – decides what type of value
can be stored (integer, real, etc.)
• The variable name is used to refer to the value of the
variable
• A variable is stored at a particular location of the N–2

memory, called its address N–1

MEMORY
Example

#include <stdio.h>
int main( )
{
int x;
int y;
The = sign in a C program does NOT denote equality

x=1; We are actually assigning a value to a variable, i.e.,


y=3; storing a particular value into the variable

printf("x = %d, y= %d\n", x, y);

return 0;
}
Variables in Memory

Instruction sequence Memory


int X
X = 10
(may contain whatever
X ? value at the beginning)
X = 20

X = X +1

X = X*5

A variable can have only one value assigned to it at


any given time during the execution of the program
Variables in Memory

Instruction sequence Memory

X = 10
after executing X = 1
0
X 10
X = 20

X = X +1

X = X*5

A variable can have only one value assigned to it at


any given time during the execution of the program
Variables in Memory

Instruction sequence Memory

X = 10

e c uti ng X = 20 X 20
X = 20 after e x

X = X +1

X = X*5

A variable can have only one value assigned to it at


any given time during the execution of the program
Variables in Memory

Instruction sequence Memory

X = 10

1 X 21
X = 20 = X+
gX
ec utin
te r ex
af
X=X+1

X=X*5

A variable can have only one value assigned to it at


any given time during the execution of the program
Variables in Memory

Instruction sequence Memory

X = 10

X 105
X = 20
* 5
X =X
X=X+1 ti ng
x ecu
fte re
a

X=X*5

A variable can have only one value assigned to it at


any given time during the execution of the program
Variables in Memory

Instruction sequence Memory

X = 20
(may contain whatever
X ? value at the beginning)
Y = 15

(may contain whatever


X=Y+3 Y ? value at the beginning)

Y=X/6
Variables in Memory

Instruction sequence Memory

X = 20
after executing X = 2
0
X 20
Y = 15

X=Y+3 Y ?

Y=X/6
Variables in Memory

Instruction sequence Memory

X = 20

X 20
Y = 15
after execu
ting Y = 15

X=Y+3 Y 15

Y=X/6
Variables in Memory

Instruction sequence Memory

X = 20

X 18
Y = 15

X=Y+3 uti ng X = Y+3 Y 15


after exe c

Y=X/6

Note: Y does not change, only X changes after


execution of this instruction
Variables in Memory

Instruction sequence Memory

X = 20

X 18
Y = 15

X=Y+3 6 Y 3
Y =X/
ti ng
e xecu
after
Y=X/6
Data Types

• Each variable has a type, indicates what type of values the variable can hold

• Four common data types in C (there are others)


• int - can store integers (usually 4 bytes)
• float - can store single-precision floating point numbers (usually 4 bytes)
• double - can store double-precision floating point numbers (usually 8 bytes)
• char - can store a character (1 byte)
Rules and good practices

• First rule of variable use: Must declare a variable (specify its type and name) before using
it anywhere in your program

• All variable declarations should ideally be at the beginning of the main( ) or other
functions
• There are exceptions, we will see later

• A value can also be assigned to a variable at the time the variable is declared.
int speed = 30;
Initialization of a variable
char flag = ‘y’;
Variable Names
• Sequence of letters and digits
• First character must be a letter or ‘_’
• No special characters other than ‘_’
• No blank in between
• Names are case-sensitive (max and Max are two different names)

• Examples of valid names:


i rank1 MAX max Min class_rank _Average

• Examples of invalid names:


a’s fact rec 2sqroot class,rank
More Valid and Invalid Names (Identifiers)

Valid identifiers Invalid identifiers


X 10abc
abc my-name
simple_interest “hello”
a123 simple interest
LIST (area)
stud_name %rate
Empl_1
Empl_2
_avg_empl_salary
C Keywords

• Specific terms used by the C language having specific meaning, cannot be used as variable
names

• Examples:
int, float, char, double, main, if else, for, while, do, struct, union, typedef, enum, void,
return, signed, unsigned, case, break, sizeof,….

• There are others, see textbook…


Example 1
#include <stdio.h>
int main()
{
int x, y, sum;
Three integer type variables declared
scanf (“%d%d”, &x, &y);
sum = x + y;
Values assigned

printf( “%d plus %d is %d\n”,


x, y, sum );
return 0; 15 23
} 15 plus 23 is 38
Example 2
#include <stdio.h>
int main()
{
float x, y; /* Two floating point variables declared */
int d1, d2 = 10; /* Integer variable d2 is initialized to 10 */

scanf(“%f%f%d”,&x, &y, &d1);


printf( “%f plus %f is %f\n”, x, y, x + y);
printf( “%d minus %d is %d\n”, d1, d2, d1 – d2);
return 0;
}
Input: scanf function
• Performs input from keyboard
• It requires a format string and a list of variables into which the value received from the
keyboard will be stored
• format string = individual groups of characters (usually ‘%’ sign, followed by a conversion
character), with one character group for each variable in the list

int a, b; Commonly used conversion characters


float c; c for char type variable

Variable list (note the & before d for int type variable
each variable name)
f for float type variable
scanf( “%d%d%f”, &a, &b, &c); lf for double type variable
Format string
Examples
scanf ("%d", &size) ;
• Reads one integer from keyboard into an int type variable named size

scanf ("%c", &nextchar) ;


• Reads one character from keyboard into a char type variable named nextchar

scanf ("%f", &length) ;


• Reads one floating point (real) number from keyboard into a float type variable named
length

scanf (“%d%d”, &a, &b);


• Reads two integers from keyboard, the first one in an int type variable named a and the
second one in an int type variable named b
Important

• scanf( ) will wait for you to type the input from the keyboard

• You must type the same number of inputs as the number of %’s in the format string

• Example: if you have scanf (“%d%d”,…), then you must type two integers (in same line or
different lines), otherwise scanf( ) will just wait and the next statement will not be
executed
Reading a single character

• A single character can be read using scanf with %c

• It can also be read using the getchar() function


char c;

c = getchar();

• Program waits at the getchar() line until a character is typed, and then reads it and stores it
in c
Output: printf function
• Performs output to the standard output device (typically defined to be the screen)

• It requires a format string in which we can specify:


• The text to be printed out
• Specifications on how to print the values
printf ("The number is %d\n", num);

• The format specification %d causes the value listed after the format string to be
embedded in the output as a decimal number in place of %d

• Output will appear as: The number is 125


General syntax of printf( )
printf (format string, arg1, arg2, …, argn);

• format string refers to a string containing formatting information and data types of the
arguments to be output
• the arguments arg1, arg2, … represent list of variables/expressions whose values are to be
printed
• The conversion characters are the same as in scanf

• Examples:
printf (“Average of %d and %d is %f”, a, b, avg);
printf (“Hello \nGood \nMorning \n”);
printf (“%3d %3d %5d”, a, b, a*b+2);
printf (“%7.2f %5.1f”, x, y);
• Many more options are available for both printf and scanf – read from the book
More Examples
(Explain the outputs to test if you understood format strings etc.)
More point

#include <stdio.h> Output


int main()
Hello, World! Hello
World!
{
printf ("Hello, World! ") ;
printf ("Hello \n World! \n") ;
return 0;
}
Some more point

Output
#include <stdio.h>
Hello, World!
int main() Hello
{ World!
printf ("Hello, World! \n") ; Hell
printf ("Hello \n World! \n") ; o World!
printf ("Hell\no \t World! \n") ;
return 0;
}
Some more point
Output
Enter values for f1 and f2:
#include <stdio.h>
23.5 14.326
int main() Enter values for x1 and x2:
54 7
{
f1 = 23.500000, f2 = 14.33
float f1, f2; x1 = 54, x2 = 7
int x1, x2;
printf(“Enter values for f1 and f2: \n”); Can you explain why 14.326
scanf(“%f%f”, &f1, &f2); got printed as 14.33?
printf(“Enter values for x1 and x2: \n”);
scanf(“%d%d”, &x1, &x2);
printf(“f1 = %f, f2 = %5.2f\n”, f1, f2);
printf(“x1 = %d, x2 = %10d\n”, x1, x2);
return 0;
}

You might also like