Advertisement
defineSN

find min and delete(as index as well as element)

Jan 20th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4.  
  5.  
  6. void selector(int optn,int size,int* arr);
  7. void findMin(int* arr,int size);
  8. void deleteElement(int size,int* arr);
  9. int* populateArray(int size); // return type pointer
  10. void printArray(int* arr,int size);
  11.  
  12. int main(){
  13.    
  14.     int bool=0;
  15.     do{
  16.         int size=0,first=0,choice=0;
  17.         int* arr;
  18.        
  19.         printf("\n\n\nenter array size: ");
  20.         scanf("%d",&size);
  21.        
  22.         arr=populateArray(size); //returns the starting position of array
  23.         printArray(arr,size);   // displays the array on screen
  24.        
  25.         do{
  26.             printf("\n\naction to perform: 1.find minimum 2.delete an element\nenter choice:");
  27.             scanf("%d",&choice);
  28.             selector(choice,size,arr);
  29.  
  30.             printf("iterate again?\n (1)Yes, with existing array \n (2)Yes, with new array \n (0)no\nenter choice: ");
  31.             scanf("%d",&bool);     
  32.         }while(bool==1);
  33.    
  34.     }while(bool==2);   
  35.    
  36.     return 0;
  37. }
  38.  
  39.  
  40.  
  41. int* populateArray(int size){
  42.     int i=0;
  43.     int* arr;
  44.    
  45.     arr=(int*)malloc(sizeof(int)*size);
  46.    
  47.     srand(time(NULL)); //creates seed for rand() by taking system exec time
  48.     for(i=0;i<size;i++){
  49.         *(arr+i)=(rand()%1000); // populates array with integers 0-999 
  50.     }  
  51.     return arr;
  52. }
  53.  
  54. void printArray(int* arr,int size){
  55.    
  56.     int i;
  57.     for(i=0;i<size;i++)
  58.     printf("%d ",*(arr+i));
  59.     puts("\n");
  60. }
  61.  
  62. void selector(int optn,int size,int* arr){
  63.     puts("\nin selector\n");
  64.     if(optn==1){
  65.         printf("you seslected findMin\n");
  66.         findMin(arr,size);
  67.     }
  68.     else if(optn==2){
  69.         printf("you selected deleteElement\n");
  70.         deleteElement(size,arr);   
  71.    
  72.     }
  73. }
  74.  
  75. void findMin(int* arr,int size){
  76.     puts("\nfinding Min...");
  77.     int min,loc,i;
  78.    
  79.     min=*(arr+0);
  80.     for(i=1;i<size;i++){
  81.         if(*(arr+i)<min){
  82.             min=*(arr+i);
  83.             loc=i;
  84.         }
  85.     }
  86.     printf("min value:%3d found at %3d\n\n",min,(loc+1));
  87. }
  88.  
  89. void deleteElement(int size,int* arr){ 
  90.    
  91.     int bool=0;
  92.     do{
  93.  
  94.         int Elem=0,optn=0,i=0,loc;
  95.            
  96.         printf("1.delete index or 2.delete element? enter choice: ");
  97.         scanf("%d",&optn);
  98.         switch(optn){
  99.             case 1:
  100.                 printf("enter position to delete: ");
  101.                 scanf("%d",&Elem);
  102.                
  103.                 if(Elem==size){
  104.                     size--;
  105.                 }
  106.                 else{
  107.                     for(i=(Elem-1);i<size;i++){
  108.                         *(arr+i)=*(arr+(i+1));
  109.                     }
  110.                     size--;
  111.                 }
  112.  
  113.                 printf("\narray after deletion: ");
  114.                 printArray(arr,size); // size is reduced by 1 after deletion
  115.                 break;
  116.            
  117.             case 2:
  118.                 printf("enter element to delete: ");
  119.                 scanf("%d",&Elem);
  120.                
  121.                 for(i=0;i<size;i++){
  122.                     if(*(arr+i)==Elem){
  123.                         loc=i;
  124.                         break;         
  125.                     }
  126.                 }
  127.                 if(loc==(size-1)){
  128.                     size--;
  129.                 }              
  130.                 else {
  131.                     for(i=loc;i<size;i++){
  132.                         *(arr+i)=*(arr+(i+1));
  133.                     }
  134.                     size--;
  135.                 }  
  136.                 printf("\narray after deletion: ");
  137.                 printArray(arr,size); // size is reduced by 1 after deletion
  138.                 break;             
  139.         }
  140.        
  141.         printf("delete another? (1)yes or (0)no\nenter choice: ");
  142.         scanf("%d",&bool); 
  143.     }while(bool==1);
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement