Computer Science, asked by myidentity, 1 year ago

ICSE Write a Program to Initialize an array of ten numerical elements sorted in ascending order.
Using binary search print out cell position of an accepted number from a user.

Answers

Answered by jogender77
1
{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++) //Loop for ascending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] > a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing array data after sorting
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) //Loop for descending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] < a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nDescending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing array data after sorting
{
printf(" %d ", a[i]); //Printing data
}

return 0; //returning 0 status to system
}

//Ouput
/*

Array size: 10

Elements : 3 4 7 6 5 1 2 8 10 9

Ascending : 1 2 3 4 5 6 7 8 9 10

Descending : 10 9 8 7 6 5 4 3 2 1
*/
Similar questions