Tải bản đầy đủ

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

LAB101 Assignment

Type:
Code:
LOC:
Slot(s):

Short Assignment
wgd1540956345
50
1

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:

1. Enter an integer array
2. Sort array in ascending and display the sorted array on the screen
3. Enter new integer value
4. Add new value entered to the sorted array in ascending order and then display new array on the screen
5. Finish program
Expectation of User interface:
The Program must have interface as below:
Please enter size of array: 5
Enter element[0]: 13
Enter element[1]: 3
Enter element[2]: 5
Enter element[3]: 10
Enter element[4]: 8
The array after sorting:
3
5
8
10
Please enter new value: 9
New array:
3
5
8
9

13

10

13

Code
#include
#include
int cleanStdin() {
while (getchar() != '\n') {
}
return 1;
}
//check if the size of array is an integer higher than 0




int checkSize() {
float value;
char after;
int check;
printf("Please enter size of array: ");
check = scanf("%f%c",&value,&after);
while(1) {
if((check == 0 || 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 == 0 || 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 == 0 || 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;
}



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×