0

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;
}

}

aslg
  • 1,966
  • 2
  • 15
  • 20
Umut Keskin
  • 37
  • 1
  • 10
  • 3
    You can do this without the magic numbers of `20` and `40` in your code, and much shorter also -- that is, if you *really* want to do this in C++ (using istringstream, and STL). – PaulMcKenzie Mar 14 '16 at 16:29
  • make a list of integers, do a readline, split your string by space character, store in that list and get max – Pankaj Mar 14 '16 at 16:29
  • there is no magi numbers lol there is numbers representing my textfile's array i have 20 numbers in x line and 40 numbers in y line. I am trying to read text file with arrays because i am a newbie and just know this kind of reading :( – Umut Keskin Mar 14 '16 at 16:32
  • Those numbers are magic. What if your next text file contains only 10 lines and 15 numbers? Are you going to rewrite your program using 10 and 15? – PaulMcKenzie Mar 14 '16 at 16:35
  • i am just trying to do read my first line with 20 numbers with an array than the other one than the other one etc. i have 40 rows like this so i have 800 numbers total but i want to take each string's highest number alone and write it so i need 40 numbers at the end and also when i do with it my "magic numbers", i just taking all of the text file's biggest number which is 814 and it is on the 40th string. I just want to take each string's highest number alone. Just this :( – Umut Keskin Mar 14 '16 at 16:48
  • @UmutKeskin -- You're still not understanding my point. Look at the answer that was posted. You don't see 20 or 40 in there. It will work with 20, 40, 100, 1000, 1, 14, any number of rows or columns. That is what you should be striving for -- write the program once and not worry if tomorrow you will need to read 10 rows, or 1000 rows, or 1 row, etc. – PaulMcKenzie Mar 14 '16 at 16:51

4 Answers4

3

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

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Just a little quibble: I wouldn't use `eof` for something that isn't the end of a file. I'd probably use `begin` and `end` as the names of those iterators, because of the parallel with `vec.begin()` and `vec.end()`. – Pete Becker Mar 14 '16 at 16:39
  • ‘istream_iterator’ is not a member of ‘std’ std::istream_iterator inf(str), eof; ^ – Umut Keskin Mar 14 '16 at 16:43
  • i never used code like your solution, so i don't know which classes i need and get many errors – Umut Keskin Mar 14 '16 at 16:44
  • @UmutKeskin - yes, you need to figure out which headers to `#include`. There are lots of resources on line for that. Look it up! – Pete Becker Mar 14 '16 at 16:45
  • @marcinj - much better! – Pete Becker Mar 14 '16 at 17:07
  • Ty i just found them online as u said @PeteBecker. Ty for your solution. I also look these classes online but i didn't found a solution about taking 2nd and 3rd highest number in those strings too. Any help will be good too. Thanks a lot... – Umut Keskin Mar 14 '16 at 17:21
  • @UmutKeskin see this SO: http://stackoverflow.com/questions/30488586/stdmax-element-for-second-largest-element – marcinj Mar 14 '16 at 17:42
  • @marcinj , i take a look at this link but when i try to get 2nd element with like this, i get many errors, is there any other way to get 2nd, 3rd, 4th and 5th element and get the max one (i already get max with your helps). – Umut Keskin Mar 15 '16 at 18:27
  • @UmutKeskin you can always use `std::sort` on `vector` and choose n-th element from the end to find n-th largest value – marcinj Mar 15 '16 at 18:37
0

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.

Community
  • 1
  • 1
QuestionC
  • 10,006
  • 4
  • 26
  • 44
0

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;
}
Sahil Arora
  • 875
  • 2
  • 8
  • 27
-1

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

Eksapsy
  • 1,541
  • 3
  • 18
  • 26
  • What do you mean by "magic numbers" ? Also sorry I had some things wrong in my answer, fixed them. – Eksapsy Mar 14 '16 at 17:00
  • *What do you mean by "magic numbers" ?* -- Look at the other answer that uses `stringstream`. You see no 20 or 3 in that answer. It will work, regardless of the number of lines or numbers in each line. – PaulMcKenzie Mar 14 '16 at 17:05