### Making array with Uknown size and order it

Dear Users and developers

I want to make an array from Unknown size and order ascending order.

As shown in the following peace of code, the number n is always between (1 to 4) which depend on the if functions. i want to make an array with n and order it. But how i can do it! I Google somehow but couldn't find the proper information.

for(i = 1; i < aNodes.Length()+1; i++)
{

for( nt = 1 ; nt < nnn+1 ; nt++) {

int n;

if (P1.X()<L1 && P1.Z()<zmax/2) {

int n=1;

}

else if (L1 < P1.X()<=L2 && P1.Z()<zmax/2) {

int n=2;

}

else if (L2 <P1.X()<=L3 && P1.Z()<zmax/2) {

int n=3;

}

else {

int n=4;

}

// Here I want to create an array
int j;
int num[j]=n;
int holder;
for (j=0, j<=max, j++)

if(num[j]>num[j+1]) // comparison between two adjacent array elements
{
holder=num[j]; // holder holds the value temporarily
num[j]=num[j+1];
num[j+1]=holder;
}

// further calculation
}
}

In many examples that i have browsed, the size of an array should be known. Any help, it doesn't as i tried!

With best regards

Game
Use the code tags ( <> ) its difficult to read your code .
Is this what you want ?
Declare and array of unknown size, unknown size comes from the user ?
also the elements in the array come as user input and then the array is arranged in the ascending order .
If you want to create an array of an unknown size at compile time, then you need to create one on the heap i.e. at run time:

 `` `` ``char* pszString = new char[size+1];``

Then if you want to order it, then you can still use the stl functions provided in the algorithm header:

 ``1234567891011121314151617`` ``````#include #include using namespace std; int main() { char* pszString = new char[200]; strcpy_s(pszString, 200, "onmlkjihgfedcba"); sort(pszString, pszString+strlen(pszString)); // dont't forget to delete the memory we // allocated on the heap: delete[] pszString; return 0; }``````

HTH

Andrew
Last edited on
just use a vector, you don't need to care about the size and it comes with some helpful functions.

also you can simply call std::sort as described here: http://www.cplusplus.com/reference/algorithm/sort/
 just use a vector

But, he specifically stated that he wanted an array. I know that technically a vector is an array, but ...
if thats the case use an array, std::sort also works with arrays

Thank you guys,

It seems using vector is better, I will look and come up with my result,

Thank you again,

If you have more tutorials or examples, let me know, regards

Game

Input

10 2 3 34 55 65 67 76 23
56 9 5 29 19 29 19 29 19
56 23 23 43 45 12 56 43 12
100 234 123 12 12 34 32 45 46
.
.

so on (unknow size) means it is dynamic

intermidiate output add one column at the end
10 2 3 34 55 65 67 76 23 1
56 9 5 29 19 29 19 29 19 3
30 23 23 43 45 12 56 43 12 2
45 9 10 29 19 29 9 19 29 3
100 234 123 12 12 34 32 45 46 4

Then i want to sort in the following way based on the last column (10th column)

10 2 3 34 55 65 67 76 23 1
30 23 23 43 45 12 56 43 12 2
45 9 10 29 19 29 9 19 29 3
56 9 5 29 19 29 19 29 19 3
100 234 123 12 12 34 32 45 46 4

Using the above matrix, i want to calculate the normal to triangle ans so on

I try to write as follow, Here i am posting this part only! Because i am using other libraries before using the vector here below!
#include <iostream>
#include <vector>
using namespace std;

int main() {

// Here other program module which generate

std::vector<vector<int> > ph;
std::vector <int> p;
int tr= numberoftriagle ();
for (int j=0; j<=tr; j++) // this loop is from above program part and gives the value of nine verticies of triagle in each loop!

//push_back all vertices to verctor p
p.push_back(x1);
p.push_back(y1);
p.push_back(z1);
p.push_back(x2);
p.push_back(y2);
p.push_back(z2);
p.push_back(x3);
p.push_back(y3);
p.push_back(z3);

// depending on the following if conditions the tenth vector is added
if ((x1<20) && (z1<62))
{
p.push_back(1);
}
if ((20<=x1<=40)&& (z1<60))
{
p.push_back(2);
}
if((40<x1<60) && (z1<20))
{
p.push_back (3);
}
else
{
p-push_back(4);
}

// then push back vector p to ph

ph.push_back(p);

for(int i=0; i<ph.size(); i++)
{

cout<<ph[i][0] << " " <<ph[i][1] " "<<ph[i][2]<<" "<<ph[i][3] << " " <<ph[i][4] " "<<ph[i][5]<<" "<<ph[i][6] << " " <<ph[i][7] " "<<ph[i][8]<<" "<<<<ph[i][9] " "<<ph[i][10]<<endl;

//Here I want to sort based on the last column (ph[i][10])
int temp;
if (ph[i][10] >ph[i+1][10])
{
temp=ph[i][10];
ph[i][10]=ph[i+1][10];
ph[i+1][10]=temp
}
}

//to make some calculation with sorted vector

for (int j=0; j<=ph.size(); j++)
{
//to make some calculation here
}
return 0;

}

I tried this way, but i am not sure if it gives me what i wanted to do! I am really very new to use vector!

Would you give a recommendation! For above code, i got an error message

../src/vector.cpp: In function ‘int main()’:
../src/vector.cpp:37:15: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
../src/vector.cpp:41:13: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
../src/vector.cpp:47:17: error: ‘push_back’ was not declared in this scope
../src/vector.cpp:54:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
../src/vector.cpp:57:36: error: expected ‘;’ before string constant
make: *** [src/vector.o] Error 1

Any help and suggestion,

thank for going through in advance,

I hope i make it clear

please edit your post and use codetags "<>" for the parts that contain sourcecode
Last edited on

Dear Darkmaster

Here below!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include #include using namespace std; int main() { // Here other program module which generate std::vector > ph; std::vector p; int tr= numberoftriagle (); for (int j=0; j<=tr; j++) // this loop is from above program part and gives the value of nine verticies of triagle in each loop! //push_back all vertices to verctor p p.push_back(x1); p.push_back(y1); p.push_back(z1); p.push_back(x2); p.push_back(y2); p.push_back(z2); p.push_back(x3); p.push_back(y3); p.push_back(z3); // depending on the following if conditions the tenth vector is added if ((x1<20) && (z1<62)) { p.push_back(1); } if ((20<=x1<=40)&& (z1<60)) { p.push_back(2); } if((40ph[i+1][10]) { temp=ph[i][10]; ph[i][10]=ph[i+1][10]; ph[i+1][10]=temp } } //to make some calculation with sorted vector for (int j=0; j<=ph.size(); j++) { //to make some calculation here } return 0; }``````
i dont understand what the aim of your code is, but since you have problems with sorting a vector have a look at:
http://www.cplusplus.com/reference/algorithm/sort/

I look at this code! But it doesn't helped me, as I have multiple array type vector!

My sorting depend only on the last column of the vector!

Any other Hint:::::::
in that case you have to do it on your own or save the column to a temp vector, which you only use for std::sort.

closed account (D80DSL3A)
You are swapping one element on lines 58-60.
Why stop there? Use a for loop to swap them all and the rows would be swapped.

Also, I count 10 push_backs (not 11), so the high index value = 9, not 10.
Firstly,
 ``1234567891011121314151617`` ``````// depending on the following if conditions the tenth vector is added if ((x1<20) && (z1<62)) { p.push_back(1); } if ((20<=x1<=40)&& (z1<60)) { p.push_back(2); } if((40

And I'm a little bit confused... Where are you actually reading the vertex x1,x2,x3,y1,y2,y3,z1,z2,z3 ?

If you want my help, you have to provide the code. No one here is interested in copying someone's code. Almost everyone here has enough knowledge to be able to develop what you've done by themselves. So please provide the full code. The problems you have might be somewhere else than what you showed us.

Best of wishes,
~ Raul ~
Last edited on

Dear Guys ,

Sorry for late reply, i was away this days. Now i am back,

To: Raul, i am not trying to hide my code! I am using another library to manipulate geometric model, and finally to extract points and different regions.
1. The reason i posted part of the program is that! One the whole program is very big and out of regular c++ syntax. I thought not necessary to post here.

Anyhow i will post, at-least the earlier loop to create vertexes!

Let me include your comments and if it doesn't work, i will post it.

Thank you guys again.

Game
Topic archived. No new replies allowed.