c programming array & shorting

26

Upload: argusacademy

Post on 13-Jul-2015

175 views

Category:

Education


0 download

TRANSCRIPT

Page 1: C  programming array & shorting
Page 2: C  programming array & shorting

NOTES

Page 3: C  programming array & shorting

NOTES

Page 4: C  programming array & shorting

NOTES

Page 5: C  programming array & shorting

NOTES

Page 6: C  programming array & shorting

#include<stdio.h>

#include<conio.h>

void main()

{

int arr[10], i;

clrscr();

printf(“Enter Array Element\n”);

for(i=0;i<10;i++)

{

scanf(“%d”, &arr[i]);

}

printf(“Array Element\n”);

for(i=0;i<10;i++)

{

printf(“%d\n”,arr[i]);

}

getch();

}

EXTRA

Page 7: C  programming array & shorting

EXTRA

Page 8: C  programming array & shorting

EXTRA

Page 9: C  programming array & shorting

NOTES

Page 10: C  programming array & shorting

EXTRA

Page 11: C  programming array & shorting

EXTRA

Page 12: C  programming array & shorting

#include<stdio.h>#include<conio.h>void main(){

int arr[100], s, i , j, temp; clrscr();printf(“Enter Array Size\n”);scanf(“%d”,&s);printf(“Enter Array Element\n”);for(i=0;i<s;i++){

scanf(“%d”, &arr[i]);}printf(“Array Element befor sorting\n”);for(i=0;i<s;i++){

printf(“%d\n”, arr[i]);}

EXTRA

Page 13: C  programming array & shorting

for(i=0;i<s-1;i++){

for(j=0;j<s-1-i;j++){

if(arr[j]>arr[j+1]){

temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;

}}

}printf(“Data after sorting \n”);for(i=0;i<s;i++){

printf(“%d\n”, arr[i]);}getch();}

EXTRA

Page 14: C  programming array & shorting

EXTRA

Page 15: C  programming array & shorting

#include<stdio.h>#include<conio.h>void main(){

int arr[100], s, i , j, temp; clrscr();printf(“Enter Array Size\n”);sxcanf(“%d”,&s);printf(“Enter Array Element\n”);for(i=0;i<s;i++){

scanf(“%d”, &arr[i]);}printf(“Array Element befor sorting\n”);for(i=0;i<s;i++){

printf(“%d\n”, arr[i]);}

EXTRA

Page 16: C  programming array & shorting

for(i=0;i<s-1;i++){

for(j=i+1;j<s;j++){

if(arr[i]>arr[j]){

temp=arr[i];arr[i]=arr[j];arr[j]=temp;

}}

}printf(“Data after sorting\n”);for(i=0;i<s;i++){

printf(“%d\n”,arr[i]);}getch();}

EXTRA

Page 17: C  programming array & shorting

EXTRA

Page 18: C  programming array & shorting

EXTRA

Page 19: C  programming array & shorting

#include<stdio.h>#include<conio.h>void main(){

int arr[10],s, p, temp,i,k;clrscr();printf("enter Array Size\n");scanf("%d",&s);printf("Enter Array Element\n");for(i=0;i<s;i++){

scanf("%d",&arr[i]);}printf("Array Element\n");for(i=0;i<s;i++){

printf("%d\n",arr[i]);}

EXTRA

Page 20: C  programming array & shorting

for(i=1;i<s;i++){

p=i-1;temp=arr[i];while(temp<arr[p]&&p>=0){

arr[p+1]=arr[p];p--;

}arr[p+1]=temp;

}printf("Data after sorting\n");for(i=0;i<s;i++){

printf("%d\n",arr[i]);}getch();

}

EXTRA

Page 21: C  programming array & shorting

Searching

Linear Search Binary Search

Linear Search- This is the simplest technique to find

out an element in an unsorted list. In this technique the value of the key is compared with the first element of the list, if match is found then the search is terminated. Otherwise next element from the list is fetched and compared with the key and this process is continued till the key is found or list is completely.

EXTRA

Page 22: C  programming array & shorting

Binary Search- Binary search works for sorted lists and is avery efficient searching technique. It is used to find thelocation of a given element or record in a list. Otherinformation associated with the element can also befetched if required. To find the location of a file in thecomputer directory one can use this searching technique.Let low represents lower limit of the list l, high upper andmid is average of low and high that is middle of the lowerand upper limits.Mid = (low+high)/2;we compare the middle element with key to be searched. Ifthe value of middle element is greater than key ,then keywill exit in lower half of the list, take high = mid-1 and findnew value of mid.

EXTRA

Page 23: C  programming array & shorting

#include<stdio.h>#include<conio.h>void main(){

int arr[100],s, i, first=0, mid=0, last =0, count =0, num;clrscr();printf(“Enter Array Size\n”);scanf(“%d”,&s);printf(“Enter Array Element\n”);for(i=0;i<s;i++){

scanf(“%d”,&arr[i]);}printf(“Array Element\n”);for(i=0;i<s;i++){

printf(“%d\n”,arr[i]);}

EXTRA

Page 24: C  programming array & shorting

printf(“Enter the no want to search \n”);scanf(“%d”,&num);last = s-1;while((first<=last)&&(count==0)){

mid=(first+last)/2;if(arr[i]==num){

count=mid;}else if(arr[i]<num){

first=mid+1;}else {

last=mid-1;}

}

EXTRA

Page 25: C  programming array & shorting

if(count>0){

printf(“No found”);}else{

printf(“No not found”);}getch();}

EXTRA

Page 26: C  programming array & shorting

Double Dimension Array#include<stdio.h>

#include<conio.h>

void main()

{

int arr[3][3], i, j;

clrscr();

printf(“Enter Array Element\n”);

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

scanf(“%d”,&arr[i][j]);

}

}

printf(“Matrix \n\n”);

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf(“%d ”,arr[i][j]);

}

printf(“\n”);

}

getch();

}

EXTRA