samroar04

Untitled

Feb 11th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. //This function traverses the array accordingly to the input guven by user
  5. void traverse(int i,int a[], int len) {
  6. printf("\nTotal number of elements are : %d",len);
  7. printf("\nElements are : ");
  8. for(i = 0 ; i < len ; i++) {
  9. printf("\t%d",a[i]);
  10. }
  11. }
  12.  
  13. //This function inserts element at a desired location in the array.
  14. void insert_ele(int i, int a[], int *len) {
  15. int ele,loc;
  16. printf("\nEnter element to be inserted :" );
  17. scanf("%d",&ele);
  18. printf("\nEnter loc");
  19. scanf("%d",&loc);
  20. if(i==19) printf("\Overflow");
  21. else {
  22. while(i>=loc) {
  23. a[i+1]=a[i];
  24. i--;
  25. }
  26. a[loc]=ele;
  27. (*len)++;
  28. printf("\nNew Array :");
  29. for(i = 0 ; i < *len ; i++) {
  30. printf("\t%d",a[i]);
  31. }
  32. }
  33. }
  34.  
  35. //This functions deletes an element from a desired location in the array.
  36. void delete_ele(int i, int a[], int *len) {
  37. int loc;
  38. printf("\nEnter location of element to be deleted");
  39. scanf("%d",&loc);
  40. if(*len==0) { printf("\nUnderflow"); }
  41. else {
  42. for(i = loc ; i < (*len-1) ; i++) {
  43. a[i]=a[i+1];
  44. }
  45. (*len)--;
  46. printf("\nNew Array :");
  47. for(i = 0 ; i < *len ; i++) {
  48. printf("\t%d",a[i]);
  49. }
  50. }
  51. }
  52.  
  53. //This function searches an element from the array.
  54. void find_ele(int i, int a[], int len) {
  55. int ele, count=0;
  56. printf("\nEnter element to be searched :");
  57. scanf("%d",&ele);
  58. for(i = 0 ; i < len ; i++) {
  59. if(a[i]==ele) { count++;}
  60. }
  61. if(count == 0) {
  62. printf("\nElement does not exist");
  63. }
  64. else {
  65. printf("\nElement found %d times",count);
  66. }
  67. }
  68.  
  69. //This function includes taking and printing user input and switch case to select from different option what user want to do.
  70. void main() {
  71. int a[20], i, len, ch;
  72. char choice;
  73. clrscr();
  74. printf("\nEnter number of elements you want to enter");
  75. scanf("%d",&len);
  76. printf("\nEnter the elements :");
  77. for(i = 0 ; i < len ; i++) {
  78. scanf("%d",&a[i]);
  79. }
  80. printf("\nElements inserted are :");
  81. for(i = 0 ; i < len ; i++) {
  82. printf("%d",a[i]);
  83. }
  84. i--;
  85. label:
  86. printf("\nPress 1 to insert, 2 to delete,3 to find any element");
  87. printf("\n or 4 to traverse the array");
  88. scanf("%d",&ch);
  89. switch(ch) {
  90. case 1:insert_ele(i, a, &len);
  91. break;
  92. case 2:delete_ele(i, a, &len);
  93. break;
  94. case 3:find_ele(i, a, len);
  95. break;
  96. case 4:traverse(i, a, len);
  97. break;
  98. default:printf("\nInvalid option");
  99. break;
  100. }
  101. printf("\nPress y to continue or any other key to quit :");
  102. scanf("%s",&choice);
  103. if(choice == 'y' || choice == 'Y') {
  104. goto label;
  105. }
  106. getch();
  107. }
  108.  
  109.  
  110. #include<stdio.h>
  111. #include<conio.h>
  112. int i,j,n,pivot,a[20];
  113. void quick(int a[], int left, int right);
  114. void swap(int a[], int i, int j);
  115. void main() {
  116. int n,a[20];
  117. textcolor(15);
  118. clrscr();
  119. printf("\n\nQUICK SORT");
  120. printf("\n\nEnter the limit : ");
  121. scanf("%d",&n);
  122. printf("\n\nEnter the element\n\n");
  123. for(i = 0 ; i < n ; i++) {
  124. scanf("%d",&a[i])
  125. }
  126. quick(a,0,n-1);
  127. printf("\n\nThe sorted list is \n\n");
  128. for(i = 0 ; i < n ; i++) {
  129. printf("%d ",a[i]);
  130. }
  131. getch();
  132. }
  133. void quick(int a[], int first, int last) {
  134. if(first<last) {
  135. pivot=a[first];
  136. i=first;
  137. j=last;
  138. while(i < j) {
  139. while(a[i] <= pivot && i < last)
  140. i++;
  141. while(a[j] >= pivot && j > first)
  142. j--;
  143. if(i<j){ swap(a,i,j); }
  144. }
  145. swap(a,first,j);
  146. quick(a,first,j-1);
  147. quick(a,j+1,last);
  148. }
  149. }
  150. void swap(int a[], int i, int j) {
  151. int temp;
  152. temp=a[i];
  153. a[i]=a[j];
  154. a[j]=temp;
  155. }
  156.  
  157.  
  158. #include<stdio.h>
  159. #include<conio.h>
  160. void bsort(int [], int);
  161. void main() {
  162. int a[20], n, i;
  163. clrscr();
  164. printf("Enter number of elements");
  165. scanf("%d",&n);
  166. printf("Enter the array");
  167. for(i = 0 ; i < n ; i++) {
  168. scanf("%d",&a[i]);
  169. }
  170. bsort(a,n);
  171. printf("sorted elements\n");
  172. for(i = 0 ; i < n ; i++) {
  173. printf("%d\n",a[i]);
  174. }
  175. getch();
  176. }
  177. void bsort(int a[],int n) {
  178. int temp, i=1, j, flag=0;
  179. while(i <= n-1 && flag == 0) {
  180. flag=1;
  181. for(j = 0 ; j <= n-i-1 ; j++) {
  182. if(a[j] > a[j+1]) {
  183. temp=a[j];
  184. a[j]=a[j+1];
  185. a[j+1]=temp;
  186. }
  187. flag=0;
  188. }
  189. i=i+1;
  190. }
  191. }
  192.  
  193.  
  194.  
  195. #include<stdio.h>
  196. #include<conio.h>
  197. #include<string.h>
  198. #define NULL 0
  199. struct queuenode {
  200. int info;
  201. struct queuenode *link;
  202. };
  203. typedef struct queuenode node;
  204. void enqueue(node** front, node** rear, int item) {
  205. node *new;
  206. new=(node*) malloc(sizeof(node));
  207. new->info=item;
  208. if(*front==NULL) *front=new; *rear=new;
  209. else { (*rear)->link=new;
  210. (*rear)=new;
  211. }
  212. new->link=NULL;
  213. }
  214. void dequeue(node** front) {
  215. node *save;
  216. if(*front==NULL) printf("undeflow: queue empty");
  217. else {
  218. printf("element deleted=%d",(*front)->info);
  219. save=*front;
  220. *front=save->link;
  221. free(save);
  222. }
  223. }
  224. void display(node *front) {
  225. node *ptr=front;
  226. printf("\n traversing queue:");
  227. while(ptr!=NULL) {
  228. printf("%d",ptr->info);
  229. ptr=ptr->link;
  230. }
  231. }
  232. void main() {
  233. node *front=NULL;
  234. node *rear=NULL;
  235. int choice,item;
  236. char ch;
  237. clrscr();
  238. printf("1:ENQUEUE\n2.DEQUEUE\n3.Display\n4.Another to exit");
  239. do {
  240. printf("\nEnter your choice:");
  241. scanf("%d",&choice);
  242. switch(choice) {
  243. case 1:
  244. printf("Enter value to be inserted:\n");
  245. scanf("%d",&item);
  246. enqueue(&front,&rear,item);
  247. break;
  248. case 2:
  249. dequeue(&front);
  250. break;
  251. case 3:
  252. display(front);
  253. break;
  254. default:
  255. printf("\nend of operation \n");
  256. }
  257. printf("want to continue:(y or Y)=");
  258. fflush(stdin);
  259. ch=getchar();
  260. } while((ch == 'y')||(ch == 'n'));
  261. printf("\n end of program");
  262. getch();
  263.  
  264.  
  265. #include<stdio.h>
  266. #include<conio.h>
  267. void traverse(int i, int a[], int len) {
  268. printf("\nTotal number of elements are : %d",len);
  269. printf("\nElements are : ");
  270. for(i=0; i<len; i++) {
  271. printf("\t%d",a[i]);
  272. }
  273. }
  274. void insert_ele(int i, int a[], int *len) {
  275. int ele,loc;
  276. printf("\nEnter element to be inserted :" );
  277. scanf("%d",&ele);
  278. printf("\nEnter loc");
  279. scanf("%d",&loc);
  280. if(i==19) printf("\Overflow");
  281. else {
  282. while(i>=loc) {
  283. a[i+1]=a[i];
  284. i--;
  285. }
  286. a[loc]=ele;
  287. (*len)++;
  288. printf("\nNew Array :");
  289. for(i = 0 ; i < *len ; i++) {
  290. printf("\t%d",a[i]);
  291. }
  292. }
  293. }
  294. void delete_ele(int i, int a[], int *len) {
  295. int loc;
  296. printf("\nEnter location of element to be deleted");
  297. scanf("%d",&loc);
  298. if(*len==0) printf("\nUnderflow");
  299. else {
  300. for(i = loc ; i < (*len-1) ; i++) {
  301. a[i]=a[i+1];
  302. }
  303. (*len)--;
  304. printf("\nNew Array :");
  305. for(i = 0 ; i < *len ; i++) {
  306. printf("\t%d",a[i]);
  307. }
  308. }
  309. }
  310. void find_ele(int i, int a[], int len) {
  311. int ele, count=0;
  312. printf("\nEnter element to be searched :");
  313. scanf("%d",&ele);
  314. for(i = 0 ; i < len ; i++) {
  315. if(a[i]==ele) count++;
  316. }
  317. if(count == 0) {
  318. printf("\nElement does not exist");
  319. }
  320. else {
  321. printf("\nElement found %d times",count);
  322. }
  323. }
  324. void main() {
  325. int a[20], i, len, ch;
  326. char choice;
  327. clrscr();
  328. printf("\nEnter number of elements you want to enter");
  329. scanf("%d",&len);
  330. printf("\nEnter the elements :");
  331. for(i = 0 ; i < len ; i++) {
  332. scanf("%d",&a[i]);
  333. }
  334. printf("\nElements inserted are :");
  335. for(i = 0 ; i < len ; i++) {
  336. printf("%d",a[i]);
  337. }
  338. i--;
  339. label:
  340. printf("\nPress 1 to insert, 2 to delete,3 to find any element");
  341. printf("\n or 4 to traverse the array");
  342. scanf("%d",&ch);
  343. switch(ch) {
  344. case 1:insert_ele(i, a, &len);
  345. break;
  346. case 2:delete_ele(i, a, &len);
  347. break;
  348. case 3:find_ele(i, a, len);
  349. break;
  350. case 4:traverse(i, a, len);
  351. break;
  352. default:printf("\nInvalid option");
  353. break;
  354. }
  355. printf("\nPress y to continue or any other key to quit :");
  356. scanf("%s",&choice);
  357. if(choice == 'y' || choice =='Y') {
  358. goto label;
  359. }
  360. getch();
  361. }
  362.  
  363.  
  364.  
  365.  
  366. #include<stdio.h>
  367. #include<conio.h>
  368. #define max 5
  369. int top=-1;
  370. int stack[max];
  371.  
  372. //This function push an element into the stack.
  373. void push() {
  374. int item;
  375. if(top == (max-1)) printf("\n stack is full oro verflow \n");
  376. else {
  377. printf("\nEnter the item:");
  378. scanf("%d",&item);
  379. stack[top]=item;
  380. top=top+1;
  381. }
  382. }
  383.  
  384. //This function pop an element from the stack
  385. void pop() {
  386. int temp;
  387. if(top==-1) { printf("\n stack is empty or underflow \n"); }
  388. else {
  389. temp=stack[top];
  390. printf("item deleted is %d",temp);
  391. top=top-1;
  392. }
  393. }
  394.  
  395. //This function is used to desipla the current elements in stack
  396. void display() {
  397. int i;
  398. if(top==-1) {
  399. printf("\nstack is empty\n");
  400. }
  401. else {
  402. printf("Elements of stack are:\n");
  403. for(i = -1 ; i <= top-1 ; i++) {
  404. printf("%d\n",stack[i]);
  405. }
  406. }
  407. }
  408.  
  409. //This function contains the switch statement to choose from different options
  410. void main() {
  411. int choice;
  412. clrscr();
  413. while(1) {
  414. printf("1:Push\n 2:Pop\n 3:Display\n 4:Exit");
  415. printf("\nEnter your choice 1-4:");
  416. scanf("%d",&choice);
  417. switch(choice) {
  418. case 1:push();
  419. break;
  420. case 2:pop();
  421. break;
  422. case 3:display();
  423. break;
  424. case 4:exit(0);
  425. }
  426. }
  427. getch();
  428. }
  429. #include<stdio.h>
  430. #include<conio.h>
  431.  
  432. void merge_split(int a[], int first, int last);
  433. void merge(int a[], int f1, int l1, int f2, int l2);
  434. int a[25],b[25];
  435.  
  436. void main() {
  437. int i,n;
  438. clrscr();
  439. printf("\n\nMERGE SORT");
  440. printf("\n\nEnter the limit : ");
  441. scanf("%d",&n);
  442. printf("\nEnter the elements\n");
  443. for(i = 0 ; i < n ; i++) {
  444. scanf("%d",&a[i]);
  445. }
  446. merge_split(a,0,n-1);
  447. printf("\n\nSorted list : ");
  448. for(i = 0 ; i < n ; i++) {
  449. printf("\n %d",a[i]);
  450. }
  451. getch();
  452. }
  453.  
  454. void merge_split(int a[], int first, int last) {
  455. int mid;
  456. if(first<last) {
  457. mid=(first+last)/2;
  458. merge_split(a,first,mid);
  459. merge_split(a,mid+1,last);
  460. merge(a,first,mid,mid+1,last);
  461. }
  462. }
  463.  
  464. void merge(int a[], int f1, int l1, int f2, int l2) {
  465. int i,j,k=0;
  466. i=f1;
  467. j=f2;
  468. while(i <= l1 && j <= l2) {
  469. if(a[i]<a[j])
  470. b[k]=a[i++];
  471. else
  472. b[k]=a[j++];
  473. k++;
  474. }
  475. while(i<=l1)
  476. b[k++]=a[i++];
  477. while(j<=l2)
  478. b[k++]=a[j++];
  479. i=f1;
  480. j=0;
  481. while(i<=l2 && j<k)
  482. a[i++]=b[j++];
  483. }
  484.  
  485.  
  486.  
  487.  
  488. #include<stdio.h>
  489. #include<conio.h>
  490. void main() {
  491. int a[10], i, n, m, c=0;
  492. clrscr();
  493. printf("Enter the size of an array");
  494. scanf("%d",&n);
  495. printf("\nEnter the elements of the array");
  496. for(i = 0; i <=n-1 ; i++) {
  497. scanf("%d",&a[i]);
  498. }
  499. printf("\nThe elements of an array are");
  500. for(i = 0 ; i <= n-1 ; i++) {
  501. printf(" %d",a[i]);
  502. }
  503. printf("\nEnter the number to be search");
  504. scanf("%d",&m);
  505. for(i = 0 ; i <= n-1 ; i++) {
  506. if(a[i]==m) {
  507. c=1;
  508. break;
  509. }
  510. }
  511. if(c==0) printf("\nThe number is not in the list");
  512. else{ printf("\nThe number is found"); }
  513. getch();
  514. }
  515.  
  516.  
  517. #include<stdio.h>
  518. #include<conio.h>
  519. void main() {
  520. int a[10], i, n, m, c=0;
  521. clrscr();
  522. printf("Enter the size of an array");
  523. scanf("%d",&n);
  524. printf("\nEnter the elements of the array");
  525. for(i = 0; i <=n-1 ; i++) {
  526. scanf("%d",&a[i]);
  527. }
  528. printf("\nThe elements of an array are");
  529. for(i = 0 ; i <= n-1 ; i++) {
  530. printf(" %d",a[i]);
  531. }
  532. printf("\nEnter the number to be search");
  533. scanf("%d",&m);
  534. for(i = 0 ; i <= n-1 ; i++) {
  535. if(a[i]==m) {
  536. c=1;
  537. break;
  538. }
  539. }
  540. if(c==0) printf("\nThe number is not in the list");
  541. else{ printf("\nThe number is found"); }
  542. getch();
  543. }
  544.  
  545.  
  546.  
  547. #include<stdio.h>
  548. #include<conio.h>
  549. void main() {
  550. int n, a[30], item, i, j, mid, top, bottom;
  551. clrscr();
  552. printf("Enter how many elements you want:\n");
  553. scanf("%d", &n);
  554. printf("Enter the %d elements in ascending order\n", n);
  555. for (i = 0 ; i < n ; i++) {
  556. scanf("%d", &a[i]);
  557. }
  558. printf("\nEnter the item to search\n");
  559. scanf("%d", &item);
  560. bottom = 1;
  561. top = n;
  562. do {
  563. mid = (bottom + top) / 2;
  564. if (item < a[mid])
  565. top = mid - 1;
  566. else if (item > a[mid])
  567. bottom = mid + 1;
  568. } while (item != a[mid] && bottom <= top);
  569.  
  570. if (item == a[mid]) { printf("Binary search successfull!!\n"); printf("\n %d found in position: %d\n", item, mid + 1) }
  571. else {
  572. printf("\n Search failed\n %d not found\n", item);
  573. }
  574. getch();
  575. }
  576. #include<stdio.h>
  577. #include<conio.h>
  578. void main() {
  579. int n, a[30], item, i, j, mid, top, bottom;
  580. clrscr();
  581. printf("Enter how many elements you want:\n");
  582. scanf("%d", &n);
  583. printf("Enter the %d elements in ascending order\n", n);
  584. for (i = 0 ; i < n ; i++) {
  585. scanf("%d", &a[i]);
  586. }
  587. printf("\nEnter the item to search\n");
  588. scanf("%d", &item);
  589. bottom = 1;
  590. top = n;
  591. do {
  592. mid = (bottom + top) / 2;
  593. if (item < a[mid])
  594. top = mid - 1;
  595. else if (item > a[mid])
  596. bottom = mid + 1;
  597. } while (item != a[mid] && bottom <= top);
  598.  
  599. if (item == a[mid]) { printf("Binary search successfull!!\n"); printf("\n %d found in position: %d\n", item, mid + 1) }
  600. else {
  601. printf("\n Search failed\n %d not found\n", item);
  602. }
  603. getch();
  604. }
Advertisement
Add Comment
Please, Sign In to add comment