/* Purpose : Array Manipulations( add , delete , sort , display ) ; Author : Linhvqge140868 Date :: 27/02/2020 */ #include #include #include void addValue( int * , int * ); void searchValue( int *, int , int ); void deleteFirstValue( int * , int * , int ) ; void displayArray( int * , int ); void deleteAllOfValue(int * , int * , int ); void swap( int * , int *) ; void sortArray( int * , int , int ) ; int gettInt() ; main(){ int *arr , n = , value , m ; int choice ; char getCharacter ; arr = (int *)malloc( n * sizeof(int)); do{ printf("\n\n\tAdmin !\n1- Add a value\n2- Search a value\n3- Remove the first existence of a value\n4- Remove all existences of a value\n5- Print out the array \n6- Sort the array in ascending order (positions of elements are preserved)\n7- Sort the array in descending order (positions of elements are preserved)"); printf("\nWhat is your choice ? "); scanf("%d" , &choice) ; switch(choice){ case : // Case : dung de add gia tri addValue(arr, &n ); break ; case : // Case : dung de search gia tri searchValue(arr,n,value); break ; case : // Case : Xoa di gia tri xuat hien dau tien cua phan tu da nhap deleteFirstValue(arr,&n,value); break ; case : // Case : Xoa di tat ca gia tri xuat hien ma user muon xoa deleteAllOfValue(arr,&n,value); break ; case : // Case : Hien thi mang displayArray(arr,n); break ; case : // Case : Sap xep mang theo tang dan m = ; sortArray(arr , n , m ); break ; // Case : Sap xep mang giam dan case : m = 0; sortArray(arr , n , m ); break ; default : break ; } }while( choice >= && choice size cua mang ban dau in thong bao add cong if( *n > m ){ printf("\nAdditon element successflly !"); } // Neu nho hon in thong bao khong add cong else{ printf("\nNo element have add !"); } } /* Purpose : Tim kiem vi tri cua gia tri nhap vao Author : Linhvqhe140868 Date : 27/02/2020 arr : la mang truyen vao n : la size cua mang value : la gia tri can search */ void searchValue( int *arr , int n , int value){ printf("Enter your value which you want to search : "); value = gettInt() ; /* Khoi tao mang moi de luu nhung vi tri ma gia tri xuat hien va khoi tao size cua mang ban dau = */ int i , sizeArray2 = , array2[100] ; // bien i se chay tu den size - for( i = ; i < n ; i++){ // Kiem tra gia tri tai vi tri i co == gia tri can search if( arr[i] == value){ // KNeu bang se gan vi tri vao mang moi da khoi tao array2[sizeArray2] = i ; // Tang size cua mang len sizeArray2++ ; } } if( sizeArray2 != 0){ // Hien thi nhung vi tri can tim kiem printf("\nIndex of your value apperance in array : "); // i se chay tu den size cua mang moi - for( i = ; i < sizeArray2 ; i++){ printf("\t%d", array2[i]) ; } } else{ printf("\nYour value which you want to search not exist !"); } } /* Purpose : Xoa bo gia tri dau tien nhap vao Author : Linhvqhe140868 Date : 27/02/2020 */ void deleteFirstValue( int *arr , int *n , int value){ // i , j la hai bien chay int i , j; int m = *n ; printf("Enter your value which you want to delete : "); value = gettInt() ; // i chay tu den size cua mang - ; for( i = ; i < (*n); i++ ){ // Kiem tra gia tri tai vi tri i co bang gia tri can xoa if( arr[i] == value){ // Neu bang chay vong lap thu j chay tu vi tri i den size cua mang - for( j = i ; j < (*n) - ; j++){ // Gan gia tri tai vi tri bang gia tri tai vi tri j + va tiep tuc thuc hien vong lap arr[j] = arr[j + 1] ; } // Cap phat lai bo nho cho zie cua mang arr = ( int*)realloc( arr , ((*n)-1) * sizeof(int)); // Giam size uca mang di (*n) ; } // dung lenh break de thoat khoi vong lap for dau tien break ; } // Check size cua mang da thay doi sau xoa if( m != *n ){ //Khi da thay doi in thong bao printf("\nDelete !"); } else{ // Neu size van duoc giu nguyen in thong bao printf("\nYour value which you want to delete not exist !"); } } /* Purpose : Hien thi mang Author : Linhvqhe140868 Date : 27/02/2020 */ void displayArray( int *arr , int n){ // i la bien chay int i ; if( n > ){ // i se chay tu - den size cua mang - for( i = ; i < n ; i++){ // Hien thi nhung phan tu mang printf("\t%d" , arr[i]) ; } } else{ } printf("\nArray is empty array !") ; } /* Purpose : Xoa bo tat ca gia tri bang gia tri nhap vao Author : Linhvqhe140868 Date : 27/02/2020 */ void deleteAllOfValue( int *arr , int *n , int value){ // i , j la hai bien chay int i , j; printf("Enter your value which you want to delete : "); value = gettInt() ; // i chay tu den size cua mang - ; int m = *n ; for( i = ; i < (*n); i++ ){ // Kiem tra gia tri tai vi tri i co bang gia tri can xoa if( arr[i] == value){ // Neu bang chay vong lap thu j chay tu vi tri i den size cua mang - for( j = i ; j < (*n) - ; j++){ // Gan gia tri tai vi tri bang gia tri tai vi tri j + va tiep tuc thuc hien vong lap arr[j] = arr[j + 1] ; } // Cap phat lai bo nho cho zie cua mang arr = ( int*)realloc( arr , ((*n)-1) * sizeof(int)); // Giam size uca mang di (*n) ; // Giam bien thu j di de xoa nhung phan tu trung ma o canh j ; } } + // vong lap thuc hien xoa lai ki tu ma nguoi dung muon xoa // i chay tu den size cua mang - for( i = ; i < (*n) ; i++){ // Check gia tri tai vi tri i == gia tri ma user nhap if( arr[i] == value){ // Vong lap thay chay de xoa gia tri khoi mang // j se chay tu vi i , noi ma gia tri xuat hien for( j = i ; j < (*n) - ; j++){ // Gia tri tai vi tri j se gan bang gia tri tai vi tri j arr[j] = arr[ j + 1]; } // Giam size cua mang di ; (*n) ; } } // Check size cua mang sau xoa ma < size uca mang ban dau se in thong bao if( m > *n ){ printf("\nDelete successfully !"); } else{ printf("\nYour value which you want to delete not exist !"); } } /* Purpose : Sap xep lai mang Author : Linhvqhe140868 Date : 26/02/2020 */ void sortArray( int *arr , int n , int m){ int i , j ; // i chay tu den size cua mang - if( n >= ){ for( i = ; i < n ; i++){ // j chay tu size mang - den lon hon i , va j giam dan for( j = n - ; j > i ; j ){ // check neu a[j - 1] > a[ j ] va gia tri check == thi se sap xep tang dan // truong hop lai neu a[j - 1] < a[j] va gia tri check == thi se sap xep giam dan if( (arr[j - 1] > arr[j] && m == 1) || (arr[j - 1] < arr[j]) && m == ){ // Ham doi cho hai gia tri swap(&arr[j-1] , &arr[j]) ; } } } printf("\nArray have been sorted !"); } } else{ printf("\nYour array not element enough to sort !") ; } int gettInt(){/* Purpose : Ham phan tich du lieu nguoi dung nhap vao Author : Linhvqhe140868 Date : 2/18/2020 */ int = , max = 9999 ; /* value la gia tri nguoi dung nhap kepptrying la gia tri dung de check */ int value , keeptrying = , rc ; // // ki tu cuoi cung ma nguoi dung da nhap char after ; do{ rc = scanf("%d%c" , &value , &after) ; // rc = thi nguoi dung nhap vao kieu ki tu chu khong phai kieu so if( rc == ){ printf("** No input accepted!**\n\n") ; fflush(stdin) ; } // after != '\n' la co chua ki tu o vi tri cuoi cung else if( after != '\n'){ printf("** Trailing character !**\n\n") ; fflush(stdin) ; } cho phep // kiem tra value co nam pham vi gia tri ma chuong trinh else if(value < || value > max){ printf("** Out of range !**\n\n"); } else // nhung truong hop nha vao dung yeu cau cua chuong trinh thi se lam thay doi gia tri cua keeptrying keeptrying = ; }while( keeptrying == 1) ; keeptrying khong doi } return value ; // vong lap se thuc hien gia tri cua