printf Matrices from a .txt file

I have a text file with the following two matrices:

3 2

5 1
9 5
7 6


3 2

4 2
1 8
6 9

I need to get the program below to read the first two numbers (3 2) and printf the following numbers based on those corresponding (x, y) dimensions. Then do it again for the numbers below that.

------------------------------------------------------------------------------

#include <iostream>
#include <stdio.h>

int main() {

//declare matrices
int matrixInA;
int matrixInB;
float matrixOutC;
int numbers[100];
int size;
//set up file reading
FILE *fptr;

//opening the file
fptr = fopen("matrixIn.txt", "r");

if (!fptr){
printf("Error!");
return -1;
}

int count = 0;

//read the file
while (fscanf(fptr, "%d", &numbers[count]) !=EOF){
count++;
}

/*figure out how to read in the first two numbers and fulfil the matrix dimensions
then read int the next two numbers and full in the matrix to those dimensions*/

//print file
for(int i = 0; i < count; i++){
printf("%d ", numbers[i]);
}
return 0;
}
Just for clarification:
Does it mean that for your examples the result should be:

5 9 7
1 5 6

and

4 1 6
2 8 9

?
Yes, sorry. Bad formatting.
Hello hackermanGabe,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.


You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



First question is this a C or C++ program that you need?

Based on the code the header file "iostream" should not be included.

Next question after the read of the initial two numbers should what is left be in a 1D or 2D array?

I know that there is a way to use a 1D array as a 2D array, but I am not use to using it that way.

I think you might do better using a 2D array for the matrix.

I will need to work on this a bit.

Andy
Sorry, I'm new to this platform. I use C++

After the two initial numbers are read, the code should use them as the appropriate (X, Y) or height x length dimensions to create a 2d array, then printf them as separate matrices for the user to read.
That's my program. HandyAndy, I'm be curious of yours :-)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

// Comment the line below out if you want to read from a real file!
std::istringstream in_file("3 2 5 1 9 5 7 6 3 2 4 2 1 8 6 9");

int main()
{
    std::string file_name = "matrixin.txt";
    
    // Remove the comment of the line below if you want to read from a real file!
    //std::ifstream in_file( file_name );
    if( !in_file )
    {
        std::cerr << "File " << file_name << " couldn't opened!\n";
        return 1;
    }
   
    
    while( true )
    {
        std::vector<int> matrix;
        int height, width; // the dimensions
        
        // read the dimensions from file
        if( !(in_file >> height >> width) )
            break;  // file is at at end (or others happend with)
    
        // I use a one dimensional array, the two-dimensonality will
        // get achieved through arithmetic operations
        
        // read the matrix from file
        for( int i = 0; i < width*height; ++i )
        {
            int tmp;
            in_file >> tmp;
            matrix.push_back( tmp );
        }
        
        // print the matrix
        for( int h = 0; h < height; ++h )
        {
            for( int w = 0; w < width; ++w )
            {
                std::cout << matrix[ w + h*width ] << ' ';
            }
            std::cout << '\n' ;// line break
        }
        std::cout << '\n';
    }
}
OP, in C++, we have iostreams like std::cout, std::cin, and std::cerr. For most purposes, you can use these over traditional printf() statements. They are a lot more flexible with custom classes.
I appreciate you taking the time. I'm doing my best to make sense of your code, but that is my own inexperience. Thanks!
@hackermanGabe, learning C++ takes a stiff slope in learning. If you are really interested in that, I recommend a good book, (or several). A very good but easy to read for beginners is e.g. "Programming: Principles and Practice Using C++" from Bjarne Stroustrup, its so afaik available in the 2nd edition.

And if something (or a lot) of the code is unclear to you, feel free and ask here in the forum.
Last edited on
There are good "Learn C++" books, as well as online tutorials.

One is here at CPlusPlus:
http://www.cplusplus.com/doc/tutorial/

Another one is:
https://www.learncpp.com/

I personally prefer the Learn C++ tutorial, it covers C++14 and later. The tutorial here stopped being updated with C++11.
@OP,
You can read the first two values like 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
#include<stdio.h>
#include<stdlib.h> 

int main()
{
  int rows = 0, cols = 0;
  //set up file reading
  FILE *fptr;

  //opening the file
  fptr = fopen("matrixIn.txt", "r");

  if (!fptr) {
    printf("Error!");
    return -1;
  }
  if (fscanf(fptr, "%d %d", &rows, &cols) != 2)
  {
    printf("Error reading file");
    return 1;
  }
  printf("Rows: %d\tCols: %d", rows, cols);
  return 0;
}


To read the actual data you need to decide where to store it.
You could use a dynamic array, a dynamic 2d array or a static array if the the rows and cols have a max value.

BTW. I would stick with C unless you have plenty of time to learn C++.
Topic archived. No new replies allowed.