You are on page 1of 122

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Searching and Sorting

Dr. Muhammad Yousaf Hamza


Linear Search
#include <stdio.h> for (c = 0; c < n; c++)
int main() {
{ if (array[c] == search)
int array[100], search, c, n; {
printf("Enter number of
printf("%d is present at
elements in array,
maxim 100\n"); location %d.\n", search, c+1);
scanf("%d", &n); break;
printf("Enter %d }
integer\n", n); }
for (c = 0; c < n; c++) if (c == n)
scanf("%d", &array[c]); printf("%d isn't present in the
printf("Enter a number to array.\n", search);
search\n"); getchar(); return 0;
scanf("%d", &search);
}
Dr. Yousaf, PIEAS
Linear Search for multiple occurrences
#include <stdio.h> for (c = 0; c < n; c++)
int main() {
{ if (array[c] == search)
int array[100], search, c, n; {
printf("%d is present at
printf("Enter number of
location %d.\n", search, c+1);
elements in array, count++;
maxim 100\n"); }
scanf("%d", &n); }
printf("Enter %d if (count == 0)
integer\n", n); printf("%d isn't present in the
for (c = 0; c < n; c++) array.\n", search);
scanf("%d", &array[c]); else
printf("Enter a number to printf("%d is present %d times in
search\n"); the array. \n”, search, count);
scanf("%d", &search); getchar(); return 0;
}
Dr. Yousaf, PIEAS
Binary Search
Binary search in C language to find an element
in a sorted array. If you wish to use it on an
array that isn't in sorted order, then you must
sort the array using some sorting technique
and then use it to find the desired element. If
the element to search is present in the list,
then we print its location. The program
assumes that the input numbers are
in ascending order.

Dr. Yousaf, PIEAS


//Selection Sort for (i = 0; i < n-1; i++)
#include <stdio.h> {// Find the smallest value
int main()
{
min_index = i;
int array[100], n, i, j, min_index, for (j = i+1; j < n; j++)
swap; if (array[j] < rray[min_index])
printf("Enter number of { min_index = j; }
elements\n"); /* Swap the the first element
scanf("%d", &n);
with smallest element */
printf("Enter %d integers\n", n); swap = array[i];
for (i = 0; i < n; i++) array[i] = array[min_index];
scanf("%d", &array[i]); array[min_index] = swap;
}
printf("Sorted list in ascending
order:\n");
for (i = 0; i < n; i++)
printf("%d ", array[i]);
getchar(); return 0; }
Dr. Yousaf, PIEAS
Numerical Integration

Dr. Muhammad Yousaf Hamza


INTRODUCTION
Numerical integration
#include<stdio.h>
int main()
{ int n, i;
float a,b,x,dx = 0.2,fa,fb,fx, sum = 0, integ;
a = 0; b = 1; n = 5;
x = a; fa = 1 - x*x;
x = b; fb = 1 - x*x;
x = 0.2;
for (i =1; i<=n-1; i++)
{ fx = 1 - x*x;
sum = sum + fx;
x = x+dx;
}
integ = ( (b-a)/n) * (fa + 2*sum + fb)/2.0;
printf("%f", integ);
getchar();return 0;
}
Importance of Computational and
Simulation Work

Dr. Yousaf, PIEAS


Importance of Computational Work
• Computational and simulation (C&S) work has become a
fundamental tool in the fields of science and engineering. It
has geared the scientific research. It is enabling and very
important technology in almost every area of life.

• The computational and simulation work can lead to the


successful as well as economical development of scientific
systems.

• C&S helps for making the correct decisions among the many
available choices. It also helps to model prototypes and
simulators. C&S has been approved as a powerful tool for
scientific research. Due to very attractive benefits, the
computational and simulation has been technically accepted
across the globe. It has not only increased the efficiency of
scientific research but also made it faster and cheaper.

Dr. Yousaf, PIEAS


Importance of Computational Work
The potential benefits of Simulations are:
• The trained students can understand the assumptions
and implementation procedures to handle a problem
in efficient way at their work places.
• They can have deep knowledge of a system through
quantitative analysis by performing extensive
simulations.
• The knowledge that the students gain can be applied
in various scientific applications. Different parameters
involved in the working of a system can be
understood efficiently.
• As the experience of C&S work enhances the
visualization of a system, it can be used as a
diagnostic tool for measuring the efficiency and
performance of various systems.
Dr. Yousaf, PIEAS
Importance of Computational Work
• The trained students can foresee the response
of a system during normal and hypothetical
accidental situations.
• The trained students can identify the role of
most important components in a system
through C&S work.
• It would help them to estimate the system
parameters prior to the actual system design. If
a component for a system is to be selected
which can perform the best among the various
types, then the optimum choice for the selection
of the component can be made without having
the need of buying actual physical components.
Dr. Yousaf, PIEAS
Importance of Computational Work
• The trained student can design/test various modules
and then can integrate them in efficient way to
prepare a prototype. After carefully testing of such
prototype, the new developments can be made more
efficiently and economically.
• Having experience in C&S, the trained students can
predict the changes in a system under certain
circumstances. By this knowledge some pre-emptive
measurements can be taken to avoid any accident at
the projects.
• C&S work allows for efficient if-then-else analyses
without actually performing the experiments. It
would save the time/money/labor at the projects.
• As the decision making abilities are enhanced through
C&S, the trained students can make technical
decisions more conveniently and correctly.
Dr. Yousaf, PIEAS
Usefulness
The computational and simulation work is
helpful in electrical systems. Some of the
applications are mentioned below.
• For programming of the controllers to control
certain processes
• Models of state estimators and filters
• Models of uncertainties, disturbances and
noises
• Models of faults
• For power network design
• For voice and sound recognition
• For processing of the digital data.
Dr. Muhammad Yousaf Hamza
Importance of Computer Simulations
in Physics and Medical Physics

Dr. Yousaf, PIEAS


Importance of Computer Simulations
in Metallurgical

Dr. Yousaf, PIEAS


122 Pages Book
Revision

Dr. Muhammad Yousaf Hamza


Qualification
• Postdoc.
Department of Electrical Engineering, University of Minnesota, Duluth, USA.
• Ph.D.
Lahore University of Management Sciences (LUMS), Lahore, Pakistan.
Carried one-year PhD research at University of Ottawa, Canada.
• MS (Computer Science)
Lahore University of Management Sciences (LUMS), Lahore, Pakistan.
(With Honors and Merit certificate)
• M.Phil. (Electronics)
Quaid-i-Azam University, Islamabad, Pakistan. (Distinction and Merit Certificate)
• Post Graduate Course
Post-Graduate Course in Lasers and Optics, PIEAS, Nilore, Islamabad, Pakistan.
• B.Sc. Electrical Engineering
University of Engineering and Technology (UET), Lahore, Pakistan.
• F.Sc. Government College, Lahore.
Dr. Muhammad Yousaf Hamza
Dr. Muhammad Yousaf Hamza
Let us Write First C Program

Dr. Muhammad Yousaf Hamza


Approach To Our First C Program
x = 5;
y = 7;
z = x + y;

x, y and z are integer variables

Dr. Muhammad Yousaf Hamza


Data Types
• Integer variables (int)
For example:
– Number of students
– Number of tables
• Floating point variables (float)
For quantities which may contain decimal point
such as distance, area, and temperature.
• Character variables (char)

At this stage, we will focus only on integers.

Dr. Muhammad Yousaf Hamza


Approach To Our First C Program
x = 5;
y = 7;
z = x + y;

x, y and z are integer variables

Dr. Muhammad Yousaf Hamza


#include <stdio.h>
int main( )
{ //Our First C Program
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“%d", z);

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


#include <stdio.h>
int main( )
{ //Our First C Program
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“%d", z);

getchar();
return 0;
}
12
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“Sum is %d", z); // Compare it with printf(“%d", z);


getchar();
return 0;
}
Sum is 12
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d", z);


printf("Difference is %d", m);

getchar();
return 0;
}
Sum is 12Difference is -2
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\n", z);


printf("Difference is %d", m);
getchar();
return 0;
}
Sum is 12
Difference is -2
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m); // two outputs in


//one printf
getchar();
return 0;
}
What’s output?
S
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m); // two outputs in


//one printf
getchar();
return 0;
}
What’s output?
Sum is 12
Difference is -2 Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
Exercise
Try out various escape
int x, y, z, m;
sequences in this program.
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m);

getchar();
return 0;
}
Sum is 12
Difference is -2
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x = 5, y = 7, z; // initialization with declaration

z = x + y;

printf(“%d", z);

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d\n", x); // 5

x = x + 3;
printf(“%d\n", x); // 8

printf(“%d\n", x*6); // 48

printf(“%d\n", x); // 8
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Names of C Variables
• A good name for your variables is important
• Variables in C can be given any name made from numbers,
letters and underscores which is not a keyword and does
not begin with a number.
• Names may contain letters, digits and underscores
• The first character must be a letter or an underscore.
• First 31 characters are significant
(too long name is as bad as too short).
• Are case sensitive:
– abc is different from ABC
• Must begin with a letter or underscore and the rest can be
letters, digits, and underscores.

Dr. Muhammad Yousaf Hamza


#include <stdio.h>
int main( )
{
int x, y, z;
printf("This program adds two integer values\n");

printf ("Enter 1st Integer Value: ");


scanf ( "%d", &x);

printf ("Enter 2nd Integer Value:");


scanf ( "%d", &y);

z = x + y;
printf("Sum is %d", z);
getchar();
getchar(); return 0;
} Dr. Muhammad Yousaf Hamza
Example
#include <stdio.h>
int main ()
{
float radius, area;
printf ("Enter the value of radius ");
scanf ( "%f", &radius);

area = 3.14159 * radius * radius;


printf ("Area = %f", area);

getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Dr. Muhammad Yousaf Hamza
Arithmetic Operators
Operator Symbol Action

Addition + Adds operands x+y


Subtraction - Subtracts from first x-y
Negation - Negates operand -x
Multiplication * Multiplies operands x*y
Division / Divides first by second x/y
(integer quotient)
Modulus % Remainder of divide op x%y

• (x % y) gives the remainder when x is divided by y


• remainder= x%y; (ints only)
Dr. Muhammad Yousaf Hamza
Increment and Decrement Operators

Dr. Muhammad Yousaf Hamza


Mathematical Functions

Dr. Yousaf, PIEAS


Control Statements

Dr. Yousaf, PIEAS


Relational Operators

Dr. Yousaf, PIEAS


Relational Operators
Relational operators allow you to compare
variables.
– They return a 1 value for true and a 0 for false.
Operator Symbol Example

Greater than > x > y


Less than < x< y
Greater than/equals >= x >= y
Less than/equals <= x <= y
Equals == x == y
Not equal != x != y NOT x = y

Dr. Yousaf, PIEAS


if else statements

Dr. Yousaf, PIEAS


if else if statements

Dr. Yousaf, PIEAS


Logical Operators

Dr. Yousaf, PIEAS


Logical Operators

• && AND

• || OR

• ! NOT

Dr. Yousaf, PIEAS


Conditional Operator

Dr. Yousaf, PIEAS


switch statement

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1 star as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1 star as
#include<stdio.h>

int main()
{
printf("*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 2 stars as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 2 stars as
#include<stdio.h>

int main()
{
printf("*\n*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as
#include<stdio.h>

int main()
{
printf("*\n*\n*\n*\n*\n*\n*\n*\n*\n*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1000 stars in the
format shown in the previous slides.

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1000 stars in the
format shown in the previous slides.

Too much labor work?

There MUST be an easier way

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting 1 to 10 as

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting 1 to 10 as
#include<stdio.h>

int main()
{
printf("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting from 1 to
1000 in the format shown in the previous slide.

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting from 1 to
1000 in the format shown in the previous slide.

• Too much labor work?


• There MUST be an easier way
• Is there any?
• YES

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting from 1 to
1000 in the format shown in the previous slide.

• Too much labor work?


• There MUST be an easier way
• Is there any?
• YES
LOOPS
Dr. Yousaf, PIEAS
Loops

Dr. Yousaf, PIEAS


Examples
There are three types of loops in C-Language

• for loop

• while loop

• do-while loop

Dr. Yousaf, PIEAS


For Loop

Dr. Yousaf, PIEAS


20-Spans System

Span 1 Span 2 Span 20

I/P OA OA OA OA OA O/P
DCF1 DCF2 DCF20

80 km 80 km 80 km
while Loop

Dr. Yousaf, PIEAS


do while Loop

Dr. Yousaf, PIEAS


Infinite Loops

Dr. Yousaf, PIEAS


break and continue Statements

Dr. Yousaf, PIEAS


exit() function

Dr. Yousaf, PIEAS


exit()
// About exit() function. Page 236
#include<stdio.h>
#include<stdlib.h> // to use exit() function.
int main()
{
int i, j, k;
for (i = 0; i<5; i++)
printf("%d\n", i);
getchar();
exit(0); // it will exit the program
j = 23;
printf("j = %d\n", j);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Continue and exit together
#include<stdio.h>
// Continue to enter integer values. This program
#include<stdlib.h>
//will terminate when you enter 5 odd numbers.
int main()
{
int num;
int count=0;
while(1)
{
printf("\nEnter an odd number :");
scanf("%d",&num);
if(num%2==0)
continue;
count++;
if(count==5)
exit(0);
}
getchar();
return 0;
} Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1 star as
#include<stdio.h>

int main()
{
printf("*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as

#include<stdio.h>
int main()
{
int i;
for (i = 0; i<10; i++)
printf("*");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 2 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 2 Rows as
#include<stdio.h>
int main()
{
int i; // use of 2 for loops
for (i = 0; i<10; i++)
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 3 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 3 Rows as
#include<stdio.h>
int main() getchar();
{ return 0;
int i; // use of 3 for loops }
for (i = 0; i<10; i++)
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 4 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 4 Rows as
#include<stdio.h> printf("\n");
int main()
{ for (i = 0; i<10; i++)
int i; // use of 4 for loops {
for (i = 0; i<10; i++) printf("*");
{ }
printf("*");
} getchar();
printf("\n"); return 0;
for (i = 0; i<10; i++) }
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 5 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 5 Rows as
#include<stdio.h> printf("\n");
int main()
{ for (i = 0; i<10; i++)
int i; // use of 5 for loops {
for (i = 0; i<10; i++) printf("*");
{ }
printf("*"); printf("\n");
}
printf("\n"); for (i = 0; i<10; i++)
for (i = 0; i<10; i++) {
{ printf("*");
printf("*"); }
} getchar();
printf("\n"); return 0;
for (i = 0; i<10; i++) }
{
printf("*");
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 500 Rows as

Use of 500 for loops???

It’s not efficient/realistic approach

Efficient Solution?

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 500 Rows as

Use of 500 for loops???

It’s not efficient/realistic approach

Efficient Solution?

NESTED Loops

Dr. Yousaf, PIEAS


Nested Loops

Dr. Yousaf, PIEAS


Arrays

Dr. Yousaf, PIEAS


2-D Arrays
Nesting in Loops

Dr. Yousaf, PIEAS


Characters and Strings

Dr. Yousaf, PIEAS


Strings Operations

Dr. Yousaf, PIEAS


FUNCTIONS

Dr. Yousaf, PIEAS


Anatomy of a C program

Dr. Yousaf, PIEAS


Recursion

‫ﺗﮑرار‬

Dr. Yousaf, PIEAS


Dr. Muhammad Yousaf Hamza
Scope of Variables

Dr. Yousaf, PIEAS


Local and Global Variables

Dr. Yousaf, PIEAS


Automatic Versus Static Variables

Dr. Yousaf, PIEAS


To Generate Random Numbers
#include <stdio.h> If we use rand() only as
#include <stdlib.h>
int main() n = rand();
It can generate random number
{ of value in thousands. So when
int c, n; we use
printf("Ten random numbers n = rand()%100 + 1;
in [1,100]\n");
for (c = 1; c <= 10; c++) Whatever be the value of
random number, its modulus
{ with hundred will always be in
n = rand()%100 + 1; the range 0 to 99. By adding 1, n
printf("%d\n", n); will always be in the range from
} 1 to 100.
getchar(); return 0; Concept of Seed
srand(Study it yourself)
}
Dr. Yousaf, PIEAS
POINTERS

Dr. Yousaf, PIEAS


Arrays and Pointers

Dr. Yousaf, PIEAS


Pass By Reference

Dr. Yousaf, PIEAS


Passing Arrays to Functions

Dr. Yousaf, PIEAS


#include<stdio.h> void add_mat(int x[2][2],
void add_mat(int x[2][2], int y[2][2], int y[2][2],int z[2][2])
int z[2][2]); {
int main()
int i, j;
{ int mat1[2][2]={ {1,2},{3,4} },
mat2[2][2] = { {5,6},{7,8} }, for (i = 0; i<2;i++)
res_mat[2][2]; for (j = 0; j<2; j++)
int i,j; z[i][j] = x[i][j] + y[i][j];
add_mat(mat1, mat2, res_mat); }
for (i = 0; i<2;i++)
{
printf("\n");
for (j = 0; j<2; j++)
printf("%d \t", res_mat[i][j]);
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
STRUCTURES

Dr. Yousaf, PIEAS


Arrays of Structures

Dr. Yousaf, PIEAS


Nested Structures

Dr. Yousaf, PIEAS


FILE HANDLING

Dr. Yousaf, PIEAS


Graph of Sine θ
1

0.8

0.6

0.4

0.2
sine θ

-0.2

-0.4

-0.6

-0.8

-1
0 90 180 270 360 450 540 630 720
θ (degress)

Dr. Yousaf, PIEAS


Importance of Computer
Programming is Obvious
World Wide Applications
HOPE
+
EFFORT

Dr. Yousaf, PIEAS


It is The

END

of the course

Dr. Yousaf, PIEAS


BEST WISHES

Dr. Yousaf, PIEAS

You might also like