Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<time.h>
- #include<stdlib.h>
- void selector(int optn,int size,int* arr);
- void findMin(int* arr,int size);
- void deleteElement(int size,int* arr);
- int* populateArray(int size); // return type pointer
- void printArray(int* arr,int size);
- int main(){
- int bool=0;
- do{
- int size=0,first=0,choice=0;
- int* arr;
- printf("\n\n\nenter array size: ");
- scanf("%d",&size);
- arr=populateArray(size); //returns the starting position of array
- printArray(arr,size); // displays the array on screen
- do{
- printf("\n\naction to perform: 1.find minimum 2.delete an element\nenter choice:");
- scanf("%d",&choice);
- selector(choice,size,arr);
- printf("iterate again?\n (1)Yes, with existing array \n (2)Yes, with new array \n (0)no\nenter choice: ");
- scanf("%d",&bool);
- }while(bool==1);
- }while(bool==2);
- return 0;
- }
- int* populateArray(int size){
- int i=0;
- int* arr;
- arr=(int*)malloc(sizeof(int)*size);
- srand(time(NULL)); //creates seed for rand() by taking system exec time
- for(i=0;i<size;i++){
- *(arr+i)=(rand()%1000); // populates array with integers 0-999
- }
- return arr;
- }
- void printArray(int* arr,int size){
- int i;
- for(i=0;i<size;i++)
- printf("%d ",*(arr+i));
- puts("\n");
- }
- void selector(int optn,int size,int* arr){
- puts("\nin selector\n");
- if(optn==1){
- printf("you seslected findMin\n");
- findMin(arr,size);
- }
- else if(optn==2){
- printf("you selected deleteElement\n");
- deleteElement(size,arr);
- }
- }
- void findMin(int* arr,int size){
- puts("\nfinding Min...");
- int min,loc,i;
- min=*(arr+0);
- for(i=1;i<size;i++){
- if(*(arr+i)<min){
- min=*(arr+i);
- loc=i;
- }
- }
- printf("min value:%3d found at %3d\n\n",min,(loc+1));
- }
- void deleteElement(int size,int* arr){
- int bool=0;
- do{
- int Elem=0,optn=0,i=0,loc;
- printf("1.delete index or 2.delete element? enter choice: ");
- scanf("%d",&optn);
- switch(optn){
- case 1:
- printf("enter position to delete: ");
- scanf("%d",&Elem);
- if(Elem==size){
- size--;
- }
- else{
- for(i=(Elem-1);i<size;i++){
- *(arr+i)=*(arr+(i+1));
- }
- size--;
- }
- printf("\narray after deletion: ");
- printArray(arr,size); // size is reduced by 1 after deletion
- break;
- case 2:
- printf("enter element to delete: ");
- scanf("%d",&Elem);
- for(i=0;i<size;i++){
- if(*(arr+i)==Elem){
- loc=i;
- break;
- }
- }
- if(loc==(size-1)){
- size--;
- }
- else {
- for(i=loc;i<size;i++){
- *(arr+i)=*(arr+(i+1));
- }
- size--;
- }
- printf("\narray after deletion: ");
- printArray(arr,size); // size is reduced by 1 after deletion
- break;
- }
- printf("delete another? (1)yes or (0)no\nenter choice: ");
- scanf("%d",&bool);
- }while(bool==1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement