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.
//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;
}
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.
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.
#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.
@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.
#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++.