Hello,
I need help with re-ordering the values in ascending order in the array Unordered. However, I cannot use sort or swap functions. The values in array Index are used as indexes of array Unordered to put the values into the sequence shown in Ordered. I am writing this code for a class and cannot go out of scope; 2D arrays are the farthest ive gotten. So, I can use functions but no classes nor structs.

Unordered: 8 5 32 -6 15
Index: 2 1 4 0 3

Ordered: -6 5 8 15 32

I have written half the code and most likely will not compile.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

ifstream Input;				
ofstream Output;


void ReadValues(int Array[], int N)
{
	for(int i = 0; i < N; i++)
		Input >> Array[i];
}


void OrderValues(int Unordered[], int Index[], int N)
{	

	ReadValues(Unordered, N);
	ReadValues(Index, N);

	for(int i = 0; i < N; i++)
		for(int j = 0; j < N, j++)
			Ordered[i][j] = {{Unordered[i]}, {Index[j]}};
}

	
int main()
{
	Input.open("input.txt");
	Output.open("out.txt");
	
	int size;
	
	Input >> size;

	int *Unordered = new int[size];
	int *Index = new int[size];
	

	OrderValues(Unordered, Index, size);

	Input.close();
	Output.close();
	
	return 0;
}

What do you mean "most likely ", you didn't try to compile the code yet? What exactly is your question?

The code written in the function OrderValues will not compile. I just wanted to show that I had attempted at solving the problem.
My question: How do I arrange an array in ascending order without using swap or sort function (i.e. Bubblesort).

I have to use the values stored in the array Index as indexes for the array Unordered. If all goes well, the outcome should resemble the Ordered array.

I figured it out. I almost had it right, but i had it backwards.

void OrderValues(int Unordered[], int Index[], int Ordered[], int N)
{	

	ReadValues(Unordered, N);
	ReadValues(Index, N);

	for(int i = 0; i < N; i++)
	{
		Ordered[Index[i]] = Unordered[i];
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.