I have no idea ho to write a program for this problem and i am new to C++ so I really needhelp!!

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
Make a proj8 directory. Use Vim to create the file "raw_points.txt" - copy the raw_points.txt contents from above, start editing a file by the same name, go into insert mode, and type SHIFT+INSERT to 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:
Read in an x value.

Read in a y value.

Calculate the radial length of (x,y).

Divide both x and y by r.

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.

Also have your program work with command line arguments instead of "hard-coding" the filenames in. You should be able to type:
./proj8 raw_points.txt normalized_points.txt

and have the program figure out what two filenames you typed as you ran the program. Note that the specific filenames "raw_points.txt" etc. should no longer be present in your source code.

Recommended Answers

All 7 Replies

What I need to know is do i have to open both the files in the program and what conditions i have to use in the while loop.

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

1) Yes you do
2) start from i=0 and loop while i<count, incrementing i at every passage

Answer#1: Look at the assignment. Of course, you must open file before use (read or/and write) it. There are two files in your assignment...

Answer#2 - counter-question: where is that while loop? No loop - no condition...

Apropos (about the original post): in my opinion, no ideas - no problems...

Member Avatar for iamthwee

I still see no code from the OP.

Code would be a nice start.

lol, this person must be in my class. I hate to sound like an asshole, and honestly the reason i found this forum site was looking up code for this exact assignment, but the instructions that were given to us actually give us the code for what is being asked here.

but im a nice person, so here is what i have for you. sorry if im breaking forum rules here, but this assignment is definitely a pain in the %^&* for the lack of teaching skills.

int main()
{
ifstream infile( "raw_points.txt" );
ofstream outfile( "normalized_points.txt" );
int c=0;
double n;
string line;


while(!infile.eof()) {
getline (infile, line);
c++;
}
outfile<< c<< " lines"<< endl;


for (x = 0; x<= c; x++)
{


}

That will read in the number of lines from the file and output the number of lines to the output file.

back to trying to find out what exactly this normalize bit means.

This just doesn't work, your read the file into line one line at a time
and each time overwrite the previous line before actually doing anything.

Move the } below c++; down one line but still it doesn't do much.
(other than actually print
1 lines
2 lines ....


Normalize means divide a vector by a number such that the length of the vector is 1.0. A vector with length 1 is called a unit vector.
It is applicable to all dimensionality, e.g. vector([tex]3/4,-1/4,\sqrt{6}/4[/tex]) is a unit vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.