I have a big file with numbers and I want to extract numbers based on their positions(row, column). For example, if I want to process only the first 3 rows and 3 columns which are 9 numbers. The program print the 9 numbers in the first row. I want to go the next line once the column index is 4.
How to do this in c++.
Here is what I have been don so far:
#include <iostream>
#include <fstream>
using namespace std;
const int NROWS = 3;
const int NCOLS = 3;
int main()
{
ifstream data;
double Numbers[NROWS][NCOLS];
double Num;
data.open("Input.txt");
for (int i = 0; i<NROWS; ++i)
{
for (int j = 0; j<NCOLS; ++j)
{
data >> Num;
Numbers[i][j]=Num;
cout << Numbers[i][j] << endl;
}
}
data.close();
return 0;
}
You should skip lines after each NCOLS column numbers are read.
#include <iostream>
#include <fstream>
using namespace std;
const int NROWS = 3;
const int NCOLS = 3;
int main()
{
ifstream data;
double Numbers[NROWS][NCOLS];
double Num;
data.open("Input.txt");
for (int i = 0; i<NROWS; ++i)
{
for (int j = 0; j<NCOLS; ++j)
{
data >> Num;
Numbers[i][j]=Num;
cout << Numbers[i][j] << endl;
}
std::string skip;
std::getline(data, skip);
}
data.close();
return 0;
}
Related
I'm trying to create a battleship program that reads in a 25x25 grid of characters from a text file and puts the info into a 2D array. I've been able to set up the array and read in the info, but for some reason my first nested loop is reading the entire file instead of just one line like I intend. I have tried using .get(), .getLine(), .peek(), etc. with no luck. I'm not sure if I'm using the >> operator incorrectly or if there is a logic error in the loops. Below is the code for my program.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
char game_map[25][25];
int main()
{
ifstream file("GameMap.txt"); //Opens text file so that data can be read in
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
file >> game_map[i][j];
}
}
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
cout << game_map[i, j];
}
cout << "LINE " << i << endl;
}
system("pause");
return 0;
}
Please let me know if you have any questions.
You should enable and read the warnings. The compiler says
warning: left operand of comma operator has no effect [-Wunused-value]
23 | cout << game_map[i, j];
| ^
After you fix it, it should work.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
char game_map[25][25];
int main()
{
ifstream file("GameMap.txt"); //Opens text file so that data can be read in
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
file >> game_map[i][j];
}
}
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
cout << game_map[i][j]; // <-- Fix it
}
cout << "LINE " << i << endl;
}
system("pause");
return 0;
}
Update: I need help figuring out how to print this correctly. I've tried and I can not get it right. What would you do?
I am using c++. 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 summed in the corner. Here is an example of what the output has to look like: Example of output screen.
Here is the code I currently have:
#include <iostream>
#include <iomanip>
#include <fstream>
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[11][11];
ifstream inputFile;
inputFile.open("Ex9data.txt");
for(int countRows = 0; countRows < ROWS; countRows++)
{
for(int countColumns = 0; countColumns < COLS; countColumns++)
{
inputFile >> numbers[countRows][countColumns];
}
}
inputFile.close();
for(int i=0;i<10;i++)
{
column = 0;
numbers[10][i]=0
for(int j=0;j<10;j++)
{
numbers[10][i] +=numbers[j][i];
}
//do the printing here & storing... column based sum here.
}
for(int i=0;i<10;i++)
{
row = 0;
numbers[i][10]=0;
for(int j=0;j<10;j++)
{
numbers[i][10] +=numbers[i][i];
}
//do the printing here & storing... row based sum here.
}
return 0;
}
I need to know how to print the whole array.
Try this:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
cout << "Logman ;P" << endl;
cout << "Exercise 9A - Arrays" << endl;
cout << "October 31, 2016" <<endl;
int ROWS = 0;
int COLS = 0;
float numbers[11][11];
ifstream inputFile;
inputFile.open("Ex9data.txt");
inputFile >> ROWS;
inputFile >> COLS;
for(int countRows = 0; countRows < ROWS; countRows++)
{
for(int countColumns = 0; countColumns < COLS; countColumns++)
{
inputFile >> numbers[countRows][countColumns];
}
}
inputFile.close();
for(int i=0;i<COLS;i++)
{
numbers[ROWS][i] = 0.0f;
for(int j=0;j<ROWS;j++)
{
numbers[ROWS][i] +=numbers[j][i];
}
}
for(int i=0;i<ROWS+1;i++)
{
numbers[i][COLS] = 0.0f;
for(int j=0;j<COLS;j++)
{
numbers[i][COLS] +=numbers[i][j];
}
}
for(int i=0;i<ROWS+1;i++)
{
if(i==ROWS)
{
for(int j=0;j<(COLS+1)*10;j++) cout << "-";
cout <<endl;
}
for(int j=0;j<COLS+1;j++)
{
if(j==COLS) cout << "|";
cout << setw(9) << setprecision(6) << numbers[i][j] << " ";
}
cout <<endl;
}
return 0;
}
Since the data is read and put into the matrix, now iterate the matrix and do the column based sum. Sample code for it is below.
for(int i=0;i<10;i++)
{
sum = 0;
for(int j=0;j<10;j++)
{
sum+= numbers[j][i];
}
//do the printing here or storing... basically you get the column based sum here.
}
for Row based sum
for(int i=0;i<10;i++)
{
sum = 0;
for(int j=0;j<10;j++)
{
sum+= numbers[i][j];
}
//do the printing here or storing... basically you get the row based sum here.
}
The term summing means to add up numbers. Summing the rows means to add all the numbers in the row.
// Pick a row to sum
const unsigned int row_to_sum = 3;
// Declare and initialize a summation variable.
float sum = 0.0f;
// Add all the columns in the given row.
for (unsigned int i = 0; i < COLS; ++i)
{
sum += numbers[row_to_sum][i];
}
To sum up the a column, keep the column index constant and iterate the rows.
const unsigned int column_to_sum = 8;
sum = 0.0f;
for (unsigned int j = 0; j < ROWS; ++j)
{
sum += numbers[j][column_to_sum];
}
This is what I have now:
#include <iostream>
#include <iomanip>
#include <fstream>
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[11][11];
float row, column;
ifstream inputFile;
inputFile.open("Ex9data.txt");
for(int countRows = 0; countRows < ROWS; countRows++)
{
for(int countColumns = 0; countColumns < COLS; countColumns++)
{
inputFile >> numbers[countRows][countColumns];
}
}
inputFile.close();
for(int i=0;i<10;i++)
{
column = 0;
for(int j=0;j<10;j++)
{
numbers[10][i] +=numbers[j][i];
}
//do the printing here & storing... column based sum here.
}
for(int i=0;i<10;i++)
{
row = 0;
for(int j=0;j<10;j++)
{
numbers[i][10] +=numbers[i][i];
}
//do the printing here & storing... row based sum here.
}
return 0;
}
I've been trying to solve this problem for a while now. The code below works for 4 digits (it matches with the answer they give) and it even works for 5 consecutive digits (which was what the problem previously asked for), but it doesn't seem to work for 13 consecutive digits.
Can someone tell me what Im doing wrong?
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int Char_To_String(char);
int main()
{
/*****************************************************/
/*********** PROJECT EULER: PROBLEM #8 ***************/
/*****************************************************/
fstream file("grid.txt", ios::in); // open file for reading
if(!file)
{
cout << "FILE NOT OPENED!";
return 0;
}
int row = 20;
int column = 50;
int num;
char arr[row][column];
int intarr[row][column];
for(int i = 0; i<row; i++)
{
for(int j = 0; j<column; j++)
{
file >> arr[i][j];
intarr[i][j] = Char_To_String(arr[i][j]);
}
}
long long prod = 1;
long long int maxProd = 1;
for(int i = 0; i<row; i++)
{
for(int j = 0; j<column; j++)
{
if(j<=column-13)
{
int temp = j;
for(int n = 1; n<=13; n++)
{
prod *= intarr[i][temp];
temp++;
}
if(prod > maxProd)
maxProd = prod;
prod = 1;
}
}
}
cout << "maxProd = " << fixed << maxProd << endl;
I am trying to read in a "map" into my program. it's a list of 100 numbers, I want it to be a 10 x 10 array. I'm trying to use a void function to read the file.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int rows = 10, columns = 10, inaccessible = 0, start = 1, victory = 2;
typedef unsigned int world[rows][columns];
void loadWorld (world map[rows][columns]);
int main()
{
cout << "Welcome to my game! Get to the bottom of the volcano to win." << endl;
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
cout << world[i][j];
}
return 0;
}
void loadWorld (world map[rows][columns])
{
ifstream inData;
inData.open("world.txt");
}
To read it in, do the opposite of what you do with cout:
inData >> map[i][j];
Here you have a naive example. The output is formatted.
#include <iostream>
#define Rows 3
#define Cols 2
int main()
{
int Matrix[Rows][Cols];
//Input
for(int i = 0; i < Cols; i++)
for(int j = 0; j < Rows; j++)
std::cin >> Matrix[j][i];
//Output
for(int i = 0; i < Cols; i++)
{
for(int j = 0; j < Rows; j++)
std::cout << Matrix[j][i] << "\t";
std::cout << "\n";
}
return 0;
}
so this program is supposed to input things i type in, and output them in a matrix format, heres the code i have so far:
#include <iostream>
#include <string>
using namespace std;
#define N 6
//
// fill:
//
void fill(string s, int M[][N], int ROWS, int COLS)
{
int i, r, c;
for (i=0, r=0; r < ROWS; r++)
{
for (c=0; c < COLS; c++)
{
M[r][c] = s[i]; // store ith character into matrix:
i++; // next character:
if (i == s.length()) // start-over if that was last char:
i = 0;
}
}
}
void print(int M[][N], int ROWS, int COLS)
{
string s;
getline(cin,s);
int r, c;
for(r=0; r< ROWS; r++)
{
for(c=0; c < COLS; c++)
{
cout<<(char)M[r][c];
}
cout <<endl;
}
}
//
// main:
//
int main()
{
string s;
int M[N][N];
int M2[N][N];
int row, col, ROWS, COLS;
ROWS = sizeof(M) / sizeof(M[0]);
COLS = sizeof(M[0]) / sizeof(M[0][0]);
fill(s, M, 1, 1);
print(M, ROWS, COLS);
return 0;
}
any idea why its outputting random characters out instead of the ones i type in?
First, you are not initializing string s before passing it to fill function.
Next, you are trying to insert a string into an int. C++ doesn't let you do that directly.
Are you trying to split a string such as "123456" into [1,2,3,4,5,6; 1,2,3,4,5,6;...] etc?
Is this what you want your program to do?
Here you are storing the input in an integer array and trying to display the character values.
The statement cout<<(char)M[r][c] will give you the ASCII value corresponding to the integer value of M[r][c].
That might be the random characters you are seeing.
You can directly use a character array instead and try outputing by directly calling:cout<< M[r][c]
Try this,
#include <iostream>
#include <string>
using namespace std;
#define N 6
void fill(string s, char (&arr)[N][N])
{
int char_index =0;
for (int i=0; i < N; i++)
{
for (int j=0; j < N; j++)
{
arr[i][j] = s.at(char_index);
if (char_index+1 == s.size()){
char_index =0;
}else{
char_index++;
}
}
}
}
void print(char arr[][N])
{
cout << "--------------" << endl;
int char_index =0;
for (int i=0; i < N; i++)
{
for (int j=0; j < N; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << "--------------" << endl;
}
int main()
{
string s ="";
cout << "Enter your string:";
getline(cin,s);
char M[N][N];
fill(s,M);
print(M);
system("pause");
return 0;
}