rabbinur

Practice session (Array)

Aug 7th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.44 KB | None | 0 0
  1. (1)
  2.  
  3. Linear Search
  4. #include<iostream.h>
  5. #include<conio.h>
  6.  
  7.  
  8. int LinearSearch(int a[],int n,int data);
  9. void main()
  10. {
  11. clrscr();
  12. int a[50],i,n,d,flag;
  13. cout<<"Enter the number of elements ";
  14. cin>>n;
  15. cout<<"Enter the elements";
  16. for(i=0;i<n;i++)
  17.     {
  18.     cin>>a[i];
  19.     }
  20. cout<<"Enter the element to be searched";
  21. cin>>d;
  22. flag=LinearSearch(a,n,d);
  23. if(flag==-1)
  24.     cout<<"Element Not Found";
  25. else
  26.     cout<<"Element found at location"<<flag+1;
  27. getch();
  28. }
  29. int LinearSearch(int a[],int n,int d)
  30. {
  31. int i;
  32. for(i=0;i<n;i++)
  33.     {
  34.     if(a[i]==d)
  35.         return i;
  36.     }
  37.     return -1;
  38. }
  39.  
  40. (2)
  41.  
  42. Binary Search
  43. #include<iostream.h>
  44. #include<conio.h>
  45.  
  46.  
  47. int BinarySearch(int a[],int n,int d)
  48. {
  49. int b=0,e=n-1,m;
  50. while(b<=e)
  51.     {
  52.     m=(b+e)/2;
  53.     if(a[m]==d)
  54.         return m;
  55.     else
  56.         if(a[m]<d)
  57.             b=m+1;
  58.         else
  59.             e=m-1;
  60.     }
  61.     return 0;
  62. }
  63. void main()
  64. {
  65. clrscr();
  66. int a[50],i,n,d,flag;
  67. cout<<"Enter the number of elements ";
  68. cin>>n;
  69. cout<<"Enter the elements";
  70. for(i=0;i<n;i++)
  71.     {
  72.     cin>>a[i];
  73.     }
  74. cout<<"Enter the element to be searched";
  75. cin>>d;
  76. flag=BinarySearch(a,n,d);
  77. if(flag==0)
  78.     cout<<"Element Not Found";
  79. else
  80.     cout<<"Element found at location"<<flag+1;
  81. getch();
  82. }
  83.  
  84. (3)
  85.  
  86. Insertion Based on Location
  87. #include<iostream.h>
  88. #include<conio.h>
  89.  
  90.  
  91. void insert(int a[],int n,int d,int pos)
  92. {
  93.     int i;
  94.     for(i=n-1;i>=pos-1;i--)
  95.         {
  96.         a[i+1]=a[i];
  97.         }
  98.     a[pos-1]=d;
  99. }
  100. void main()
  101. {
  102. clrscr();
  103. int a[50],i,n,d,pos;
  104. cout<<"Enter the number of elements ";
  105. cin>>n;
  106. cout<<"Enter the elements";
  107. for(i=0;i<n;i++)
  108.     {
  109.     cin>>a[i];
  110.     }
  111. cout<<"Enter the element to be inserted and its location";
  112. cin>>d>>pos;
  113. insert(a,n,d,pos);
  114. cout<<"After Insertion";
  115. for(i=0;i<=n;i++)
  116.     {
  117.     cout<<a[i]<<",";
  118.     }
  119. getch();
  120. }
  121.  
  122. (4)
  123.  
  124. Insertion Based on value
  125. #include<iostream.h>
  126. #include<conio.h>
  127.  
  128.  
  129. void insert(int a[],int n,int d)
  130. {
  131.     int i,pos=0;
  132.     if(d>=a[n-1])
  133.         a[n]=d;
  134.     else
  135.         {
  136.         while(a[pos]<=d)
  137.             pos++;
  138.         for(i=n-1;i>=pos-1;i--)
  139.             {
  140.             a[i+1]=a[i];
  141.             }
  142.         a[pos]=d;
  143.         }
  144. }
  145. void main()
  146. {
  147. clrscr();
  148. int a[50],i,n,d;
  149. cout<<"Enter the number of elements ";
  150. cin>>n;
  151. cout<<"Enter the elements";
  152. for(i=0;i<n;i++)
  153.     {
  154.     cin>>a[i];
  155.     }
  156. cout<<"Enter the element to be inserted";
  157. cin>>d;
  158. insert(a,n,d);
  159. cout<<"After Insertion";
  160. for(i=0;i<=n;i++)
  161.     {
  162.     cout<<a[i]<<",";
  163.     }
  164. getch();
  165. }
  166.  
  167. (5)
  168.  
  169. Deletion Based on Value
  170. #include<iostream.h>
  171. #include<conio.h>
  172.  
  173.  
  174. int del(int a[],int n,int data)
  175. {
  176.     int i,j;
  177.     for(i=0;i<n;i++)
  178.         {
  179.         if(a[i]==data)
  180.             {
  181.             for(j=i+1;j<n;j++)
  182.                 a[j-1]=a[j];
  183.             return 1;
  184.             }
  185.         }
  186.     return -1;
  187. }
  188. void main()
  189. {
  190. clrscr();
  191. int a[50],i,n,element,f;
  192. cout<<"Enter the number of elements ";
  193. cin>>n;
  194. cout<<"Enter the elements";
  195. for(i=0;i<n;i++)
  196.     {
  197.     cin>>a[i];
  198.     }
  199. cout<<"Enter the elementto be deleted";
  200. cin>>element;
  201. f=del(a,n,element);
  202. if(f==1)
  203. {
  204. cout<<"After Deletion";
  205. for(i=0;i<n-1;i++)
  206.     {
  207.     cout<<a[i]<<",";
  208.     }
  209. }
  210. else
  211. cout<<"Element Not Found";
  212. getch();
  213. }
  214.  
  215. (6)
  216.  
  217. Deletion based on position
  218. #include<iostream.h>
  219. #include<conio.h>
  220.  
  221.  
  222. void del(int a[],int n,int pos)
  223. {
  224.     int i;
  225.     for(i=pos;i<n;i++)
  226.         {
  227.         a[i-1]=a[i];
  228.         }
  229. }
  230. void main()
  231. {
  232. clrscr();
  233. int a[50],i,n,pos;
  234. cout<<"Enter the number of elements ";
  235. cin>>n;
  236. cout<<"Enter the elements";
  237. for(i=0;i<n;i++)
  238.     {
  239.     cin>>a[i];
  240.     }
  241. cout<<"Enter the postion of deletion";
  242. cin>>pos;
  243. del(a,n,pos);
  244. cout<<"After Deletion";
  245. for(i=0;i<n-1;i++)
  246.     {
  247.     cout<<a[i]<<",";
  248.     }
  249. getch();
  250. }
  251.  
  252. (7)
  253.  
  254. Selection Sorting
  255. #include<iostream.h>
  256. #include<conio.h>
  257.  
  258.  
  259. void SelectionSort(int a[],int n)
  260. {
  261.     int min,pos,i,j;
  262.     for(i=0;i<=n-1;i++)
  263.         {
  264.             min=a[i];
  265.             pos=i;
  266.             for(j=i+1;j<n;j++)
  267.                 {
  268.                 if(a[j]<min)
  269.                 {
  270.                     min=a[j];
  271.                     pos=j;
  272.                 }
  273.                 }
  274.             int temp;
  275.             temp=a[i];
  276.             a[i]=a[pos];
  277.             a[pos]=temp;
  278.         }
  279. }
  280. void main()
  281. {
  282. clrscr();
  283. int a[50],i,n,d,pos;
  284. cout<<"Enter the number of elements ";
  285. cin>>n;
  286. cout<<"Enter the elements";
  287. for(i=0;i<n;i++)
  288.     {
  289.     cin>>a[i];
  290.     }
  291. SelectionSort(a,n);
  292. cout<<"Ascending Order";
  293. for(i=0;i<n;i++)
  294.     {
  295.     cout<<a[i]<<",";
  296.     }
  297. getch();
  298. }
  299.  
  300. (8)
  301.  
  302. Bubble Sort
  303. #include<iostream.h>
  304. #include<conio.h>
  305.  
  306.  
  307. void BubbleSort(int a[],int n)
  308. {
  309.     int i,j,temp;
  310.     for(i=0;i<=n;i++)
  311.         {
  312.             for(j=0;j<(n-1)-i;j++)
  313.                 {
  314.                 if(a[j]>a[j+1])
  315.                 {
  316.                     temp=a[j];
  317.                     a[j]=a[j+1];
  318.                     a[j+1]=temp;
  319.                 }
  320.                 }
  321.         }
  322. }
  323. void main()
  324. {
  325. clrscr();
  326. int a[50],i,n,d,pos;
  327. cout<<"Enter the number of elements ";
  328. cin>>n;
  329. cout<<"Enter the elements";
  330. for(i=0;i<n;i++)
  331.     {
  332.     cin>>a[i];
  333.     }
  334. BubbleSort(a,n);
  335. cout<<"Ascending Order";
  336. for(i=0;i<n;i++)
  337.     {
  338.     cout<<a[i]<<",";
  339.     }
  340. getch();
  341. }
  342.  
  343. (9)
  344.  
  345. Insertion Sorting
  346. #include<iostream.h>
  347. #include<conio.h>
  348.  
  349.  
  350. void InsertionSort(int a[],int n)
  351. {
  352.     int i,j,t;
  353.     for(i=1;i<n;i++)
  354.         {
  355.             t=a[i];
  356.             j=i-1;
  357.             while(t<a[j]&&j>=0)
  358.                 {
  359.                 a[j+1]=a[j];
  360.                 j--;
  361.                 }
  362.             a[j+1]=t;
  363.         }
  364. }
  365. void main()
  366. {
  367. clrscr();
  368. int a[50],i,n,d,pos;
  369. cout<<"Enter the number of elements ";
  370. cin>>n;
  371. cout<<"Enter the elements";
  372. for(i=0;i<n;i++)
  373.     {
  374.     cin>>a[i];
  375.     }
  376. InsertionSort(a,n);
  377. cout<<"Ascending Order";
  378. for(i=0;i<n;i++)
  379.     {
  380.     cout<<a[i]<<",";
  381.     }
  382. getch();
  383. }
  384.  
  385. (10)
  386.  
  387. Merging
  388. #include<iostream.h>
  389. #include<conio.h>
  390.  
  391.  
  392. void Merge(int a[],int m,int b[],int n,int c[])
  393. {
  394.     int p1,p2,p3;
  395.     p1=p2=p3=0;
  396.     while(p1<m&&p2<n)
  397.     {
  398.         if(a[p1]>b[p2])
  399.             c[p3++]=b[p2++];
  400.         else
  401.             c[p3++]=a[p1++];
  402.     }
  403.     while(p1<m)
  404.         c[p3++]=a[p1++];
  405.     while(p2<n)
  406.         c[p3++]=b[p2++];
  407. }
  408. void main()
  409. {
  410. clrscr();
  411. int a[50],b[50],c[100],i,n,m;
  412. cout<<"Enter the number of elements for two arrays";
  413. cin>>m>>n;
  414. cout<<"Enter the elements in 1st array";
  415. for(i=0;i<m;i++)
  416.     {
  417.     cin>>a[i];
  418.     }
  419. cout<<"Enter the elements in 2nd array";
  420. for(i=0;i<n;i++)
  421.     {
  422.     cin>>b[i];
  423.     }
  424. Merge(a,m,b,n,c);
  425. cout<<"Element in new array are";
  426. for(i=0;i<(m+n);i++)
  427.     {
  428.     cout<<c[i]<<",";
  429.     }
  430. getch();
  431. }
  432.  
  433. (11)
  434.  
  435. Concatenation
  436. #include<iostream.h>
  437. #include<conio.h>
  438.  
  439.  
  440. void Concatenation(int a[],int m,int b[],int n,int c[])
  441. {
  442.     int p=0,i=0,j=0;
  443.     while(i<m)
  444.         c[p++]=a[i++];
  445.     while(j<n)
  446.         c[p++]=b[j++];
  447. }
  448. void main()
  449. {
  450. clrscr();
  451. int a[50],b[50],c[100],i,n,m;
  452. cout<<"Enter the number of elements for two arrays";
  453. cin>>m>>n;
  454. cout<<"Enter the elements in 1st array";
  455. for(i=0;i<m;i++)
  456.     {
  457.     cin>>a[i];
  458.     }
  459. cout<<"Enter the elements in 2nd array";
  460. for(i=0;i<n;i++)
  461.     {
  462.     cin>>b[i];
  463.     }
  464. Concatenation(a,m,b,n,c);
  465. cout<<"Element in new array are";
  466. for(i=0;i<(m+n);i++)
  467.     {
  468.     cout<<c[i]<<",";
  469.     }
  470. getch();
  471. }
  472.  
  473. (12)
  474.  
  475. Program to add n integer numbers
  476. #include<iostream.h>
  477. #include<conio.h>
  478. void main()
  479. {
  480. clrscr();
  481. int sum=0,n;
  482. cout<<"Enter how many no.";
  483. cin>>n;
  484. for(int x=1;x<=n;x++)
  485.     {
  486.         sum=sum+x;
  487.     }
  488. cout<<"TOTAL is "<<sum;
  489. getch();
  490. }
  491.  
  492. (13)
  493.  
  494. Program to print pyramid
  495. /*1
  496.   12
  497.   123
  498.   1234 */
  499. #include<iostream.h>
  500. #include<conio.h>
  501. void main()
  502. {
  503. clrscr();
  504. int n;
  505. cout<<"enter how many rows";
  506. cin>>n;
  507. for(int i=1;i<=n;i++)
  508.     {
  509.         for(int j=1;j<=i;j++)
  510.             cout<<j;
  511.         cout<<endl;
  512.     }
  513.  
  514. getch();
  515. }
  516.  
  517. (14)
  518.  
  519. Program to find maximum in an array
  520. #include<iostream.h>
  521. #include<conio.h>
  522. int maximum(int n,int w[]);
  523. void main()
  524. {
  525.     clrscr();
  526.     int b[10],n,max;
  527.     cout<<"ENTER HOW MANY ELEMENTS ";
  528.     cin>>n;
  529.     cout<<"ENTER VALUES ";
  530.     for(int i=0;i<n;i++)
  531.         cin>>b[i];
  532.     max=maximum(n,b);
  533.     cout<<"MAXIMUM IS "<<max;
  534.     getch();
  535. }
  536. int maximum(int n,int w[])
  537. {
  538.     int max=w[0];
  539.     for(int i=0;i<n-1;i++)
  540.         if(w[i+1]>max)
  541.             max=w[i+1];
  542.     return max;
  543. }
  544.  
  545. (15)
  546.  
  547. Program to find SUM of an array using pointer
  548. #include<iostream.h>
  549. #include<conio.h>
  550. void main()
  551. {
  552.     clrscr();
  553.     int *p,sum=0;
  554.     int x[5]={4,10,7,5,12};
  555.     p=x;
  556.     for(int i=0;i<5;i++)
  557.     {
  558.         sum = sum + *p;
  559.         p++;
  560.     }
  561.     cout<<sum;
  562.     getch();
  563. }
  564.  
  565. (16)
  566.  
  567. Program for Bubble Sorting through pointers
  568. #include<iostream.h>
  569. #include<conio.h>
  570. void main()
  571. {
  572. clrscr();
  573. short int x[5]={10,20,5,3,123};
  574. short int *p,*q,temp;
  575. for(int i=0;i<5;i++)
  576. {
  577. p=x;
  578. q=x+1;
  579. for(int j=0;j<4-i;j++)
  580. {
  581. if((*p)>(*q))
  582. {
  583. temp=*p;
  584. *p=*q;
  585. *q=temp;
  586. }
  587. p++;
  588. q++;
  589. }
  590. }
  591. for(i=0;i<5;i++)
  592. cout<<x[i]<<endl;
  593. getch();
  594. }
  595.  
  596. (17)
  597.  
  598. Program to print array values through pointer
  599. #include<iostream>h>
  600. #include<conio>h>
  601. #include<dos>h> /*for sleep function*/
  602. void main()
  603. {
  604. clrscr();
  605. short int array[5]={10,3,96,32767,45},*p;
  606. p=array;
  607. for(int i=0;i<5;i++)
  608. {
  609.     gotoxy(40,10+i);    /*location of answer*/
  610.     sleep(1);  /* one second break*/
  611.     cout<<*p<<endl;
  612.     p++;
  613. }
  614. getch();
  615. }
  616.  
  617. (18)
  618.  
  619. Program to sort strings using bubble sort method
  620. #include<iostream.h>
  621. #include<conio.h>
  622. #include<string.h>
  623. void main()
  624. {
  625. char x[5][10]={"harish","Harish","HARISH","hARISH","HaRIsh"};
  626. cout<<"Before Sorting ->";
  627. for(int i=0;i<5;i++)
  628.   {    gotoxy(20,1+i);
  629.     cout<<x[i];    }
  630. char b[10];
  631. for( i=1;i<5;i++)
  632.         {
  633.             for(int j=0;j<5-i;j++)
  634.                 {
  635.                     if((strcmp(x[j],x[j+1]))>0)
  636.                         {
  637.                             strcpy(b,x[j+1]);
  638.                             strcpy(x[j+1],x[j]);
  639.                             strcpy(x[j],b);
  640.                         }
  641.                 }
  642.  
  643.         }
  644. gotoxy(40,1);
  645. cout<<"After Sorting ->";
  646. for( i=0;i<5;i++)     /*capital letters ascii code comes before
  647.                                 small letters ascii code */
  648.   {    gotoxy(60,1+i);
  649.     cout<<x[i];    }
  650. getch();
  651. }
  652.  
  653. (19)
  654.  
  655. Quick Sorting
  656. #include<iostream.h>
  657. #include<conio.h>
  658. #include<stdlib.h>
  659. #define size 10
  660. void quick(int a[size],int,int);
  661. int partition(int a[size],int,int);
  662. void swap(int a[size],int *,int *);
  663. int n;
  664. int main()
  665. {
  666.     int i,a[size];
  667.     clrscr();
  668.     cout<<"Quick Sort Method";
  669.     cout<<"Enter total numbers to sort";
  670.     cin>>n;
  671.     cout<<"Enter the elements";
  672.     for(i=0;i<n;i++)
  673.     {
  674.         cin>>a[i]    ;
  675.     }
  676.     quick(a,0,n-1);
  677.     cout<<"Sorted Array";
  678.     for(i=0;i<n;i++)
  679.     {
  680.         cout<<"\t"<<a[i];
  681.     }
  682.     getch();
  683.     return 0;
  684. }
  685.  
  686. //This function is to sort the elements in a sublist
  687.  
  688. void quick(int a[size],int low,int high)
  689. {
  690.     int m,i;
  691.     if(low<high)
  692.     {
  693.         m=partition(a,low,high);
  694.         quick(a,low,m-1);
  695.         quick(a,m+1,high);
  696.     }
  697. }
  698.  
  699. //This function is to partition a list and decide the pivot element
  700.  
  701. int partition(int a[size],int low,int high)
  702. {
  703.     int pivot=a[low],i=low,j=high;
  704.     while(i<=j)
  705.     {
  706.         while(a[i]<=pivot)
  707.             i++;
  708.         while(a[j]>pivot)
  709.             j--;
  710.         if(i<j)
  711.             swap(a,&i,&j);
  712.     }
  713.     swap(a,&low,&j);
  714.     return j;
  715. }
  716.  
  717. void swap(int a[size],int *i,int *j)
  718. {
  719.     int temp;
  720.     temp=a[*i];
  721.     a[*i]=a[*j];
  722.     a[*j]=temp;
  723. }
  724.  
  725. (20)
  726.  
  727. Merge Sort
  728. #include<iostream.h>
  729. #include<conio.h>
  730. #include<stdlib.h>
  731. int n;
  732. void main()
  733. {
  734.     int i,low,high;
  735.     int a[5];
  736.     void mergesort(int a[5],int low,int high);
  737.     void display(int a[5]);
  738.     clrscr();
  739.     cout<<"Merge Sort\n";
  740.     cout<<"Enter the length of list";
  741.     cin>>n;
  742.     cout<<"Enter list Elements";
  743.     for(i=0;i<n;i++)
  744.     {
  745.         cin>>a[i];
  746.     }
  747.     low=0;
  748.     high=n-1;
  749.     mergesort(a,low,high);
  750.     display(a);
  751.     getch();
  752. }
  753.  
  754. //This function is to split the list into sublists
  755.  
  756. void mergesort(int a[5],int low,int high)
  757. {
  758.     int mid;
  759.     void combine(int a[5],int low,int mid,int high);
  760.     if(low<high)
  761.     {
  762.         mid=(low+high)/2;//split the list at mid
  763.         mergesort(a,low,mid);//first sublist
  764.         mergesort(a,mid+1,high);
  765.         combine(a,low,mid,high);
  766.     }
  767. }
  768.  
  769. //This function is to for merging the two sublists
  770.  
  771. void combine(int a[5],int low,int mid,int high)
  772. {
  773.     int i,j,k;
  774.     int temp[5];
  775.     k=low;
  776.     i=low;
  777.     j=mid+1;
  778.     while(i<=mid && j<=high)
  779.     {
  780.         if(a[i]<=a[j])
  781.         {
  782.             temp[k]=a[i];
  783.             i++;
  784.             k++;
  785.         }
  786.         else
  787.         {
  788.             temp[k]=a[j];
  789.             j++;
  790.             k++;
  791.         }
  792.     }
  793.     while(i<=mid)
  794.     {
  795.         temp[k]=a[i];
  796.         i++;
  797.         k++;
  798.     }
  799.     while(j<=high)
  800.     {
  801.         temp[k]=a[j];
  802.         j++;
  803.         k++;
  804.     }
  805. //copy the elements from temp array to a
  806.     for(k=low;k<=high;k++)
  807.     {
  808.         a[k]=temp[k];
  809.     }
  810. }
  811. //Function to display sorted array
  812. void display(int a[5])
  813. {
  814.     int i;
  815.     cout<<"The Sorted array is";
  816.     for(i=0;i<n;i++)
  817.         cout<<a[i]<<"\t";
  818. }
  819.  
  820. (21)
  821.  
  822. Radix Sort
  823. #include<iostream.h>
  824. #include<conio.h>
  825. #include<math.h>
  826.  
  827. void main()
  828. {
  829.  
  830.     int a[100][100],r=0,c=0,i,sz,b[50],temp;
  831.     clrscr();
  832.     cout<<"Enter Size of Array";
  833.     cin>>sz;
  834.     for(r=0;r<100;r++)
  835.     {
  836.         for(c=0;c<100;c++)
  837.         {
  838.             a[r][c]=1000;
  839.         }
  840.     }
  841.     for(i=0;i<sz;i++)
  842.     {
  843.         cout<<"Enter Element";
  844.         cin>>b[i];
  845.         r=b[i]/100;
  846.         c=b[i]%100;
  847.         a[r][c]=b[i];
  848.     }
  849.     for(r=0;r<100;r++)
  850.     {
  851.         for(c=0;c<100;c++)
  852.         {
  853.            
  854.             for(i=0;i<sz;i++)
  855.             {
  856.                 if(a[r][c]==b[i])
  857.                 {
  858.                 cout<<a[r][c]<<"\t";
  859.                 }
  860.             }
  861.         }
  862.     }
  863.     getch();
  864. }
  865.  
  866. (22)
  867.  
  868. Program to find sum of 10 elements
  869. #include<iostream.h&gt
  870. #include<conio.h&gt
  871. void add(int a[]);
  872. void main()
  873. {
  874. clrscr();
  875. int a[10],i;
  876. cout<<"enter elements";
  877. cout<<"\n\n";
  878. for(i=0;i<10;i++)
  879.    {
  880.     cout<<"\n";
  881.     cin&gt&gta[i];
  882.    }
  883. add(a);
  884. getch();
  885. }
  886. void add(int a[])
  887.  {
  888.   int sum=0,i;
  889.   for(i=0;i<10;i++)
  890.      {
  891.       sum+=a[i];
  892.      }
  893.  cout<<"\n\n";
  894.  cout<<"sum is"<<sum;
  895.  }
  896.  
  897. (23)
  898.  
  899. Program to increase value of each element by 10
  900. #include<iostream.h>
  901. #include<conio.h>
  902. void inc(int a[]);
  903. void main()
  904. {
  905. clrscr();
  906. int a[10],i;
  907. cout<<"enter elements";
  908. cout<<"\n\n";
  909. for(i=0;i<10;i++)
  910.    {
  911.     cout<<"\n\n";
  912.     cin>>a[i];
  913.    }
  914. inc(a);
  915. getch();
  916. }
  917. void inc(int a[])
  918.  {
  919.   int i;
  920.   for(i=0;i<10;i++)
  921.      {
  922.       a[i]=a[i]+10;
  923.      }
  924. cout<<"\n\n";
  925. cout<<"new values are";
  926. cout<<"\n\n";
  927. for(i=0;i<10;i++)
  928.    {
  929.     cout<<a[i]<<"\t";
  930.    }
  931.  }
  932.  
  933. (24)
  934.  
  935. Program to count numbers greater than 5
  936. #include<iostream.h>
  937. #include<conio.h>
  938. int count(int a[],int n)
  939.  {
  940.    int count=0,i;
  941.    for(i=0;i<n;i++)
  942.       {
  943.         if(a[i]>5)
  944.           {
  945.            count++;
  946.           }
  947.        }
  948.   return(count);
  949.  }
  950. void main()
  951.  {
  952.   clrscr();
  953.   int a[10],n,i;
  954.   cout<<"enter size";
  955.   cout<<"\n\n";
  956.   cin>>n;
  957.   cout<<"\n\n";
  958.   cout<<"enter elements";
  959.   cout<<"\n\n";
  960.   for(i=0;i<n;i++)
  961.      {
  962.       cout<<"\n";
  963.       cin>>a[i];
  964.      }
  965.   cout<<"\n\n";
  966.   cout<<"nos. greater than 5 are"<<"\t"<<count(a,n);
  967. getch();
  968. }
Advertisement
Add Comment
Please, Sign In to add comment