Answer to Question #131414 in C++ for Neelo

Question #131414

Write, compile, and run a C++ program that inputs 10 double-precision numbers

in an array named raw. After these numbers are entered in the array, your program should cycle

through raw 10 times. During each pass through the array, your program should select the lowest

value in raw and place it in the next available slot in an array named sorted. When your

program is finished, the sorted array should contain the numbers in raw in sorted order from

lowest to highest. (Hint: Be sure to reset the lowest value selected during each pass to a very

high number so that it’s not selected again. You need a second for loop in the first for loop to

locate the minimum value for each pass.)


1
Expert's answer
2020-09-02T10:32:05-0400
#include <iostream>


using namespace std;




/// <summary>
/// Input array fron the console
/// </summary>
/// <param name="arr"></param>
/// <param name="arr_size"></param>
void input_array(double *arr, const size_t& arr_size)
{
	double input = 0;
	for (size_t i = 0; i < arr_size; i++)
	{
		cout << "Input array[" << i << "] : ";
		cin >> input;
		arr[i] = input;
	}
}




/// <summary>
/// Visualization of the array
/// </summary>
/// <param name="arr"></param>
/// <param name="arr_size"></param>
void print_array(const double* arr, const size_t& arr_size)
{
	cout << "[ ";
	for (size_t i = 0; i < arr_size; i++)
	{
		cout << arr[i] << ((i != (arr_size-1)) ? " , " : " ]");
	}
}




/// <summary>
/// According to the task, this function finds min elem 
/// in the array and detet it. After that creates the new arra
/// of the size-1 and without min element from the previous array.
/// </summary>
/// <param name="a"></param>
/// <param name="h"></param>
/// <param name="arr_size"></param>
/// <param name="lowest"></param>
void delete_lowest(const double *a, double *h, const size_t &arr_size, const double &lowest)
{
	size_t helper_size = arr_size-1;


	double* helper_array = new double[helper_size];


	size_t b = arr_size;
	for (size_t i = 0; i < b; i++)
	{
		if (a[i] == lowest )
		{
			b = i;
			break;
		}
		else
		{
			helper_array[i] = a[i];
		}
	}


	for (size_t i = b + 1; i < arr_size; i++)
	{
		size_t c = i;
		helper_array[c-1] = a[c];
	}


	for (size_t i = 0; i < arr_size-1; i++)
	{
		h[i] = helper_array[i];
	}




	delete[] helper_array;
};




void sort(double *a, double *b, const size_t &arr_size)
{
	double* helper = new double[arr_size];
	size_t s = arr_size;


	for (size_t i = 0; i < arr_size; i++)
	{
		helper[i] = a[i];
	}




	double lowest = helper[0];
	for (size_t i = 0; i < arr_size; i++)
	{
		for (size_t i = 0; i < s; i++)
		{
			a[i] = helper[i];
		}


		for (size_t j = 0; j < s; j++)
		{
			if (helper[j] < lowest)
			{
				lowest = helper[j];	
			}
		}


		delete_lowest(a, helper, s, lowest);
		
		s -= 1;
		/*cout << endl << "Helper: ";
		print_array(helper, s);
		cout << endl;
		cout << endl << lowest << endl;*/
		b[i] = lowest;
		lowest = helper[0];
	}


	
}










int main()
{
	const size_t len = 10;


	double* raw = new double[len];


	double* sorted = new double[len];
	






	input_array(raw, len);
	cout << "Unsorted: ";
	print_array(raw, len);


	sort(raw, sorted, len);


	cout << endl << "Sorted: ";
	print_array(sorted, len);
	cout << endl;




	delete[] raw;
	delete[] sorted;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS