Assignment - 1: Problem Statement: Ans Algorithm Input Output Array
Assignment - 1: Problem Statement: Ans Algorithm Input Output Array
Problem Statement :
Write a C program to insert a given number in an specific position in an array.
Ans:
Algorithm: array_insertion()
Step 1: Start
Step 2: Print "Enter the size of the array: "
Step 3: Read size /* 'size' value taken from user */
Step 4: arr[size+1] /* declaration of array */
Step 5: If size>0 then /* checking size validity */
Step 5.1: Print “Enter the elements of the array: ”
Step 5.2: Set i to 0
Step 5.3: For i < size do /* beginning of for loop */
Step 5.3.1: Read arr[i] /* array elements taken from user */
Step 5.3.2: i <-- i+1 /* increment loop variant, i=i+1 */
Step 5.4: EndFor /* ending of for loop */
Step 5.5: Print “Display the array before insertion:”
Step 5.6: Set i to 0
Step 5.7: For i < size do /* beginning of for loop */
Step 5.7.1: Print arr[i] /* display array elements before insertion */
Step 5.7.2: i <-- i+1 /* increment loop variant, i=i+1 */
Step 5.8: EndFor /* ending of for loop */
Step 5.9: Print "Enter the new number: "
Step 5.10: Read num /* 'num' value taken from user */
Step 5.11: Print "Enter the position: "
Step 5.12: Read pos /* position value taken from user */
Step 5.13: Set i to size-1
Step 5.14: For i >= pos /* beginning of for loop */
Step 5.14.1: arr[i+1]=arr[i] /* shifting array elements */
Step 5.14.2: i <-- i-1 /* decrement loop variant, i=i-1 */
Step 5.15: EndFor /* ending of for loop */
Step 5.16: arr[pos] <-- num /* put the value in given position */
Step 5.17: Print " Display tha array after insertion: "
Step 5.18: Set i to 0
Step 5.19: For i < size do /* beginning of for loop */
Step 5.19.1: Print arr[i] /*display the array after inserting the new element*/
Step 5.19.2: i <-- i+1 /* increment loop variant, i=i+1 */
Step 5.20: EndFor /* ending of for loop */
Step 6: Else
Step 6.1: Print " Invalid size..."
Step 7: EndIf
Step 8: End
Source Code:
#include <stdio.h> /* inclusion of standard input-output header file */
int main(){ /* beginning of main() function */
int i,size,pos,num; /* 'i' is loop variant,'size','pos' and 'num' value taken from user*/
printf("\nEnter the size of the array: ");
scanf("%d",&size); /* 'size' value taken from user*/
int arr[size+1]; /* declaration of array 'arr' */
if (size>0){ /* size validity checking */
printf("\nEnter elements of the array:\n");
for(i=0;i<size;i++)
scanf("%d",&arr[i]); /* array elements taken from user */
printf("\nDisplay the array before insertion:\n");
for(i=0;i<size;i++)
printf("%d ",arr[i]); /* display the array before insertion */
printf("\nEnter the new number: ");
scanf("%d",&num); /* new number given by user */
printf("\nEnter the position: ");
scanf("%d",&pos); /* position value given by user */
/*check the position */
for(i=size-1 ; i>=pos ; i--){
arr[i+1]=arr[i];
}
arr[pos]=num; /* put the number at founded position */
printf("\nDisplay tha array after insertion:\n");
for(i=0;i<size+1;i++)
printf("%d ",arr[i]); /* display the array after insertion */
}
else
printf("\nInvalid size...");
return 0;
}
Output:
john@john-desktop:~$ gedit ar.c
john@john-desktop:~$ gcc -o ar ar.c
john@john-desktop:~$ ./ar
Enter the size of the array: 3
Enter elements of the array:
11
22
33
Display the array before insertion:
11 22 33
Enter the new number: 44
Enter the position:2
Display tha array after insertion:
11 22 44 33
john@john-desktop:~$ ./ar
Enter the size of the array: 0
Invalid size...
john@john-desktop:~$ ./ar
Enter the size of the array: 4
12
23
34
45
Discussion:
‘size’,’pos’ and ‘num’ taken from user. Array elements taken from user.Then display the
entire array before insertion.For loop check the position & shifting the array elements to
insert the value into the array with respect to given position.After putting the elements in
right position again displaying the entire array with new element.