Umut Keskin
Umut Keskin

Reputation: 37

how to get max values from each string in a text file - C++

I have a text file like this;

1 13 330 323 18 1 40 410 413 45 1 28 381 347 16 1 16 230 261 27
2 6 208 218 8 2 24 253 277 21 2 13 223 244 14 2 10 177 185 6
3 0 12 1 1 3 20 417 416 18 3 23 322 320 23 3 5 21 23 4
4 1 7 18 2 4 11 149 138 11 4 11 120 116 10 4 2 27 24 3

and i want to take each string's maximum value. For example, in 1st string, i have 413 for highest number, for the 2nd i have 277. And i have 40 lines like this. I used this code but my code doesn't working properly - i knew i do it wrong btw- it takes all of the arrays and takes just only 1 highest value. I think i need two for loops for doing this but i already done first wrong and there in no 2nd one :) Maybe this can be done with "getline" function i really don't know but i need your help atm... Thanks.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <string.h>

using std::cin;
using std::endl;
using std::cout;
using namespace std;

int main()
{
    int a[20][40]; 
    int x,y;  
    int sum = 0;
    FILE *myDataFile1;  
    ofstream myOutFile1;
    myOutFile1.open ("highestValues.txt"); 

    myDataFile1 = fopen("input.txt", "r");

    for ( x = 0; x < 20; x++)
    {
        for ( y = 0; y < 40; y++)
        {
            a[x][y] = 0;
        }
    }


    for (x = 0; x < 20; x++)
    {
        for ( y = 0; y < 40; y++)
        {
            fscanf(myDataFile1, "%d,", &a[x][y] );
        }
    }

    for (x = 0; x < 20; x++)
    {
        for ( y = 0; y < 40; y++)
        {
            sum = a[x][y];
        }
    }

    int maxValue = 0;


    for(x = 1; x < 20; x++)
    {
        for(y = 1; y < 40; y++)
        {
            if(a[x][y] > maxValue)
            {
                maxValue = a[x][y];
            }
        }
    }

    if (myOutFile1.is_open())
    {
        myOutFile1 << left << setw (5) << maxValue << endl;
    }
    cout << "The highest value is: " << maxValue << endl;
}

}

Upvotes: 0

Views: 2378

Answers (4)

marcinj
marcinj

Reputation: 49986

One possible solution:

std::fstream fs("test.txt");
std::string line;
while(std::getline(fs, line)) {
    std::stringstream str(line);
    std::istream_iterator<int> begin(str), end;
    std::vector<int> vec(begin, end)
    std::cout << *std::max_element(vec.begin(), vec.end()) << std::endl;
}

[edit]

Your code is more C - like, C++ version (as you have tagged your question) looks as above code.

headers you can find on http://en.cppreference.com/w/, to make this answer complete I add them below:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <fstream>

Here is a Live Example

Upvotes: 3

Sahil Arora
Sahil Arora

Reputation: 885

You made a matrix of 20X40, so I presume you already know the number of numbers in one line. What you are doing wrong is your matrix size and indexing. Your input has 40 lines,so rows = 40, and 20 integers in a line, so 20 columns.

Now your matrix looks like:

int a[40][20];

Now just read integers in the matrix using for loop and fscanf or ifstream.

Here is your new code:

    #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <string.h>

using std::cin;
using std::endl;
using std::cout;
using namespace std;

int main()
{
    int a[40][20]; 
    int x,y;  
    int sum = 0;
    FILE *myDataFile1;  
    ofstream myOutFile1;
    myOutFile1.open ("highestValues.txt",ios::out); 
    myDataFile1 = fopen("input.txt", "r");
    for ( x = 0; x < 40; x++)
        for ( y = 0; y < 20; y++)
            fscanf(myDataFile1,"%d ",&a[x][y]);       //removed the comma, since your integers are not comma saperated
    for (x = 0; x < 40; x++)
    {
        int maximum = a[x][0];
        for ( y = 0; y < 20; y++)
            maximum = max(maximum,a[x][y]);
        myOutFile1 << "Maximum for line "<<x<<": "<<maximum << endl;
    }    
    fclose(myDataFile1);
    myOutFile1.close();
    return 0;
}

Upvotes: 0

QuestionC
QuestionC

Reputation: 10064

It looks like your main problem is in reading the file. I suggest starting with a framework like this instead of hardcoding the numbers 20 and 40.

#include <fstream>
#include <sstream>
#include <iostream>

int main (void)
{
    std::ifstream infile("input.txt");

    std::string line;
    while (std::getline(infile, line))  // std::getline returns infile, which evaluates to false at the end of the file.
    {
        std::cout << "\nNew Line.\n";

        std::istringstream iss(line); // Turn the line into its own stream
        int a;
        while (iss >> a)  // iss evaluates to false at the end of the stream
        {
            std::cout << a << " ";
            // You can find the max element here.
            // Exercise left for the reader.
        }
    }

    return 0;
}

Read file line by line was very helpful in making this answer, I suggest giving it a read too.

Upvotes: 0

Eksapsy
Eksapsy

Reputation: 1766

Firstly, decide if you want to use C++ or C in your code. Don't mix both, it just looks ugly. ifstream is for input. ofstream is for output. ">>" and "<<" operators do work for them as well and that's why C++ is comfortable. I suppose each line has 20 integers right ? So what you have to do is to make 3 loops for the 3 lines and add each integer into an array[n] where 0<=n<=3 is the line. In code :

ifstream input;
int max[3] /*Because you have 3 lines in your quote*/, a; 
input.open(...);
/* don't forget max[n] iniliazation */
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 20; j++)
        input >> a;
        max[i] = max[i] < a ? a : max[i];

And there you have for each line the maximum value

Upvotes: -1

Related Questions