Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code requires us to selection sort the input by the user then turn the selection sorted result into a 2d array by using functions. I have tried searching for it but haven't fully grasped the idea of converting a 1d to a 2d array. The 2d array requires us to have 4 rows and 5 columns.

#include<iostream>
#include<iomanip>

using namespace std;


void getInput(int list[],int dim);
void getSelect(int list1[], int dim);
void convert2DimSort(int list1[], int dim);

int main()
{	
	int dim = 20;
	int list[dim] = {};


	getInput(list, dim);	
	getSelect(list, dim);
	
	for(int i=0;i<20;i++)
	{
		cout<<list[i]<<" "; // how do i convert the result here to 2d array?
	}
	

	return 0;
}


void getInput(int list[], int dim)
{		
	cout<<"enter number: "<<endl;

	for(int i = 0; i<dim; i++)
	{
		cout<<"loc["<<i<<"] ";
		cin>>list[i];
	}
}

void getSelect(int list1[], int dim)
{
	for(int i = 0; i < 19; i++)
	{
		int min = i; 
		
		for(int j=i+1;j<20;j++)
		{
			if(list1[j]<list1[min])
			{
				min=j;
				
			}
		}
		if(min!=i)
		{
			int temp = list1[min];
			list1[min] = list1[i];
			list1[i] = temp;
		}
	}

}

void convert2DimSort
{
	
}
<pre lang="C++">


What I have tried:

I have no idea how to turn the result from a 1dimensional array to a 2dimensional array
Posted
Updated 3-Dec-21 8:58am
v2

To convert a 1D array to 2D, you need to decide on the sizes: for example a 20 element 1D array could be converted to a [2, 10], [4, 5], [5, 4], or [10, 2] 2D - so without knowing what your user wants, it's all a bit useless.

The mechanics are easy: allocate a new chunk of memory of the right size (probably with malloc, but it depends on your course so far) then use a nested loop to fill to it.

But I'd start by reading your homework assignment really carefully first - it probably tell you a lot more than you have shown us and it's almost certainly all relevant to the task!
 
Share this answer
 
Mathematically, the formula for going from a 1D-index to a 2D-index is:
// Given following populated arrays containing same data (initialization not shown):
T[] a1;
T[,] a2;
int N = a1.Length;
// The following yields true for any element:
bool test = a2[i / N, i % N] == a1[i];
 
Share this answer
 
Comments
Stefan_Lang 3-Dec-21 15:11pm    
I doubt this pseudo code will help someone who needs to ask for help on 1- and 2-dimensional arrays.

Your clever way of index conversion likewise is not a suitable answer to a beginner. Besides, you made a mistake there.
Technically, the clean way to copy the elements of a 1d array a to a NxM matrix m would be a nested loop over the rows and columns of the matrix:
C++
int arrayIndex = 0;
for (int row = 0; row < N; ++row) {
    for (int column = 0; column < M; ++column) {
        m[row][column] = a[arrayIndex];
        ++arrayIndex;
    }
}
As you can see, you not only need a double loop to iterate over the elements of the 2D matrix, you also need to maintain the index to the 1d array that you are copying from.

There are simpler solutions, but for now I suggest implementing this in your code and make sure it produces the desired results.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900