How do I get rid of trailing zeros?

I guess the title is pretty self-explanatory. I'm working on a project where I import a file that has a bunch of scrambled (x,y) points, and after reading it I pass it to a function that sorts them ascending order based on the x-coords. The function works like a charm, except for the fact that I have a bunch of trailing zeros when I export it to another file!
Also, I can't figure out how to get the following code to look like actual code, so it would be easier to read. The Format: <> (code) function doesn't work.

Anyway, back to the real question: how do I get rid of the trailing zeros?


void sortPoints(double points[][2], int count) //will put the points in order based on the x coordinate.
{
int n = 0;
//sort points based on the X value


for (int x = 0; x < count; x++)

{

for (int y = 0; y< count - 1; y++)

{

if (points[y][0]>points[y + 1][0])

{

double temp = points[y + 1][0];

points[y + 1][0] = points[y][0];

points[y][0] = temp;

}

}

}
for (int i = 0; i < count; i++)
{
if (points[i][0] != 0)
{
points[n++][0] = points[i][0];
}
}
while (n < count)
{
points[n++][0] = 0;
}
}
Last edited on
The Format: <> (code) function doesn't work.
You can enter the tags yourself:
[code]your code here[/code]

Anyway, back to the real question: how do I get rid of the trailing zeros?
Hard to say. The only clue given in the code posted so far is that you use values of type double. But there are no output or formatting statements shown here.

Perhaps you could show the relevant code where the values are output to the file, and some examples of the output you get, as well as the output you require.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 void writeInfo(double points[][2], int count, double min, double max, ofstream& fout, int& positionMax, int& positionMin) //will output the information that was requested about the function to the new file
{
	
	fout << "# Number of points: " << count << endl;
	fout << "# First point: " << points[0][0] << ", "<< points[0][1] << endl;

	//for (int i = 0; i < count; i++)
	//fout << "# Last point: " << points[i][0] << ", " << points[i][1] << endl;
	
	fout << "# Maximum found at position:" << positionMax << endl; 
	fout << "# XY point at position " << endl << endl;
	fout << "# Minimum found at position: " << positionMin << endl;
	fout << "# XY point at position " << endl << endl;
	
	for (int i = 0; i < count; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			fout << points[i][0] << ", " << points[i][1] << endl;
		}
	}
	

	fout.close();
}


and the output I'm given in the file is:


...6.24793, -0.266297
6.27397, -0.060437
6.27397, -0.060437
6.3, 0.444116
6.3, 0.444116
0, 0.685259
0, 0.685259
0, 0
0, 0
0, 0
0, 0...


I'm not sure what else to show. I'm at a loss here.
Thanks for the extra code and sample data. To begin with I was a little confused - which is easily done ;) . When you said trailing zeros, I thought you meant something like
6.27397, -0.060437
6.30000, 0.400000
0.00000, 0.600000

which is of course a matter of the way the numbers are displayed/formatted.

But now it's clear that the problem is something else, it is the actual values being stored/output.

So far I don't have an answer, not had time to look at it again from this angle yet.
Now I've had another look, I see a fairly obvious omission/error in the code. That is when sorting, only the x-values are exchanged, but the y-values are left unchanged. That looks like a mistake to me.

But also, these lines leave me baffled. Though I can understand the code, I've no idea what its intended purpose might be.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    int n = 0;
    
    for (int i = 0; i < count; i++)
    {
        if (points[i][0] != 0)
        {
            points[n++][0] = points[i][0];
        }
    }
    
    while (n < count)
    {
        points[n++][0] = 0;
    }



I'd suggest sortPoints() might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// will put the points in order based on the x coordinate.
void sortPoints(double points[][2], int count) 
{
    // sort points based on the X value

    for (int x = 0; x < count; x++)
    {
        for (int y = 0; y< count - 1; y++)
        {
            if (points[y][0]>points[y + 1][0])
            {
                double tempa     = points[y + 1][0];  // X value
                double tempb     = points[y + 1][1];  // Y value
                
                points[y + 1][0] = points[y][0];      // X value
                points[y + 1][1] = points[y][1];      // Y value
                
                points[y][0]     = tempa;             // X value
                points[y][1]     = tempb;             // Y value
            }
        }
    }
}

Thank you so much!! I took your advice and was able to successfully complete the project. Thank you!
Topic archived. No new replies allowed.