Professional Documents
Culture Documents
Unit II Week 4 C Language
Unit II Week 4 C Language
Unit 2
Outcomes
Students could be able to understand the
background of values and their memories
W
e
e
k
4
.
I
n
f
o
r
m
a
t
i
o
n
t
o
D
a
t
a
F
o
r
m
a
t
• it presents a framework for understanding algorithms; and it describes essential concepts in dealing with
data and code and in expressing the limits of modern computing machinery.
• Using Computational Thinking, able to apply basic strategies in problem solving, understand the
character of a solution or algorithm, and have a sense of the ways in which computerization and
digitization have changed how research is conducted.
• “Computational Thinking” as a major framing device to present core ideas in Computer Science.
3. Algorithmic Analysis
• The Analysis of algorithms can learn by constructing algorithms that iteratively process data, and
whose logic is based on the results of previous steps in the processing,
• So, we can build the algorithms to do exploratory data analysis, for example using constructs such as
decision trees and multi-dimensional scaling.
• These are algorithms that have informal descriptions and whose understanding will provide the
students both with key analytical tools, as well as with the insight into how those tools work.
• The data is not just about one-shot (if complex) mathematical functions applied to data, but rather
about algorithmically defined programs that branch and loop over the data to extract knowledge.
3. Algorithmic Analysis
• Within each of those campaigns there will be opportunities to both do direct analysis and then follow it with
algorithmic analysis in order to expose more complex and telling relationships.
• For example, students might be taught how to construct a decision tree to process and classify GPS time
series into activities, such as still, driving, and walking.
• The power of introducing decision trees is that they have a graphical as well as a predictive structure and the
relation between algorithms and mathematics behind is possible.
• Take a look at the structure and details of the decision tree and build an understanding for the algorithm.
3. Algorithmic Analysis
Example:
• Like the game of 20 questions, a decision tree is a series of splittings:
• The first split might be whether the person’s speed is greater than 25 miles an hour or not;
• A second might be whether they are near a freeway;
• A third might be based on what the person was doing in the previous time-slot, and so forth.
• Such algorithmic analysis is at the heart of data practices, modeling and many other aspects of modern
computer science.
• From this, we can see how it relates to important environmental and personal issues, and how it gets
applied to very personal instantiations of that data.
• The Sequence of the Instruction written to perform specific task is called the Program.
• This Instructions are formed using special symbols and words according to some rigid rule is known as
syntax rules.
• Every Instruction must be written according to the syntax rule of the languages.
• Like any other languages C languages has its vocabulary and grammar. They are follow up for making
the program.
Character Set:
⚫ Character Set is the set of the character.
⚫ This characters are used to form the word, numbers and expression.
⚫ The characters in the c are grouped into the following categories.
Letters : Uppercase A…Z, Lowercase a…z
Digits : All decimal digits 0 to 9
Special Character : ,(comma) .(period) ; (semicolon) :(colon), & (ampersand), # (number sign) etc.
White spaces : Blank Space, Horizontal Space, New Line.
C Tokens:
⚫ Smallest Individual units are known as C Tokens.
⚫ There are six types of the C Tokens.
Keywords
Identifiers
Constants
String
Special Symbols
Operators.
Keywords:
Identifiers:
Constants:
⚫ Constants referred as a fixed value that don't change during the execution of the program.
⚫ C Support Several types of constants:
Numeric Constants
Integer constants Real Constants
Character Constants
Integer Constants:
⚫ An integer constants refers as a sequence of digits.
⚫ There are three types of integer constants.
Decimal Integer
Octal Integer
Hexadecimal
Decimal Integer:
Octal Integer:
Hexadecimal Integer
Real Constants:
⚫ Integer numbers are inadequate to represent quantities such as distance, heights, temperature, price and
so on. These quantities are represented by a number containing fractional parts like 12.32. Such
numbers are called real constants
⚫ These numbers having a whole number followed by a decimal digits.
⚫ Example: 0.85, -0.75, 85.45, +241.54
Real number:
Example :
• A single character constant contains a single character enclosed with a pair of single quotation mark.
• Example: ‘1’, ‘a’, ‘$’
Example:
printf(“%d”,a); This will print the number stored in the variable ‘a’
printf(“%c”,97); This will print the character equivalent to the number 97 in ASCII
String Constants:
⚫ Example:
⚫ “Hello” , “1987”, “Well Done”, “X”
⚫ Note : A single String constant does not have an equivalent integer value, while a character constant
has an equivalent integer value.
⚫ Example:
⚫ \n - new line,
⚫ \t - horizontal tab,
⚫ \v-vertical tab
Variable:
⚫ A variable is a data name that may be used to store a data value.
⚫ Unlike constants that remain unchanged during the execution of the program, a variable may take
different value at different time.
Example:
• Siva, Krishnankoil, BTech_first, char_type ---- valid
• Amt$, int, class first, 938, (volume) ------- Invalid
Declaration of variables:
Syntax :
data_type v1,v2,v3….vn ;
where v1,v2..vn are different variable name.
Example:
• int count;
• double ratio;
• float price;
• char c;
where int, double, float and char are data type.
© Kalasalingam academy of research and education COURSE NAME
Information
Assignment statement:
Syntax:
variable-name = expression;
• Example:
price = 12.50;
number = 24;
character = ‘e’;
ratio = 12.2345;
a = a+2;
Note: Every statement must be ended with semicolon (;)
s = a+a*5;
© Kalasalingam academy of research and education COURSE NAME
Information
Assignment operator:
A value can be assigned to a variable when the variable is declared.
int radius = 10;
float pi = 3.14;
char f = ‘I’;
Several variable can be assigned the same using multiple assignment operators.
x=y=z=10;
f1=f2=‘I’; A same value can be assigned to multiple variables by
simply using assignment operators like this
speed=acce=0.0;
Some operators used during the assignment statement like the following;
s = a+a*5;
Some other examples are These are operators
s= u*t + 1*f*t2/2; Operators
x+y 26 20
x–y 14
x*y 120 6
x/y 3
x%y 2 It returns the quotient only
• For operators of the same priority, evaluation is from left to right as they appear.
• Parenthesis may be used to change the precedence of the operator evaluation.
© Kalasalingam academy of research and education COURSE NAME
Information- operators
a+b*c–d/e a + (b * c) – (d / e)
a*-b+d%e–f a * (-b) + (d % e) – f
a – b + c +d (((a-b)+c)+d)
x*y*z ((x * y) * z)
a+b+c*d*e (a + b) + ((c * d) * e)
Integer Arithmetic:
• When the operands in an arithmetic expression are integers, the expression is called integer expression,
and the operation is called integer arithmetic.
Real Arithmetic:
• Arithmetic operations involving only real or floating-point operands.
• Since floating point values are rounded to the number of significant digit permissible, the final value is
an approximation of the final result.
1.0 / 3.0 * 3.0 will have the value 0.99999 and not 1.0
Mixed-mode Arithmetic:
when one of the operands is integer and the other is real, the expression is called a mixed-mode
arithmetic expression.
if either operand is of the real type, then only real arithmetic is performed, and result is a real number.
25 /10 2
25 / 10.0 2.5
Type casting:
Example 1:
int x;
float r = 3.0;
x = (int)(2*r);
Relational operators:
used to compare two values.
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
== is equal to
!= is not equal to
Relational operators:
Examples:
10 > 20 is false
25 < 35.5 is true
12 > (7+5) is false
When arithemetic expressions are used on either side of a relational operator, the arithmetic expressions
will be evaluated first and then the results compared.
a+b>c–d is same as (a+b) > (c-d)
Relational operators:
Examples:
Sample code segment in C x> N
y? print y is larger
if (x > y)
printf(“%d is larger\n”);
Y
else
printf(“%d is larger\n”) print x is larger
Logical operators:
also known as logical connectives
They are
&& Logical AND
|| Local OR
What they do?
• They act upon operands that are themselves logical expressions.
• the individual logical expressions get combined into more complex conditions that are true or false.
Logical AND:
Temp > 50 (1)
a*b+c >= 25 (2)
These two are relational expressions
Connecting (1) and (2)
Temp > 50 && a*b+c>=25
Now, by connecting (1) and (2), we can make it to logical expressions
Logical OR:
exp1 || exp2 || exp3
the whole expression will be if any one of expressions.
but none of them are true means, the whole expressions will be false.
Logical AND
Result is true if both the operands are true or if all operands are true.
Logical OR
Result is true if at least one of the operands are true.
X Y X && Y X || Y
FALSE FALSE FALSE FALSE
FALSE TRUE FALSE TRUE
TRUE FALSE FALSE TRUE
TRUE TRUE TRUE TRUE
Number System
Non-
Positional Decimal Binary Octal Hexadecim
positional
number number number number al number
number
system system system system system
system
Example
258610 = (2 x 103) + (5 x 102) + (8 x 101) + (6 x 100)
= 2000 + 500 + 80 + 6
Example
= 16 + 0 + 4 + 0 + 1
= 2110
In order to be specific about which number system we are referring to, it is a common practice to
indicate the base as a subscript. Thus, we write:
101012 = 2110
Bit
Byte 8 Bits 1
• Base 64 is an encoding scheme that converts binary data into text format
• So that encoded textual data can be easily transported over network un-corrupted and without any data
loss.
• Base64 is used commonly in a number of applications including email via MIME, and storing complex
data in XML.
• Problem with sending normal binary data to a network is that bits can be misinterpreted by
underlying protocols, produce incorrect data at receiving node and that is why we use this code.
Why base 64 ?
• Resultant text after encoding our data has those characters which are widely present in many character
sets.
• so there is very less chance of data being corrupted or modified.
char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
// 64 characters
ASCII Table
Example.
• We have to encode string “MENON” into base64 format.
• Lets call “MENON” as input_str, above base64 character set (“ABC..+/”) as char_set and resultant
encoded string as res_str.
Basic Idea:
Step 1: Take 3 characters from input_str i.e “MEN” since each character size is 8 bits we will have(8 * 3)
24 bits with us.
Step 2: Group them in a block of 6 bits each (24 / 6 = 4 blocks). (why 6?) because 2^6 = 64 characters, with
6 bits we can represent each character in char_set.
Step 3: Convert each block of 6 bits to its corresponding decimal value. Decimal value obtained is the
index of resultant encoded character in char_set.
Step 4: So for each 3 characters from input_str we will receive 4 characters in res_str.
Step 5:
What if we have less than 3 characters in input_str left i.e “ON”. We have 16 bits and blocks will be 16 /
6 = 2 blocks. Rightmost 4 bits will not make a proper block (1 block = 6 bits) so we append zeros to right
side of block to make it a proper block i.e 2 zeros will be appended to right. Now we have 3 proper blocks,
find corresponding decimal value of each block to get index.
Step 6:
Since There were less than 3 characters (“ON”) in input_str we will append “=” in res_str. e.g “ON” here
3 – 2 = 1 padding of “=” in res_str.
M : 77 (01001101), E : 69 (01000101),
Example 2:
Starting from left make blocks of 6 bits until all bits are covered
BIT-STREAM :
(010011) (010100) (010101) (001110) (010011) (110100) (1110)
Example 3:
If the rightmost block is less than 6 bits just append zeros to the right of that block to make it 6 bits. Here in
above example we have to appended 2 zeros to make it 6.
BIT-STREAM :
(010011) (010100) (010101) (001110) (010011) (110100) (111000)
Example 4:
Take 3 characters from input_str (“MEN”) i.e 24 bits and find corresponding decimal value (index to
char_set).
BLOCKS :
INDEX --> (010011) : 19, (010100) : 20, (010101) : 21, (001110) : 14
char_set[19] = T, char_set[20] = U, char_set[21] = V, char_set[14] = O
Example 5:
Take remaining characters (“ON”). We have to pad resultant encoded string with 1 “=” as number of
characters is less than 3 in input_str. (3 – 2 = 1 padding)
BLOCKS :
INDEX --> (010011) : 19 (110100) : 52 (111000) : 56
char_set[19] = T char_set[52] = 0 char_set[21] = 4
So our input_str = "ON" will be converted to encoded string "T04=“.
Examples:
Input : MENON // string in ASCII
Output :TUVOT04= // encoded string in Base 64.
Types of Information/Data
Different examples and their rules
Week 4 Summary
We learned about how to use the information/data
and convert into the corresponding types of data