0% found this document useful (0 votes)
8 views49 pages

Module I

c programming

Uploaded by

Sanskriti Gupta
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)
8 views49 pages

Module I

c programming

Uploaded by

Sanskriti Gupta
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/ 49

MODULE-I: INTRODUCTION

Prof Dipali Kadam

10-10-2024 PICT,Pune 1
Course overview
• This course provides students with an entry-level foundation in
computer programming.
• The goals of the course are to develop the programming ability in
students, and to improve their proficiency in applying the computing
fundamentals to their field of study.
• The basic aim is to teach the student to program in C at a level where
they are able to eventually write programs to help solve their everyday
engineering, science and technology related problems.

10-10-2024 PICT,Pune 2
Computer Architecture Foundations

10-10-2024 PICT,Pune 3
Machine cycle

10-10-2024 PICT,Pune 4
Memory types

10-10-2024 PICT,Pune 5
Low-level, High-level Coding Languages

• Low-level language
• The low-level language is a programming language that provides no
abstraction from the hardware, and it is represented in 0 or 1 forms,
which are the machine instructions.

• The languages that come under this category are the Machine level
language and Assembly language

10-10-2024 PICT,Pune 6
• Machine-level language
The machine-level language is a language that consists of a set of
instructions that are in the binary form 0 or 1.
As we know that computers can understand only machine instructions,
which are in binary digits, i.e., 0 and 1, so the instructions given to the
computer can be only in binary codes.

• Assembly Language
The assembly language contains some human-readable commands
such as mov, add, sub, etc. Since assembly language instructions are
written in English words like mov, add, sub, so it is easier to write and
understand.

10-10-2024 PICT,Pune 7
• High-Level Language
The high-level language is a programming language that allows a
programmer to write the programs which are independent of a
particular type of computer. The high-level languages are considered as
high-level because they are closer to human languages than machine-
level languages.

• Advantages of a high-level language


• The high-level language is easy to read, write, and maintain as it is written in
English like words.
• The high-level languages are designed to overcome the limitation of low-level
language, i.e., portability. The high-level language is portable; i.e., these
languages are machine-independent.

10-10-2024 PICT,Pune 8
Differences between Low-Level language and High-
Level language
Low-level language High-level language
It is a machine-friendly language, i.e., the It is a user-friendly language as this language is
computer understands the machine language, written in simple English words, which can be
which is represented in 0 or 1. easily understood by humans.
The low-level language takes more time to It executes at a faster pace.
execute.
It requires the assembler to convert the assembly It requires the compiler to convert the high-level
code into machine code. language instructions into machine code.

The machine code cannot run on all machines, so The high-level code can run all the platforms, so it
it is not a portable language. is a portable language.

It is memory efficient. It is less memory efficient.


Debugging and maintenance are not easier in a Debugging and maintenance are easier in a high-
low-level language. level language.
10-10-2024 PICT,Pune 9
Time Space Complexity

• Time complexity measures how many operations an algorithm completes


in relation to the size of the input. It aids in our analysis of the algorithm's
performance scaling with increasing input size.
• Time complexity is frequently expressed using Big O notation.

1. Best-case complexity (O(best)): This represents the minimum time required for an algorithm to
complete when given the optimal input. It denotes an algorithm operating at its peak efficiency under
ideal circumstances.
2. Worst-case complexity (O(worst)): This denotes the maximum time an algorithm will take to finish for
any given input. It represents the scenario where the algorithm encounters the most unfavourable
input.
3. Average-case complexity (O(average)): This estimates the typical running time of an algorithm when
averaged over all possible inputs. It provides a more realistic evaluation of an algorithm's performance.

10-10-2024 PICT,Pune 10
• Space complexity of an algorithm represents the
amount of memory space needed the algorithm in its
life cycle.
• A fixed part that is a space required to store certain
data and variables
• A variable part is a space required by variables, whose
size is totally dependent on the size of the problem. For
example, recursion stack space, dynamic memory allocation etc.

10-10-2024 PICT,Pune 11
Contents
1. Problem solving in everyday life,
• Types of problem: Algorithmic & Heuristic
• Problem solving with Computer
• Algorithms and Flowcharts.

2. Introduction to “C” Programming:


• D ata types
• Constants
• Keywords
• Variable
• Operators
• Expressions
• input/output statements
• Notion of Assembler ,Interpreter and Compiler.

10-10-2024 PICT,Pune 12
Problem solving in everyday life

Objectives
1. Describe the difference between heuristic and algorithmic solutions to
problems.
2. List and describe the six problem-solving steps to solve a problem that has
an algorithmic solution.
3. Use the six problem-solving steps to solve a problem.

10-10-2024 PICT,Pune 13
Steps
1. Identify the problem.
2. Understand the problem
3. Identify alternative ways to solve the problem
4. Select the best way to solve the problem from the list of alternative
solutions.
5. List instructions that enable you to solve the problem using the
selected solution.
6. Evaluate the solution.

10-10-2024 PICT,Pune 14
Take the problem of what to do this evening
• 1. Identify the problem. How do the individuals wish to spend the evening?
• 2. Understand the problem. With this simple problem, also, the knowledge base of the participants must be considered. The only
solutions that should be selected are ones that everyone involved would know how to do. You probably would not select as a
possible solution playing a game of chess if the participants did not know how to play.
• 3. Identify alternatives.
• a. Watch television.
• b. Invite friends over.
• c. Play video games.
• d. Go to the movies.
• e. Play miniature golf.
• f. Go to the amusement park.
• g. Go to a friend’s party. The list is complete only when you can think of no more alternatives.
• 4. Select the best way to solve the problem.
• a. Weed out alternatives that are not acceptable, such as those that cost too much money or do not interest one of the individuals involved.
• b. Specify the pros and cons of each remaining alternative.
• c. Weigh the pros and cons to make the final decision. This solution will be the best alternative if all the other steps were completed well.
• 5. Prepare a list of steps (instructions) that will result in a fun evening.
• 6. Evaluate the solution. Are we having fun yet? If nobody is having fun, then the planner needs to review the steps to have a fun
evening to see whether anything can be changed, if not then the process must start again

10-10-2024 PICT,Pune 15
Types of problem

• Problems do not always have straightforward solutions. Some problems, such as


balancing a checkbook or baking a cake, can be solved with a series of actions.
These solutions are called algorithmic solutions.

• The solutions of other problems, such as how to buy the best stock or whether to
expand the company, are not so straightforward. These solutions require
reasoning built on knowledge and experience, and a process of trial and error.
Solutions that cannot be reached through a direct set of steps are called heuristic
solutions.

10-10-2024 PICT,Pune 16
What is Algorithmic Thinking?

https://medium.com/enjoy-algorithm/how-to-develop-algorithmic-thinking-in-data-structure-and-algorithms-b000bbad1ab5

10-10-2024 PICT,Pune 17
Algorithmic Problem Solving
1 Structured Approach
Follows a specific set of instructions or rules to solve the problem.

2 Logical Reasoning
Utilizes logic and reasoning to systematically break down the problem into smaller
smaller steps.

3 Guarantee of Solution
If followed correctly, an algorithm will always lead to a solution.

4 Examples
Recipes, navigation systems, and mathematical equations.
What is heuristic Thinking?

• The objective of a heuristic algorithm is to apply a rule of thumb


approach to produce a solution in a reasonable time frame that
is good enough for solving the problem at hand.

10-10-2024 PICT,Pune 19
Heuristic Problem Solving
Trial and Error Intuitive Reasoning
Involves experimenting with different approaches until a Based on experience, intuition, and past knowledge.
solution is found.

No Guarantee of Solution Examples


May not always lead to the optimal or even a satisfactory Finding a lost object, writing a creative story, or solving a
solution. puzzle.
Problem Solving with Computers
I. the term solution means the instructions listed during step 5 of
problem solving—the instructions that must be followed to
produce the best results.
II. Results means the outcome or the completed computer-assisted
answer.
III. Program means the set of instructions that make up the solution
after they have been coded into a particular computer language.

10-10-2024 PICT,Pune 21
Questions
1.What are the six steps of problem solving?
2. What is an algorithmic solution to a problem?
3. Name three current problems in your life that could be solved through an algorithmic process. Explain
why each of these problems is algorithmic in nature.
4. What is a heuristic solution to a problem?
5. Name three current problems in your life that might be solved through a heuristic approach. Explain
why each of these problems is heuristic in nature.
6. Name three problems that might arise at home, at school, or in a business that could be solved more
efficiently with computer assistance. Do these problems require an algorithmic or heuristic solution?
Why?
7. State a reason why each of the six problem-solving steps is important in developing the best solution
for a problem. Give one reason for each step

10-10-2024 PICT,Pune 22
Problem Solving with Computers
1 Problem Definition
Clearly defining the problem and identifying the desired outcome.

2 Algorithm Development
Creating a step-by-step set of instructions that can be implemented by a computer.

3 Code Implementation
Translating the algorithm into a programming language that the computer can understand.

4 Testing and Debugging


Ensuring the code works correctly and fixing any errors that arise.
Algorithms and Flowcharts

• Objectives
1. Use the sequential logic structure to develop a solution to a
problem.
2. Use the proper form for instructions in an algorithm and flowchart
3. Use problem-solving tools to develop a solution to a problem

10-10-2024 PICT,Pune 24
Algorithms and their Characteristics

Well-defined Steps Finite Effective


Algorithms consist of clear and Algorithms must have a defined end Algorithms must be designed to solve
unambiguous instructions. point, ensuring they terminate after a a specific problem effectively.
finite number of steps.
Symbols functions
Start and End

Input and Output

Processes

Decision

Direction

Connector

Predefined process

For Loop

https://engineerstutor.com/2018/08/27/examples-of-algorithms-and-flow-charts-with-c-code/
10-10-2024 PICT,Pune 26
Implementing Algorithms through
Flowcharts
Flowchart Algorithm Implementation

Start Begin the program execution.

Input radius Get the radius value from the user.

Calculate area (π * radius²) Perform the calculation using the input


radius.

Output area Display the calculated area to the user.

End Terminate the program execution.


Introduction to “C” Programming:

/* Program - My First Program */


#include <stdio.h>
$ vim HelloWorld.c
int main()
$ gcc HelloWorld.c
{
$ ./a.out
printf("Hello World!\n");
Hello World!$
return 0;
}

10-10-2024 PICT,Pune 28
Data types
Data Type Range of value Size in Bits Range of value
Char 8 -128 to +127
Unsigned char 8 0 to 255
Signed char 8 -128 to +127

Int 32 -2147483648 to +2147483647


Signed int 32 -2147483648 to +2147483647
Unsigned int 32 0 to 4294967295
Float 32 3.4e-38 to 3.4e+38
Double 64 1.7e-308 to 1.7e+308 (accuracy up
to 15 digits)
Long double 80 3.4e-4932 to 1.1e+4932 (accuracy
up to 19 digits)

10-10-2024 PICT,Pune 29
Constant
• The value of a constant cannot be changed after an initial value is
assigned to it.
const float pi = 3.141;

• Without having to refer to a variable such a constant can be defined


simply by using the # define pre-processor directive.

#define pi 3.141

10-10-2024 PICT,Pune 30
Displaying Numbers

10-10-2024 PICT,Pune 31
/* Program 2 */
/* Program 1*/ #include <stdio.h>
#include <stdio.h> int main()
int main() {
{ int a = 3;
printf("%d\n",128); float b = 10.0;
printf("%f\n",128.0); float c;
return 0; c = b/a;
} printf("A is %d\n",a); //decimal value
printf("B is %d\n",b); //decimal value
printf("Answer is %f\n",c); //floating point value
return 0; }

A is 3
128 B is 0
128.000000 Answer is 3.333333

10-10-2024 PICT,Pune 32
Tokens
• The smallest element identified by the compiler in a source file is
called a token
• Tokens can be classified as keywords, literals, identifiers, operators,
etc.

10-10-2024 PICT,Pune 33
Identifiers
▪ A token (word) composed of a sequence of letters,
digits, and underscore (_) character. (NO spaces.)
▪ First character cannot be a digit
▪ C is case sensitive, so beware (e.g. printf
Printf)
▪ Identifiers such as printf normally would not be
redefined; be careful
▪ Used to give names to variables, functions, etc.
▪ Only the first 31 characters matter
Keywords
• Language specific tokens used by a programming language are called
keywords
• Keywords are also called as reserved words.
auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

10-10-2024 PICT,Pune 35
Variable
➢A variable has a value that can change.
➢It is a memory location that can hold a value of a certain data type.
➢Programmers refer to a variable by its name (identifier) so that it can
be accessed during the course of the program.
➢Programmers cannot use any of the keywords as variable names.

int a;
float total;
unsigned int index_no;
short int number_of_students;
10-10-2024 PICT,Pune 36
• Initializing a variable can be done while declaring it, just after
declaring it or later within the code (before accessing/evaluating its
value within an expression)
int a;
a = 10;

or

int a=10;

or

int a;
--------
--------
a = 10;

10-10-2024 PICT,Pune 37
Operators

• The operators define how the variables and literals in the expression
will be manipulated.
➢Assignment operator
➢Arithmetic operators
➢Relational operators
➢Logical operators
➢Bitwise operators
➢Special operators
➢Pointer operators

10-10-2024 PICT,Pune 38
Assignment Operator
• The assignment operator is the simple equal sign (=).
• The assignment operator is used to assign a value to a variable.
• The format of an assignment statement is:

variable-name = expression;

10-10-2024 PICT,Pune 39
• The expression can be a single variable or a literal, or it may contain
variables, literals and operators.
• The assignment operation always takes place from right to left.
Consider the following set of examples:

a = 5; // value of variable ‘a’ becomes 5


a = 5+10; // value of variable ‘a’ becomes 15
a = 5 + b; // value of variable ‘a’ becomes 5 + value of b
a = b + c; // value of variable ‘a’ becomes value of b + value of c
a = (x*x + y*y)/2;

10-10-2024 PICT,Pune 40
Arithmetic operators
• In C lots of shortcuts are possible. For example, instead of the
statement, a = a + b;
• the programmer may use the shorthand format:
a += b; or a++

10-10-2024 PICT,Pune 41
/* Program */
#include <stdio.h>
int main()
{
int a,b;
printf("Enter a: ");
scanf("%d", &a); //read value of a
printf("Enter b: ");
scanf("%d", &b); //read value of b Enter a: 5
printf("\na+b = %d", a+b); //display sum of a & b Enter b: 2
printf("\na-b = %d", a-b); //display subtraction of b from a a+b = 7
printf("\na*b = %d", a*b); //display multiplication of a & b a-b = 3
printf("\na/b = %d", a/b); //display division of a by b a*b = 10
printf("\na%%b = %d", a%b); //display modulus of a divided a/b = 2
by b a%b = 1
return 0;
}

10-10-2024 PICT,Pune 42
Input/output statements
• The scanf function accepts formatted input from the keyboard.
/* Program- */
#include<stdio.h>
int main()
{ 2
float a,b,sum; 3
scanf("%f", &a); // read 1st number Answer > 5.000000
scanf("%f", &b); // read 2nd number
sum = a + b; // total or
printf("a+b = %f\n", sum); //display answer as
float
return 0; 23
} Answer > 5.000000

10-10-2024 PICT,Pune 43
Relational and Logical Operators

10-10-2024 PICT,Pune 44
Bitwise Operators

int a,b,c;
a = 12;
b = 8;
c = a & b;

10-10-2024 PICT,Pune 45
/* Program- */
#include <stdio.h>
int main()
{
char input;
printf("Character to convert: ");
Executing Program- with character
scanf("%c",&input); //read character
“b” as the input will display the
printf("Converted character: %c", input ^ 32); //input XOR 32
following:
return 0;
Character to convert: b
}
Converted character: B

10-10-2024 PICT,Pune 46
Shift Operators
Shift operators allow shifting of bits either to left or right. We
know that 8 is represented in binary as:
variable >> number-of-bits
8 = 00001000
and when it is shifted by one bit to right, we get:
8 = 00001000 -> 00000100 = 4

variable >> number-of-bits


• Similarly when the number is shifted by one bit to left, we get:
16 = 00010000 <- 00001000 = 8

10-10-2024 PICT,Pune 47
Notion of Assembler, Interpreter and Compiler

Actual C Assembly Object .exe


code code code file
Source code Compiler Assembler Linker Loader

Pure C code
Memory
Preprocessor

10-10-2024 PICT,Pune 48
Source code
(High level Executable code
programming Interpreter (Machine Language)
language)

Get next
instruction

10-10-2024 PICT,Pune 49

You might also like