# counting amount of data being inputted from a .txt file

Page 1 of 1

## 6 Replies - 951 Views - Last Post: 21 November 2011 - 12:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256487&amp;s=b119886c4add256f8ce4ccdd13e5acfb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 slipek187

Reputation: 0
• Posts: 4
• Joined: 20-November 11

# counting amount of data being inputted from a .txt file

Posted 20 November 2011 - 01:14 PM

here is my assignment... (note, I am not asking anyone to complete the assignment for me, although I would not object to
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.

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.

Is This A Good Question/Topic? 0

## Replies To: counting amount of data being inputted from a .txt file

### #2 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: counting amount of data being inputted from a .txt file

Posted 20 November 2011 - 05:30 PM

So simply read the number from the file.

```    int i = 0;

infile >> count;

while (i < count)
{

```

### #3 slipek187

Reputation: 0
• Posts: 4
• Joined: 20-November 11

## Re: counting amount of data being inputted from a .txt file

Posted 21 November 2011 - 10:40 AM

#define, on 20 November 2011 - 05:30 PM, said:

So simply read the number from the file.

```    int i = 0;

infile >> count;

while (i < count)
{

```

Thank you very much. Any help with the rest of this code would be very helpful! thanks in advance.

### #4 jimblumberg

Reputation: 5419
• Posts: 16,896
• Joined: 25-December 09

## Re: counting amount of data being inputted from a .txt file

Posted 21 November 2011 - 10:57 AM

You need to show some actual effort to solve your problem. Ask specific questions based on code you supply. No one here is going to do this assignment for you and the code you have provided so far does not show much actual effort.

Jim

### #5 slipek187

Reputation: 0
• Posts: 4
• Joined: 20-November 11

## Re: counting amount of data being inputted from a .txt file

Posted 21 November 2011 - 12:45 PM

jimblumberg, on 21 November 2011 - 10:57 AM, said:

You need to show some actual effort to solve your problem. Ask specific questions based on code you supply. No one here is going to do this assignment for you and the code you have provided so far does not show much actual effort.

Jim

I promise you I have put so much effort into this. Unfortunately our instructor is not giving us the proper resources to complete the assignments given to us. He is actually under review from the board of educators at my college and it is only his first year. This is only a intro to programming class and he assumes we all have programming experience and only brushes the ideas. But besides that point, me and many others in the class have spent hours and hours trying to figure this out. This is what we have come up with.

```#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream infile ( "raw_points.txt" );
ofstream outfile ( "normalized_points.txt");

int pts(0), sum=1;
double count, x, y;
infile >> count;
ifstream raw_points;
ofstream normalized_points;
raw_points.open("raw_points.txt");
ifstream raw_points("raw_points.txt");
if( !raw_points )
{ cerr << "File raw_points.txt could not be opened";
exit(1) }
raw_points >> x >> y;
return 0;
}

```

I am getting some errors and still unsure about how to modify the x and y variables once they have been read from the input file and then to send those new variables to the out file.

Thanks in advance for any assistance.

This post has been edited by jimblumberg: 21 November 2011 - 12:49 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

### #6 slipek187

Reputation: 0
• Posts: 4
• Joined: 20-November 11

## Re: counting amount of data being inputted from a .txt file

Posted 21 November 2011 - 12:52 PM

I appreciate all of your help (ish =P ). But I figured it out in a much simpler way. Thread can be closed.

### #7 jimblumberg

Reputation: 5419
• Posts: 16,896
• Joined: 25-December 09

## Re: counting amount of data being inputted from a .txt file

Posted 21 November 2011 - 12:54 PM

You may want to study this link: Basic File Input and Output. And this link: Control structures.

Jim