SlideShare a Scribd company logo
1 of 23
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
FILE HANDLING
Dr. Yousaf, PIEAS
File Handling
# include <stdio.h>
int main()
{
FILE *fp; // you can select any valid name of file.
int x = 10;
fp = fopen("File1.txt","w");
if (fp == NULL)
printf ("There is an error");
else
{
fprintf(fp, "%d", x);
printf("Data has been stored in the file");
fclose(fp);
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
Modes for opening files
• The second argument of fopen is the mode in
which we open the file. There are three
• "r" opens a file for reading
• "w" opens a file for writing - and writes over all
previous contents (deletes the file so be careful!)
• "a" opens a file for appending - writing on the end of
the file
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int i,j;
float x, y[1000];
i = 0;
for (x = 0; x<=10; x = x+0.1)
{
y[i] = x;
printf("%fn", y[i]);
i++;
}
getchar(); return 0;
}
/* We need to store y points in a file. So look at the following
program*/
Dr. Yousaf, PIEAS
// Graph of y versus x
#include<stdio.h>
int main()
{
int i;
float x, y[101];
FILE *fpx, *fpy;
fpx = fopen("x.txt","w");
fpy = fopen("y.txt","w");
i = 0;
for (x = 0; x<=10; x = x+0.1)
{
y[i] = x;
fprintf(fpx, "%fn", x);
fprintf(fpy, "%fn", y[i]);
i++;
}
printf("Data has been stored in the file");
getchar(); return 0; }
Dr. Yousaf, PIEAS
%%% Matlab Code
x = load('x.txt');
y = load('y.txt');
figure(1)
plot(x,y)
Dr. Yousaf, PIEAS
Y Versus X
Dr. Yousaf, PIEAS
0 2 4 6 8 10
0
1
2
3
4
5
6
7
8
9
10
X
Y
// Graph of y versus x^2
#include<stdio.h>
#include<math.h>
int main()
{
int i;
double x, y[101];
FILE *fpx, *fpy;
fpx = fopen("xs.txt","w");
fpy = fopen("ys.txt","w");
i = 0;
for (x = 0; x<=10; x = x+0.1)
{
y[i] = pow(x,2);
fprintf(fpx, "%lfn", x);
fprintf(fpy, "%lfn", y[i]);
i++;
}
printf("Data has been stored in the file");
getchar(); return 0; }
Dr. Yousaf, PIEAS
%%% Matlab Code
x = load('xs.txt');
y = load('ys.txt');
figure(1)
plot(x,y)
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
0 2 4 6 8 10
0
10
20
30
40
50
60
70
80
90
100
X
Y
Y Versus X2
/* Graph of sine(theta)
/* Graph of sine(theta)
Please read Page 378 for many
trigonometric functions*/
#include<stdio.h>
#include<math.h>
# define PI 22.0/7.0
int main()
{
double theta_deg, theta_rad, y[2000];
int i = 0;
FILE *fptheta, *fpsine;
fptheta = fopen("theta.txt","w");
fpsine = fopen("sine.txt","w");
for (theta_deg = 0;
theta_deg<=720; theta_deg++)
{
theta_rad =
((PI)/180.0)*theta_deg;
y[i] = sin(theta_rad);
fprintf(fptheta, "%lfn",
theta_deg);
fprintf(fpsine, "%lfn", y[i]);
i++;
}
printf("Data has been stored in
the file");
getchar(); return 0; }
Dr. Yousaf, PIEAS
%%%%Matlab code
x = load('theta.txt');
y = load('sine.txt');
figure(1)
plot(x,y)
Dr. Yousaf, PIEAS
Graph of Sine θ
Dr. Yousaf, PIEAS
0 90 180 270 360 450 540 630 720
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
θ (degress)
sineθ
SEARCHING AND SORTING
Dr. Yousaf, PIEAS
//Linear Search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in
arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location
%d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in
array.n", search);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
/*Binary Search (only for arrays which
are already sorted). */
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search,
array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integers in asending
ordern", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] == search )
{
printf("%d found at location
%d.n", search, middle+1);
break;
}
else if ( array[middle] < search )
first = middle + 1;
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present
in the list.n", search);
return 0;
}
Dr. Yousaf, PIEAS
Bubble Sort
Dr. Yousaf, PIEAS
//Bubble Sort
#include<stdio.h>
int main()
{
int n,temp,i,j,a[20];
printf("Enter total numbers of elements:
");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
//Bubble sorting algorithm
for(i=n-2; i>=0; i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
//Selection Sort
#include <stdio.h>
int main()
{ int array[100], n, c, d, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{ position = c;
for (d = c + 1; d < n; d++)
{ if (array[position] > array[d])
position = d;
}
if (position != c)
{ swap = array[c];
array[c] = array[position];
array[position] = swap; }
}
//Selection Sort
printf("Sorted list in ascending
order:n");
for (c = 0; c < n; c++)
printf("%dn", array[c]);
getchar();
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Importance of Software
Dr. Yousaf, PIEAS
It is The END of the Course
Dr. Yousaf, PIEAS

More Related Content

What's hot

Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
PrinceGuru MS
 
for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...
hwbloom59
 

What's hot (10)

Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown Parts
 
for this particular program how do i create the input innotepad 1st ?#include...
for this particular program how do i create the input innotepad 1st ?#include...for this particular program how do i create the input innotepad 1st ?#include...
for this particular program how do i create the input innotepad 1st ?#include...
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and Beyond
 
Workshop programs
Workshop programsWorkshop programs
Workshop programs
 
SparkSQLの構文解析
SparkSQLの構文解析SparkSQLの構文解析
SparkSQLの構文解析
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
 

Similar to C Language Lecture 22

Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 

Similar to C Language Lecture 22 (20)

C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C++
C++C++
C++
 
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
 
UNIVERSIDAD CENTRAL DEL ECUADOR C++
UNIVERSIDAD CENTRAL DEL ECUADOR C++UNIVERSIDAD CENTRAL DEL ECUADOR C++
UNIVERSIDAD CENTRAL DEL ECUADOR C++
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
Data structures
Data structuresData structures
Data structures
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
C programming file handling
C  programming file handlingC  programming file handling
C programming file handling
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C programms
C programmsC programms
C programms
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Lab test 2
Lab test 2Lab test 2
Lab test 2
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
 
Programs
ProgramsPrograms
Programs
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 

More from Shahzaib Ajmal (15)

C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 

C Language Lecture 22

  • 3. File Handling # include <stdio.h> int main() { FILE *fp; // you can select any valid name of file. int x = 10; fp = fopen("File1.txt","w"); if (fp == NULL) printf ("There is an error"); else { fprintf(fp, "%d", x); printf("Data has been stored in the file"); fclose(fp); } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 4. Modes for opening files • The second argument of fopen is the mode in which we open the file. There are three • "r" opens a file for reading • "w" opens a file for writing - and writes over all previous contents (deletes the file so be careful!) • "a" opens a file for appending - writing on the end of the file Dr. Yousaf, PIEAS
  • 5. #include<stdio.h> int main() { int i,j; float x, y[1000]; i = 0; for (x = 0; x<=10; x = x+0.1) { y[i] = x; printf("%fn", y[i]); i++; } getchar(); return 0; } /* We need to store y points in a file. So look at the following program*/ Dr. Yousaf, PIEAS
  • 6. // Graph of y versus x #include<stdio.h> int main() { int i; float x, y[101]; FILE *fpx, *fpy; fpx = fopen("x.txt","w"); fpy = fopen("y.txt","w"); i = 0; for (x = 0; x<=10; x = x+0.1) { y[i] = x; fprintf(fpx, "%fn", x); fprintf(fpy, "%fn", y[i]); i++; } printf("Data has been stored in the file"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 7. %%% Matlab Code x = load('x.txt'); y = load('y.txt'); figure(1) plot(x,y) Dr. Yousaf, PIEAS
  • 8. Y Versus X Dr. Yousaf, PIEAS 0 2 4 6 8 10 0 1 2 3 4 5 6 7 8 9 10 X Y
  • 9. // Graph of y versus x^2 #include<stdio.h> #include<math.h> int main() { int i; double x, y[101]; FILE *fpx, *fpy; fpx = fopen("xs.txt","w"); fpy = fopen("ys.txt","w"); i = 0; for (x = 0; x<=10; x = x+0.1) { y[i] = pow(x,2); fprintf(fpx, "%lfn", x); fprintf(fpy, "%lfn", y[i]); i++; } printf("Data has been stored in the file"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 10. %%% Matlab Code x = load('xs.txt'); y = load('ys.txt'); figure(1) plot(x,y) Dr. Yousaf, PIEAS
  • 11. Dr. Yousaf, PIEAS 0 2 4 6 8 10 0 10 20 30 40 50 60 70 80 90 100 X Y Y Versus X2
  • 12. /* Graph of sine(theta) /* Graph of sine(theta) Please read Page 378 for many trigonometric functions*/ #include<stdio.h> #include<math.h> # define PI 22.0/7.0 int main() { double theta_deg, theta_rad, y[2000]; int i = 0; FILE *fptheta, *fpsine; fptheta = fopen("theta.txt","w"); fpsine = fopen("sine.txt","w"); for (theta_deg = 0; theta_deg<=720; theta_deg++) { theta_rad = ((PI)/180.0)*theta_deg; y[i] = sin(theta_rad); fprintf(fptheta, "%lfn", theta_deg); fprintf(fpsine, "%lfn", y[i]); i++; } printf("Data has been stored in the file"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 13. %%%%Matlab code x = load('theta.txt'); y = load('sine.txt'); figure(1) plot(x,y) Dr. Yousaf, PIEAS
  • 14. Graph of Sine θ Dr. Yousaf, PIEAS 0 90 180 270 360 450 540 630 720 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 θ (degress) sineθ
  • 15. SEARCHING AND SORTING Dr. Yousaf, PIEAS
  • 16. //Linear Search #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) { printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 17. /*Binary Search (only for arrays which are already sorted). */ #include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integers in asending ordern", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to findn"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] == search ) { printf("%d found at location %d.n", search, middle+1); break; } else if ( array[middle] < search ) first = middle + 1; else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.n", search); return 0; } Dr. Yousaf, PIEAS
  • 19. //Bubble Sort #include<stdio.h> int main() { int n,temp,i,j,a[20]; printf("Enter total numbers of elements: "); scanf("%d",&n); printf("Enter %d elements: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); //Bubble sorting algorithm for(i=n-2; i>=0; i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("After sorting: "); for(i=0;i<n;i++) printf(" %d",a[i]); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 21. //Selection Sort #include <stdio.h> int main() { int array[100], n, c, d, position, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0; c < (n - 1); c++) { position = c; for (d = c + 1; d < n; d++) { if (array[position] > array[d]) position = d; } if (position != c) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } //Selection Sort printf("Sorted list in ascending order:n"); for (c = 0; c < n; c++) printf("%dn", array[c]); getchar(); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 22. Importance of Software Dr. Yousaf, PIEAS
  • 23. It is The END of the Course Dr. Yousaf, PIEAS