Sir,
I have 2 column having so many points. In the 2nd column there r 3 highest peak points like(6,4 and3) bellow

                             6
                           5   5
                          4     4             4
                         3       3          3   3                  3    
                        2         2       2       2               2  2
                       1           1     1         1             1    1
                   2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 

How can i peak up these 3 points(6,4 and 3) and corresponding 1st column value like(7,14,21)above.
By the help of expert brother, i got the following code to input 2 column from txt file, is given bellow:
here test1 and test2 is 2 column. I am really waiting for it...

#include <vector> //Include the header for the vector data type
#include <iostream> //Accept input and give output.
#include<fstream>

using namespace std; //Using the std namespace for coding simplicity

int main(){
    vector<double> test1,test2;
    double num;
    int i=0,j;
    ifstream infile;
    infile.open("input.txt");

while(true){
    //First column
    infile>>num;
    if(!infile.good()) break; //If it's not good break.
    if(infile.eof()) break; //If it's the end of the file break.
    test1.push_back(num); //Back of the line for you, mister!

    //Second column
    infile>>num;
    if(!infile.good()) break; //If it's not good break.
    if(infile.eof()) break; //If it's the end of the file break.
    test2.push_back(num); //Back of the line for you, mister!
    i++; //And one more row please.
    }

//Print them back out switched just for variety.
for(j=0;j<i;j++){
cout<<test2[j]<<"\t"<<test1[j]<<endl; //Notice I'm printing test2 first, then test1.
}
test1.clear(); //Clean up and free up!
test2.clear();
infile.close();
return 0;
}

Recommended Answers

All 12 Replies

Well, I have something that might help you. This will get the last character of each line, which, by your columns, should be the heighest in that line, and will get it's position from the last line, which presumably defines the "columns".
Note: This applies only if the file is as you posted, with the columns decreasing e.g. 6 -> 4 -> 3. If the file has another format, it won't work, e.g. if the file is like 4 -> 3 -> 6.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
    string filename = "clm.txt";                                       //file from which you'll read
    vector<string> lines;                                              //will store all the lines from the file
    vector<char> numbers;                                              //will store the numbers from that line
    vector<pair<char, string> > heights;                               //will store the pairs number - column position
    ifstream fin (filename.c_str(), ios::in);
    string line;
    if (fin.good()){                                                   //check if the stream is good
        while (getline(fin, line)){                                    //gets the line from the file
            string::reverse_iterator rit = line.rbegin();                                                             
            while (*(rit) != '\n' && *(rit) == ' ') rit++;             //checks where's the last character, 
                                                                       //which isn't a new line or a whitespace
            numbers.push_back(*(rit));                                 //store the corresponding number
            lines.push_back(line);                                     //store the entire line
        }
    }

    int size = (int)lines.size()-1;                                    //get the size of the lines vector (to
                                                                       //actually see the position of the column line)
    for (int i=0;i<size;i++){                                          //iterate over the vector
        string part;
        int pos = (int)lines[i].rfind(numbers[i]);                     //get the last position in the line where the number
                                                                       //is encountered.
        if (lines[size][pos] == ' ')                                   //if the position in the column line is a whitespace, 
             while (lines[size][pos++] != ' ');                        //than increase the position until a non-white space
                                                                       //character is fount
        while (lines[size][pos] != ' ')                                //if the number is greater than 9, it is composed
            part.push_back(lines[size][pos++]);                        //of multiple characters. get them all!

        heights.push_back(pair<char, string>(numbers[i], part));       //save the pair number - position
    }

    for (int i=0;i<(int)heights.size();i++){
        cout<<heights[i].first<<" : "<<heights[i].second<<endl;
    }
    return 0;
}

This is just an example. Now you'll have to find a way to get your desired positions. Also, the file I used looks like this:
clm.txt:

                             6
                           5   5
                          4     4             4
                         3       3          3   3                  3    
                        2         2       2       2               2  2
                       1           1     1         1             1    1
                   2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 

And the output:

6 : 7
5 : 8
4 : 14
3 : 21
2 : 22
1 : 22

I input these following column, I don't know, what happen, just some un readable thing came and some , horrible sound made me afraid....

1 0
2 0
3 0
4 1
5 2
6 3
7 4
8 5
9 6
10 5
11 4
12 3
13 2
14 1
15 0
16 1
17 2
18 3
19 4
20 3
21 2
22 1
23 0
24 1
25 2
26 3
27 2
28 1
29 0
30 0
31 0
32 0

(9,6), (19,4) and (26,3) are peaks

could you please say what happened?

Is this:

1 0
2 0
3 0
4 1
5 2
6 3
7 4
8 5
9 6
10 5
11 4
12 3
13 2
14 1
15 0
16 1
17 2
18 3
19 4
20 3
21 2
22 1
23 0
24 1
25 2
26 3
27 2
28 1
29 0
30 0
31 0
32 0

the file with the items, or are they presented like this:

                             6
                           5   5
                          4     4             4
                         3       3          3   3                  3    
                        2         2       2       2               2  2
                       1           1     1         1             1    1
                   2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 

If the file contains only two columns, the position and the number follow this pseudocode:

peeks = vector of pairs
tempNumber = tempPos = pos = number = 0
while stream is good do
    tempPos = first number from stream
    tempNumber = second number from stream
    if number <= tempNumber then
        number = tempNumber
        pos = tempPos
    else
        peeks.push_back (pair of number, pos)
        while stream is good do
            tempPos = first number from stream
            tempNumber = second number from stream
            if number < tempNumber then
                exit_loop/break
            end_if
            number = tempNumber
        end_while
    end_if
end_while

So you start from a "valley", and you "climb" to a "peak/summit". If your tempNumber is smaller than the number, than you've reached a peak, and you'll "descend" until you find yourself in another "valley", or the end of the file. So, you'll start "descending" and, when you've reached the lowest point, when number isn't smaller than the tempNumber, than you'll start "climbing" again, and on that moment, you redo the whole algorithm, until you have found all the peaks.

Member Avatar for iamthwee

hmm
2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Funny how the line doesn't start with one and ten is represented as a 0. The double digits also add an extra degree of complexity. For that reason alone, I would consider representing and storing this as a 2 dimensional array. Then I don't think you can go too far wrong.

To Iamthwee
actually that was my mistake.
the numbering starts from 0 and
position ten, this should not be 0, it is 10.
Can you consider folloing two column please and make it easy to compile !!!

1 0
2 0
3 0
4 1
5 2
6 3
7 4
8 5
9 6
10 5
11 4
12 3
13 2
14 1
15 0
16 1
17 2
18 3
19 4
20 3
21 2
22 1
23 0
24 1
25 2
26 3
27 2
28 1
29 0
30 0
31 0
32 0
as the 1st column goes high value the 2nd column gives 3 peak at 1st column position 9,19 and 26.

Dear
Lucaci Andrew, I am really new to programing, I understood that you tried for me a lot. Can you now, give me an example about how to put your 2nd coding with my 2 coloum, and for you kind information, column may have fraction/decimal values. these colum is bellow...

1 0
2 0
3 0
4 1
5 2
6 3
7 4
8 5
9 6
10 5
11 4
12 3
13 2
14 1
15 0
16 1
17 2
18 3
19 4
20 3
21 2
22 1
23 0
24 1
25 2
26 3
27 2
28 1
29 0
30 0
31 0
32 0

There's not much to be explained, since I gave you the pseudocode. Again, go throughout your file and see when you've "hit" a "peak". You'll notice at some point that your previous number will not be greater than your current, so you actually found a peak.
Your brother gave you some methods of how to get the numbers from your file, I'll go with that.
So, as to initalizing the variables, I've choosen to get a pair of two ints, which represents the number and the position, and I'll be storing these pairs in a vector:

vector<pair<int, int> > peaks;

If you don't know how to tackle pairs, here's an useful link: Click Here
Also, I've mentioned something related to tempPos and tempNumber, you should initialized them as follows:

int pos = 0, number = 0, tempPost = 0, tempNumber = 0;

You'll be needing the ifstream object to actually read from the file; my file is called cln.txt

string filename = "cln.txt";
ifstream fin(filename.c_str(), ios::in);

Now you have your basic elements from the top of the pseudocode. What's next is the iteration of the while:

while (fin.good())

That assures that your stream (fin) is good, and ready to be read from. On how to actually get the numbers, your brother gave you some heads up to this matter:

fin>>tempPost>>tempNumber;

As you probably noticed by now, you are extracting the first 2 numbers from the stream (that is, your columns from your the file). If you are unclear about the extracting operator>> have a look at this: Click Here
Following the pseudocode, you now have entered the while, and populated your temp variables with data from your text file. The next thing is the if contition:

if (number <= tempNumber) {
    number = tempNumber;
    pos = tempPost;
}

That says pretty much this: if your number is smaller than the tempNumber, you assign the current number to be tempNumber: you're "climbing to a peak". You're also storing the position, since it's needed. Onto the second part of the if condition, the else part:

else {
    peaks.push_back(pair<int, int>(number, pos));
    while (fin.good()){
        fin>>tempPost>>tempNumber;
        if (number < tempNumber) break;
        number = tempNumber;
    }
}

So, you have found a "peak". You'll be storing it into your "peaks" vector, and you'll descend until you are in a "valley", that is the second while. Since the elements from your descending are not a concern to you, you actually keep track of your number and your tempNumber to see when you've reached the "valley", but you ignore the position. When you indeed got in a "valley", the algorithm repeats, until you've reached the end of the file.

I pretty much gave you the solution here, but I do hope you'll understand something from this.

Dear
Lucaci Andrew, I guess you think I am little bit good in programing, But no i am not. even I can't get how to combine your coding to my first coding....ohhhh feel really sorry. Can you give me now a complete post!!! otherwise this really uselees diamond to me !!!

Member Avatar for iamthwee

@siddiquedu

What you're asking for is a complete freebie answer that you can just hand in to your teacher. Sorry, but we've been here long enough to know all the tricks:

'Can someone please complete this program to show how brainy you are.'
'Please give me the complete code... I will pay you.'

...Ad nauseum...

Sorry it's not going to happen. Lucaci probably provided more than I would be willing to give. My advice would be to actually try DOING this for YOURSELF.

That's not to say we won't help if you get stuck. At the moment you've showed no effort whatsoever, except paste up a piece of code from 'an expert brother.'

otherwise this really uselees diamond to me !!!

It won't be if you start thinking a bit it, gluing all the pieces of code I gave you. I cannot go further with my explanations, since you haven't provided any actual work in solving this problem. Indeed, you posted some code ripped off your 'expert brother', but I fail to see your actual work.

Thanks sir @ Lucaci Andrew

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.