| | |
Homework HELP!!urgent!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
Well, and what's your problem?
May be better start from two wonderful ballades on this forum rules?..
http://www.daniweb.com/forums/thread78223.html
http://www.daniweb.com/forums/announcement118-2.html
May be better start from two wonderful ballades on this forum rules?..
http://www.daniweb.com/forums/thread78223.html
http://www.daniweb.com/forums/announcement118-2.html
Last edited by ArkM; Nov 5th, 2008 at 5:37 am.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
•
•
•
•
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
2) start from i=0 and loop while i<count, incrementing i at every passage
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
lol, this person must be in my class. I hate to sound like an *******, 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.
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.
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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(
) is a unit vector.
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(
![]() |
Similar Threads
- infix to postfix ( homework help - Urgent ) (C++)
- java doubt - urgent (Java)
- URGENT!: Saving Database information from ASP.NET Button (ASP.NET)
- can anyone help me!! urgent (C)
- Urgent Help Needed!!!! Plzzz (Java)
- URGENT: Need help on I/O code! (Java)
- Urgent help (Networking Hardware Configuration)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Hash Table Confusion
- Next Thread: I implemented a String, how well or how bad it is implemented?
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






