0

Background:

I'm attempting to read through a 2d array and find values that coincide with row numbers and column numbers.

Question:

How do I read values off a file and acquire, for example, 1 and 4 vs. 14?

Here's what I have so far...

All constructive criticism is welcome.

int arrayOfNum[5][5] = {
    {34,21,32,41,25},
    {14,42,43,14,31},
    {54,45,52,42,23},
    {33,15,51,31,35},
    {21,52,33,13,23}};

ofstream arrayFile;
arrayFile.open("arrays.txt");

if (arrayFile.is_open()) {
    cout << "File opened successfully..." << endl;
}

for (int i = 0; i <= 4; i++) {

    arrayFile << endl;

    for (int j = 0; j <= 4; j++) {

        arrayFile << arrayOfNum[j][i] << ' ';
    }
}
6
  • 1
    From an std::ifstream, you may use operator >>... Commented Aug 2, 2014 at 16:53
  • Reading from an ofstream ?? Are you sure ? Commented Aug 2, 2014 at 16:54
  • You have the output operator << for writing, what do you think you would use for operator to read from an input file stream? Hint: If you have used std::cin anytime you already know how to do it. Commented Aug 2, 2014 at 16:55
  • I'm not sure what the question is. Is about how to read data from a file, or is it about how to make a proper data layout in order to retrieve e.g. the dimensionality of the matrix? Commented Aug 2, 2014 at 17:39
  • I'm trying to output a 2d array to a file and then reading the values off and assigning them to a variable. The main objective is to find a value on the array such as '25', check if '25' is in row 2 and column 5...if not then the program goes to row 2 column 5 and checks that number until it finds a number where the row and column coincide with a value in the array. I need to read '25' as 2 and 5 in the file. Commented Aug 2, 2014 at 18:21

1 Answer 1

1

You are writing rows in columns as you wrote "arrayOfNum[j][i]", but I'm writing the code for correct direction. You can change the condition in following code but it's working perfect if you write "arrayOfNum[i][j]" while outputting in 'array.txt'

ifstream arrayFile;
arrayFile.open("arrays.txt");
for (int i = 0; i <= 4; i++)
{
    for (int j = 0; j <= 4; j++)
    {
        arrayFile >> arrayOfNum[i][j];
        cout<<arrayOfNum[i][j]<<" ";
        if(i+1 == arrayOfNum[i][j] / 10 && j+1 == arrayOfNum[i][j] % 10)
        {
            cout<<"\n Matched: "<<arrayOfNum[i][j]<<endl;
        }
    }
    cout<<endl;
}

And yes it will work for only 2 digit numbers, if you want for N digit numbers then I can do it for you too! :)

Sign up to request clarification or add additional context in comments.

4 Comments

This is a good answer but should explain the operators used. division gives you the first digit. % is the modulus operator and it will give you the second digit.
You used ifstream class for arrayFile so does that mean it's only an input file? Would I be able to still acquire values from the file and assign variables to them or should I switch the class to fstream? And one last thing, can I get a general formula for what you did in the if statement? Did you just divide by 10 and use the modulus because it was 2 digits?
1- Yes 'ifstream' class can only input data from file. 2- Obviously, if you're inputting using 'ifstream' you can acquire values from the file one by one using 'arrayFile>>variableName;' which means you're assigning value taken from file to 'variableName' variable. I don't understand why did you asked this question, I think you want to ask "Can I still write to file?". You can ask me again if you meant this. 3- And sure you can make a general formula like this in if statement: (log10(arrayOfNum[i][j])*10) You'll need #include <math.h> where log10 will give you (numberOfDigits-1)
But you should make sure what will be the meaning of each digit if your numbers are more than 99 i.e 100, 101, 102...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.