it. I am just stuck on a specific part in my code and am giving the assignment so that you are all clear on my goal)

Consider the following file called "raw_points.txt". The first line of this file contains a count of how many pairs of coordinates are specified in this file. The remaining lines contain pairs of (x,y) coordinates, one pair per line and separated by a space:

raw_points.txt

15

0 1.38

87 0

32 32

3.2 6.4

1.25252 0.680254

1.6877 0.137556

0.819814 1.75999

0.638961 1.96114

0.17001 1.81526

0.205019 1.84396

1.0151 1.74458

0.666519 1.38482

1.11389 0.72383

0.0639877 1.71702

0.197969 1.75414

If you calculate the radial length of each coordinate pair using the Pythagorean Theorem you get lengths ranging anywhere from 1.33 to 87. Let's normalize the numbers so that the radial length is always exactly 1.0.

As a rationale, imagine that we're wanting to convert the numbers into a table of cosine and sine values of the angle represented by each coordinate pair. This is useful in many situations - for example, many video games and computer graphics applications need "normal maps" that indicate the angle that each pixel on an image is "facing" - when lighting calculations are applied, this makes a flat picture of a rock surface look 3D due to the resulting highlights and shadows.

The formula to take a raw coordinate pair (x,y) and normalize it as (x',y') is simple:

x' = x / r

y' = y / r

where r is the radial length of (x,y) - which itself is:

r = sqrt( x2 + y2 )

Here's the same data but with normalized coordinate pairs:

normalized_points.txt

15

0 1

1 0

0.707107 0.707107

0.447214 0.894427

...

0.112146 0.993692

Notice how the numbers are still proportionally the same relative to one another - the polar direction stays the same but the polar distance becomes 1.0.

Assignment

Use Vim(or Notepad) to create the file "raw_points.txt" - copy the raw_points.txt contents from above, and paste the numbers in. Next, write a program to load the data from raw_points.txt, convert it, and save it as normalized_points.txt. Here are some guidelines.

• Appendix A at the end of this assignment lists some relevant file commands you'll need.

• Open the file "raw_points.txt" as an input filestream.

• Open the file "normalized_points.txt" as an output filestream.

• Read in the count of how many points there are from the input file.

• Write the count out to the output file (with an endl) and then repeat the following that many times:

o Read in an x value.

o Read in a y value.

o Calculate the radial length of (x,y).

o Divide both x and y by r.

o Write x and y to the output file, separated by a space and with a cursor return at the end.

• Close both the input and the output file.

Compile and run the program. Nothing should print to the screen - if it does, you're accidentally printing to the screen instead of to the output file. If you type "cat normalized_points.txt" you should see the same numbers as shown in the example above.

here is my code so far...

#include <iostream> #include <fstream> using namespace std; int main() { ifstream infile ( "raw_points.txt" ); ofstream outfile ( "normalized_points.txt"); int count; int i = 0; while (i < count) { i = i + 1; } return 0; }

What I need help doing is counting the amount of data given in the text file. And then reading them as x and y coordinates so that I can manipulate them and then send them to the out file.

Thank you in advance, let me know if you need any more information.

This post has been edited by **jimblumberg**: 20 November 2011 - 03:39 PM

Reason for edit:: Added missing Code Tags, Please learn to use them.