Reading a file into an array that is 10 by 10

closed account (ENhkSL3A)
I'm trying to figure out how to read a list of numbers from a file into an array that has 10 rows and 10 columns. Here is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream> 
#include <fstream>
#include <iomanip>

using namespace std;
int main()
{
    cout << "Kaitlin Stevers" << endl;
    cout << "Exercise 9A - Arrays" << endl;
    cout << "October 31, 2016" <<endl;
    
    const int ARRAY_SIZE = 100    
    int numbers[ARRAY_SIZE];
    int count = 0;
    
    ifstream inputFile;
    
    inputFile.open("Ex9data.txt");
    while(count < ARRAY_SIZE && inputFile >> numbers[count])
        count++;
    
    inputFile.close();
}
closed account (ENhkSL3A)
I forgot to add they would need to be set to two decimal places and they are for sure all ints. I can post the file it'll be reading if needed.
Your array has one dimension, and ints don't have "decimal places" since they are only capable of representing whole numbers.
closed account (ENhkSL3A)
Ops. I meant floats! & What do you mean? Because i put the array size = 100? Do I just change that to const int ROW = 10 and const int COL = 10? I have no idea what to do..
I'm still a newbie, but from my understanding, you're wanting to use a two-dimension array, since there are rows and columns. Since you know how many rows and columns you have, you can declare them as constants and declare your array as int numbers[ROWS][COLS];.

You'll then need nested for loops to write the numbers into the file (I believe).

Hopefully that is all correct and I haven't led you astray.
Last edited on
closed account (ENhkSL3A)
Thanks :)
closed account (ENhkSL3A)
Now I've got this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream> 
#include <fstream>
#include <iomanip>

using namespace std;
int main()
{
    cout << "Kaitlin Stevers" << endl;
    cout << "Exercise 9A - Arrays" << endl;
    cout << "October 31, 2016" <<endl;
    
    const int ROWS = 10;
    const int COLS = 10;
    float numbers[10][10];
    int count = 0;
    
    ifstream inputFile;
    
    inputFile.open("Ex9data.txt");
    while(COLS < ROWS && inputFile >> numbers[count])
        count++;
        
    
    inputFile.close();
}


My while statement obviously doesn't work... Can someone tell me what it should be and why, please.
Last edited on
Since you've declared the rows and columns as constants, you can use them as your size declarators for your array, instead of writing 10 for each.

You also didn't write the nested for loops in your while loop. You need to write one to read each row and then another to read each column.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream> 
#include <fstream>
#include <iomanip>

using namespace std;
int main()
{
    cout << "Kaitlin Stevers" << endl;
    cout << "Exercise 9A - Arrays" << endl;
    cout << "October 31, 2016" <<endl;
    
    const int ROWS = 10;
    const int COLS = 10;
    float numbers[10][10];
    
    ifstream inputFile;
    inputFile.open("Ex9data.txt");
    
    int countRows = -1;
    int countCols = -1;

    while(++countRows < ROWS)
    {
        countCols = -1;
        while(++countCols < COLS)
        {
            inputFile >> numbers[countRows][countCols];
        }
    }
       
    inputFile.close();
}
Last edited on
closed account (ENhkSL3A)
Thank you.
closed account (ENhkSL3A)
Now I have to find out how to add the rows and put their sums as a column at the end. I also have to ad the columns and put their sums in another row at the bottom. The corner where the sums meet would hold the total of all the sums. LOL Kill me now....
closed account (ENhkSL3A)
Quick question, how would I display the array to the user after doing this?
closed account (ENhkSL3A)
Thanks!

Now can someone help me out with my next question:

My assignment is to read in data from a .txt file into a 10 by 10 array. Then I am supposed to add each row and put their total on the end. Then each column needs added and the totals in a new row at the bottom. I also need the sums sum in the corner. Her is an example of what the output has to look like:

https://www.facebook.com/profile.php?id=100010573514088

As you can I have not figured out how to sum the columns or rows. The book I go by has examples of how to do it but it does not have examples of how to do it with a .txt file being read in. I'm sure you can see why I'm lost! Any who, thanks in advance for the help!
Last edited on
closed account (ENhkSL3A)
http://stackoverflow.com/questions/40411440/reading-data-from-a-txt-file-into-an-array-and-then-summing-the-rows-and-column
Topic archived. No new replies allowed.