answersLogoWhite

0

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 );

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What will be the program to arrange numbers stored in array in ascending order using pointers?

sorry


Write a program to sort elements of an array in ascending order?

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); } } }


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


What is the process to find the median of an array of numbers?

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.


How can the keyword "sorting" be implemented in pseudo code to arrange the elements of an array a of integers in ascending order?

To implement the keyword &quot;sorting&quot; 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.


Sort array in ascending descending order in python?

Using sorted(array,reverse=True)


How can you write a c program to display numbers in ascending order in array?

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.


How do you arrange 4 numbers in ascending order without array?

addends


How do you display 15 numbers in an ascending order using an array?

Sort the array then traverse the array, printing the element values as you go.


What is the value of the kth smallest element in the given array?

The value of the kth smallest element in the array is the kth element when the array is sorted in ascending order.


What is a example of a array?

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.


How use array sort program in c sharp?

Assume an array of integers (size &gt; 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