The following example repeatedly asks for a number. As each number is entered, the array (arr) is dynamically resized to accommodate the new number. To finish entering numbers, enter any non-number (such as the letter X). If any numbers were entered, the array is then sorted using a simple bubble sort and the result is then displayed.
Note that resizing an array like this is highly inefficient because the array must be copied each time it is resized. If you can determine how many numbers will be entered in advance (at runtime), then the array can be allocated just once, before reading the numbers into each array element.
#include <iostream>
int main()
{
float t, num = 0, *arr = NULL;
int max = 0, i, j;
while( 1 )
{
printf( "\nEnter a number:\n(Any letter to quit)\n>" );
int iResult = scanf( "%f", &num );
if( !iResult )
break;
arr = ( float * ) realloc( arr, ++max * sizeof( float ));
arr[ max-1 ] = num;
}
printf( "%d numbers entered.\n", max );
if( max )
{
for( i=1; i<max; i++ )
{
for( j=0; j<max-i; j++ )
{
if( arr[j] > arr[j+1] )
{
t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
printf( "Sorted:\n");
for( i=0; i<max; i++ )
printf( "%f\n", arr[i] );
printf( "\n" );
free( arr );
arr = NULL;
}
return( 0 );
}
To write a C++ program to display the student details using class and array of object.
Using sorted(array,reverse=True)
Sort the array then traverse the array, printing the element values as you go.
It means that elements are fetched from the array in the same order they arrive - first in, first out (FIFO). Also called a queue.It means that elements are fetched from the array in the same order they arrive - first in, first out (FIFO). Also called a queue.It means that elements are fetched from the array in the same order they arrive - first in, first out (FIFO). Also called a queue.It means that elements are fetched from the array in the same order they arrive - first in, first out (FIFO). Also called a queue.
Start by pointing to each end of the array. Work your way towards the middle of the array, swapping elements as you go. When the pointers meet or pass each other, the array is completely reversed.
sorry
import java.util.Arrays; public class arraysort { public static void main(String[] a) { int array[] = { 2, 5, -2, 6, -3 }; Arrays.sort(array); for (int i : array) { System.out.println(i); } } }
To write a C++ program to display the student details using class and array of object.
To find the median of an array of numbers, first, arrange the numbers in ascending order. If the array has an odd number of elements, the median is the middle number. If the array has an even number of elements, the median is the average of the two middle numbers.
To implement the keyword "sorting" in pseudo code to arrange the elements of an array a of integers in ascending order, you can use the following algorithm: Start by iterating through the array a from the first element to the second-to-last element. Compare each element with the next element in the array. If the current element is greater than the next element, swap their positions. Continue this process until the entire array is sorted in ascending order. Here is a simple example of pseudo code for implementing the sorting algorithm: for i from 0 to length(a) - 1 do for j from 0 to length(a) - i - 1 do if aj aj 1 then swap(aj, aj 1) end if end for end for This pseudo code represents a basic implementation of a sorting algorithm to arrange the elements of an array in ascending order.
Using sorted(array,reverse=True)
I know how to do this and you need to know how to do this. Why don't you do your best at writing this program and if it does not work then ask for help. You will not learn anything if I give you the answer.
addends
Sort the array then traverse the array, printing the element values as you go.
The value of the kth smallest element in the array is the kth element when the array is sorted in ascending order.
An ordered array is simply an array where all elements are in sorted order: int a[] = {3, 6, 9, 10, 15, 21}; // ordered array An array can either be initialised with ordered elements or the elements may be sorted after initialisation. When inserting new elements into an ordered array, the order must be maintained.
Assume an array of integers (size > 0) to be sorted in descending order. And the values have been populated already: const int size = 50; //by changing the value here int[] array = new int[](); // omit the initialization of array Array.Sort(array); // array will be changed with ascending order Array.Reverse(array); // change from ascending to descending NOTE: Array.Sort() and Array.Reverse() are 2 of the functions in the class library. We do not care the algorithm being used within the functions, just the result. Also noted that the these 2 functions do change the content of the source array. It may not be what you want