1. Trang chủ
  2. » Công Nghệ Thông Tin

C.S.P0003 Program to insert new element into an existing array.

4 486 28

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 16,61 KB

Nội dung

Title Program to insert new element into an existing array.Background ContextInsert new element into an existing array Program SpecificationsCreate a program to insert new element into an existing arrayFunction details: 1.Enter an integer array2.Sort array in ascending and display the sorted array on the screen3.Enter new integer value4.Add new value entered to the sorted array in ascending order and then display new array on the screen5.Finish programExpectation of User interface:The Program must have interface as below:Please enter size of array: 5Enter element0: 13 Enter element1: 3Enter element2: 5Enter element3: 10Enter element4: 8The array after sorting:3581013Please enter new value: 9New array:35891013C.S.P0043

LAB101 Assignment Type: Code: LOC: Slot(s): Short Assignment wgd1540956345 50 Title Program to insert new element into an existing array Background Context Insert new element into an existing array Program Specifications Create a program to insert new element into an existing array Function details: Enter an integer array Sort array in ascending and display the sorted array on the screen Enter new integer value Add new value entered to the sorted array in ascending order and then display new array on the screen Finish program Expectation of User interface: The Program must have interface as below: Please enter size of array: Enter element[0]: 13 Enter element[1]: Enter element[2]: Enter element[3]: 10 Enter element[4]: The array after sorting: 10 Please enter new value: New array: 13 10 13 Code #include #include int cleanStdin() { while (getchar() != '\n') { } return 1; } //check if the size of array is an integer higher than int checkSize() { float value; char after; int check; printf("Please enter size of array: "); check = scanf("%f%c",&value,&after); while(1) { if((check == || after != '\n') && cleanStdin() ) { printf("\nThe number you typed includes letter(s) Please enter a number: ");check = scanf("%f%c",&value,&after); }else { if(value != (int)value) { printf("\nThe size of array must be an integer Please try again: ");check = scanf("%f%c",&value,&after); }else { if(value < 1) { printf("\nThe number you typed is too small Please enter a number higher than 0: ");check = scanf("%f%c",&value,&after); }else return value; } } } } int checkElement(int i) { float value; char after; int check; printf("Enter element[%d]: ",i); check = scanf("%f%c",&value,&after); while(1) { if((check == || after != '\n') && cleanStdin() ) { printf("\nThe number you typed includes letter(s) Please enter a number: ");check = scanf("%f%c",&value,&after); }else { if(value != (int)value) { printf("\nThe element of array must be an integer Please try again: ");check = scanf("%f%c",&value,&after); }else { printf("The element[%d]: %d is correct!\n\n",i,(int)value); return value; } } } } int checkNewElement() { float value; char after; int check; printf("\nPlease enter new value: "); check = scanf("%f%c",&value,&after); while(1) { if((check == || after != '\n') && cleanStdin() ) { printf("\nThe number you typed includes letter(s) Please enter a number: ");check = scanf("%f%c",&value,&after); }else { if(value != (int)value) { printf("\nThe new element of array must be an integer Please try again: ");check = scanf("%f%c",&value,&after); }else { printf("The new element %d is correct!\n\n",(int)value); return value; } } } } //sort array increasingly void bubble(int array[], int size) { int i, j; // loop through all numbers for(i = 0; i < size - 1; i ++) { // loop through numbers falling ahead // Last i elements are already in place for(j = i + 1; j < size; j++) { //check if next number is lesser than current no if(array[i] > array[j]) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } int insert(int a[], int n, int v, int k) { //chen so v vao vi tri k int i; for(i = n; i > k; i ) { a[i] = a[i-1]; } a[k] = v;n++; return n; } int main(int argc, char** argv) { int size= checkSize(); int array[size]; //ask user to input the values of the array for(int i = 0; i < size; i++){ array[i] = checkElement(i); } bubble(array,size); printf("The array after sorting:\n"); //loop printing all elements sorted increasingly for(int i = 0;i < size; i++) printf("%d\t",array[i]); int new_element = checkNewElement(); //insert the number to end of array int i = 0; while(array[i] < new_element) i++; printf("%d\n",i); printf("%d",array[i]); size = insert(array,size,new_element,i); printf("\nNew array:\n"); //to display the new sorted array for(int i = 0;i < size; i++) printf("%d \t",array[i]); return 0; }

Ngày đăng: 31/10/2018, 10:25

TỪ KHÓA LIÊN QUAN

w