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;
}
Related
How would I swap 2d array columns in places, but the swap has to happen between the columns that contain the smallest and biggest element in the 2d array. Any help on this would be appreciated.
#include <iostream>
#include <ctime>
#include <cstdlib>
#define rows 4
#define cols 4
using namespace std;
int main () {
srand(time(0));
int i=0, j=0,n,low,high,lowpos,highpos,a,b;
int arr[rows][cols];
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
arr[i][j] = 1 + (rand() % 200);
cout << "Random 2d generated array:" << endl;
for (i=0; i<rows; i++){
for (j=0; j<cols; j++)
cout << arr[i][j] << " ";
cout << endl;
}
high=arr[0][0];
low=arr[0][0];
for(i=0;i<rows;++i)
{
for(j=0;j<cols;++j)
{
if(arr[i][j]>high)
high=arr[i][j];
else if(arr[i][j]<low)
low=arr[i][j];
}
}
cout<<"\nBiggest element:"<<high<<"\nSmallest:"<<low<<"\n";
}
I decided to stay in c++11 standard. Of course swap helps do the trick.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#define rows 4
#define cols 4
using Arr = int[rows][cols];
std::pair<int, int> high_low_ids(const Arr arr, int nrow = rows, int ncol = cols) {
int highpos = 0, lowpos = 0;
int high = arr[0][0];
int low = arr[0][0];
for(int i = 0; i < nrow; ++i)
for(int j = 0; j < ncol; ++j)
if(arr[i][j] > high) {
highpos = j;
high=arr[i][j];
}
else if(arr[i][j] < low) {
lowpos = j;
low=arr[i][j];
}
std::cout << "\nBiggest element:" <<high<< "\nSmallest:" << low << "\n";
return {highpos, lowpos};
}
void rows_swap(Arr table2d, int nrow = rows){
auto p = high_low_ids(table2d);
const int high_idx = p.first;
const int low_idx = p.second;
if (high_idx == low_idx)
return;
for (int i = 0; i < nrow; ++i) {
std::swap(table2d[i][high_idx], table2d[i][low_idx]);
}
}
void print_arr(Arr arr, int nrow = rows, int ncol = cols) {
for (int i = 0; i < nrow; i++) {
for (int j = 0; j < ncol; j++)
std::cout << std::left << std::setw(6) << arr[i][j];
std::cout << std::endl;
}
}
int main () {
srand(time(0));
Arr arr;
for (int i = 0; i < rows; ++i)
for (int j=0; j<cols; ++j)
arr[i][j] = 1 + (rand() % 200);
std::cout << "Random 2d generated array:\n";
print_arr(arr);
rows_swap(arr);
std::cout << "Swapped array:\n";
print_arr(arr);
return 0;
}
PS. please avoid using namespace std.
I am again here with this query.
In the following code, I initialize my matrix with all values equals zero and after it, I write this matrix in a text file. Now I want to read that matrix from the file and put all the values in another matrix named arr1[][] and when I print it on the screen I got a blank screen why?
Please help me thanks in advance.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
int n = 10;
int m = 10;
int arr[10][10];
int arr1[10][10];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
arr[i][j] = 0; // here i initialized my matrix with all values zero.
}
ofstream fout;
fout.open("array.txt");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
fout << arr[i][j]; // here i write it into the file
}
cout << endl;
}
fout.close();
ifstream fin;
fin.open("array.txt");
if (!fin)
{
cerr << std::strerror(errno) << "\n"; // handle open errors
}
fin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
fin >> arr[i][j]; // now here i read the matrix and put it into the
// different matrix.
arr1[i][j] = arr[i][j];
}
fin.close();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arr1[i][j]; // when i print it to the screen i got black screen
// but i wants
// a matrx which written in the file.
}
cout << endl;
}
return 0;
}
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;
}
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;
}
I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}