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!
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 .
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);
}
//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
#include <iostream>
#include <vector>
usingnamespace 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;
}
// 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); // <--- You have a "-" instead of a dot "."
}
And I'm a little bit confused... Where are you actually reading the vertex x_{1},x_{2},x_{3},y_{1},y_{2},y_{3},z_{1},z_{2},z_{3} ?
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.
Thank you very much for your correction and comments!
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.