#include int counter=0; int size; void deleteNumber(int array[],int index); void insertNumber(int array[],int index,int value); void insertIntelligently(int array[],int number); void insertByvalue(int array[]); void deleteByrange(int array[]); void search(int array[]); void ascendingOrder(int array[]); void descendingOrder(int array[]);
#include <stdio.h>
int counter=0;
int size;
void deleteNumber(int array[],int index);
void insertNumber(int array[],int index,int value);
void insertIntelligently(int array[],int number);
void insertByvalue(int array[]);
void deleteByrange(int array[]);
void search(int array[]);
void ascendingOrder(int array[]);
void descendingOrder(int array[]);
void replace(int array[]);
void printArray(int array[]);
int main() {
int array[size], choice, i;
printf("Enter size of the array");
scanf("%d", &size);
for (i = 0; i < size; i++) {
printf("Enter the value for Index %d: ", i);
scanf("%d", &array[i]);
printf("\n");
counter++;
printf("%d", counter);
}
printArray(array);
printf("\nCounter is: %d \n", counter);
printf("\n");
while (choice != 9) {
printf("\n\nPress '1' to Insert by Index and Value \nPress '2' to Delete \nPress '3' to Insert by Value\nPress '4' to Delete By Range\nPress '5' to replace a value\nPress '6' to search a number\nPress '7' to print in ascending order\nPress '8' to print in descending order\n");
scanf("%d", &choice);
if (choice == 1) {
int index, value, option;
printf("Do you want to insert the nnumber intelligently or on a specific position?/n Press 1 to insert intelligently!!/n Press 2 to insert at a specific position!!");
scanf("%d", &option);
if (option == 1) {
int number;
printf("Enter number to place it intelligently!! ");
scanf("%d", &number);
insertIntelligently(array, number);
printArray(array);
} else if (option == 2) {
printf("Enter index to insert number at: \n");
scanf("%d", &index);
if (index > counter) {
printf("Enter Valid Index!\n");
} else {
printf("Enter value to insert: \n");
scanf("%d", &value);
insertNumber(array, index, value);
printArray(array);
}
} else if (choice == 2) {
int index;
printf("Enter index to delete number: \n");
scanf("%d", &index);
if (index >= counter) {
printf("Deletion not possible!!!\n");
} else {
deleteNumber(array, index);
}
printArray(array);
} else if (choice == 3) {
insertByvalue(array);
} else if (choice == 4) {
deleteByrange(array);
} else if (choice == 5) {
replace(array);
} else if (choice == 6) {
search(array);
} else if (choice == 7) {
ascendingOrder(array);
} else if (choice == 8) {
descendingOrder(array);
}
}
}
return 0;
}
void insertIntelligently(int array[],int number){
int i,j;
for(i=0;i<counter;i++){
if ( number > array[i] && number< array[i+1] )
{
for( j=counter-1 ; j >=i ; j--)
{
array[j+1]=array[j];
printf("++++");
}
array[i+1]=number;
counter+=1;
break;
}else printf("elseee");
}
}
void insertByvalue(int array[]){
int v;
printf("Enter value to insert: \n");
scanf("%d", &v);
array[counter]=v;
counter++;
}
void printArray(int array[]){
int j;
printf("Resultant Array is: ");
for (j=0;j<counter; j++){
printf("%d ", array[j]);
}
}
void deleteByrange(int array[]){
int fromIndex,toIndex,i,j;
printf("Enter the starting index: \n");
scanf("%d", &fromIndex);
printf("Enter the ending index: \n");
scanf("%d", &toIndex);
if(fromIndex>=counter || fromIndex<0){
printf("Invalid Index!!");
deleteByrange(array);
}
if(toIndex>=counter || toIndex<0){
printf("Invalid Index!!");
deleteByrange(array);
}
for(i=toIndex ; i>=fromIndex ; i--){
array[i]=array[i+1];
counter--;
}
printArray(array);
}
void replace(int array[]){
int index,value;
printf("Enter the index: \n");
scanf("%d",&index);
if(index<0 || index>counter-1){
printf("Enter Valid Index\n");
replace(array);
}
printf("Enter the value: \n");
scanf("%d",&value);
array[index]=value;
printArray(array);
}
void search(int array[]){
int key;
int low = 0;
int high = counter - 1;
int mid = (low+high)/2;
printf("Enter the value you want to search");
scanf("%d",key);
while (low <= high) {
if(array[mid] < key) {
low = mid + 1;
}
else if (array[mid] == key) {
printf("%d found at location %d", key, mid+1);
break;
}
else{
high = mid - 1;
mid = (low + high)/2;
}
}
if(low > high){
printf("Not found! %d isn't present in the list", key);
}
}
void ascendingOrder(int array[]){
int i,j,temp;
for(i=0;i<counter;i++){
for(j=i+1;j<counter;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
void descendingOrder(int array[]){
int i,j,temp;
for(i=0;i<counter;i++){
for(j=i+1;j<counter;j++){
if(array[i]<array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
printArray(array);
}
Step by step
Solved in 2 steps with 1 images