I want to read 2d triangle array from a txt file.
1
8 4
2 6 9
8 5 9 6
I wrote this code. At the end I wanted to print it out if I got the array right. When I run it it does not print the array, but in debug it prints. So there is a problem, but I cannot find it. Sometimes it gives segmentation fault, but I dont understand.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main() {
std::ifstream input_file("input_file.txt");
int size{1};
int **arr = (int**)malloc(3*sizeof(int));
int *lineArr = (int*) malloc(size*sizeof(int));
int temp{};
int index{};
while(input_file >> temp){
lineArr[index] = temp;
index++;
if(index == size){
index = 0;
arr[size-1] = new int[size-1];
for(int i{}; i<size; i++){
arr[size-1][i] = lineArr[i];
}
size++;
lineArr = (int*) realloc(lineArr, size*sizeof(int));
}
}
input_file.close();
for(int a{}; a<size-1; a++){
for(int j{}; j<=a; j++){
std::cout << arr[a][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
You can just use vector instead of malloc. Like this:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream input_file("input_file.txt");
vector<string> numbers;
if (input_file.is_open()) {
string line;
while (getline(input_file, line)) {
numbers.push_back(line);
}
input_file.close();
}
for (vector<string>::iterator t=numbers.begin(); t!=numbers.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}
Related
Absolute beginner here. I'm trying to solve this but get a segmentation fault. I tried searching for a solution but couldn't find the reason why this doesn't work.
To reproduce the error simply copy the code below and paste it into the editor under the link above.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
//using namespace std;
int main() {
int n, q;
std::cin >> n >> q;
std::vector<std::vector<int>> arr;
// handle n lines representing the arrays
for (int i = 0; i < n; i++) {
std::vector<int> numbers;
std::string line;
getline(std::cin, line);
std::istringstream iss(line);
int enterNumber;
while (iss >> enterNumber)
{
numbers.push_back(enterNumber);
}
arr.push_back(numbers);
}
// handle q lines representing i, j
int x, y;
for (int i = 0; i < q; i++) {
std::cin >> x >> y;
std::cout << arr[x][y];
}
return 0;
}
What am I missing? Why doesn't it work?
Input that causes the segmentation fault:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Expected output:
5
9
1) getline(std::cin, line); . Line contains space and numbers.
2) Handle spaces in lines
3) Handle first int in line for array length. (You tried to add array length in array itself)
Here is working code for reference. (Passed all test cases)
#include <vector>
#include <iostream>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector< vector<int> > arr;
int temp, array_count;
for(int i = 0; i < n; i++) {
vector<int> numbers;
cin>>array_count;
for(int j = 0; j < array_count; j++) {
cin>>temp;
numbers.push_back(temp);
}
arr.push_back(numbers);
numbers.clear();
}
// handle q lines representing i, j
int x, y;
for (int i = 0; i < q; i++) {
cin >> x >> y;
cout << arr[x][y]<<"\n";
}
return 0;
}
To fix the segmentation fault simply add std::cin.get(); after the first std::cin(third line in your main function).
The segmentation fault happened because getline(std::cin, line);returned an empty string during the first iteration of the for loop. See this.
Note that even after fixing the segmentation fault issue your code is still wrong (doesn't solve the challenge) :p
Try this:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
//using namespace std;
int main() {
int n, q;
std::cin >> n >> q;
std::cin.get();
std::vector<std::vector<int>> arr;
// handle n lines representing the arrays
for (int i = 0; i < n; i++) {
std::vector<int> numbers;
std::string line;
getline(std::cin, line);
std::istringstream iss(line);
int enterNumber;
while (iss >> enterNumber)
{
numbers.push_back(enterNumber);
}
numbers.erase(numbers.begin()); // because first value is k
arr.push_back(numbers);
}
// handle q lines representing i, j
int x, y;
for (int i = 0; i < q; i++) {
std::cin >> x >> y;
std::cout << arr[x][y] << std::endl;
}
return 0;
}
I am trying to read a dat file into a vector
the data is 5 in a row and 400 columns
however, it results in cygwin_exception::open_stackdumpfile: Dumping stack trace to ....
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
int main() {
char line[40];
vector<double> vec[400];
#define DEMENSION 5
ifstream file ("train.dat", ios::in);
//file.open("train.dat",ios::in);
int i=0,l=1;
double tmp;
while(!file.eof()){
//vec.push_back(l);
vec[i].push_back(l);
for(int j = 0; j < DEMENSION; j++) {
file >> tmp;
vec[i].push_back(tmp);
}
cout << vec[i].size()<< endl;
i++;
}
cout<<i<<endl;
//file.close();
for(int iy; iy < 400; ++iy){
vector<double>:: iterator iter = vec[iy].begin();
for(int ix = 0; iter != vec[iy].end(); ++iter, ++ix){
cout << *iter<<" " ;
}
cout<<endl;
}
return 0;
}
But if I change the dimension to 6, it works.
Then the vector results in 7 number in a row(one number is add by me),left only 344 columns,
which is not I want....It supposed to be 6 numbers if the dimension is correctly set at 5,
I'm trying to write a function that will write an array (2D) to file. This is the code below:
#ifndef WRITE_FUNCTIONS_H_
#define WRITE_FUNCTIONS_H_
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void write_array(string name, int rows, int columns, double **array){
ofstream output;
output.open(name, ios::out);
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
output<<array[r][c]<<",";
}
output<<endl;
}
output.close();
}
#endif
When I try to run it in this program here:
#include <string>
#include <iostream>
#include "write_functions.h"
using namespace std;
int main(){
double **array = new double*[10];
for(int i = 0; i < 10; i++){
array[i] = new double[10];
}
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
array[i][j] = i + j;
}
}
string array_name="home/Plinth/Documents/Temp/array.txt";
write_array(array_name, 10, 10, array);
return(0);
}
It runs perfectly fine, without error or warning, but there is no file created. Did I write something improperly? Am I going about this the wrong way?
You're likely writing in an unexpected directory.
Try to fully specify the path like /home/... (note the first '/') or just write it to a local file like array.txt.
When handling file streams, I prefer using this idiom to detect errors early.
#include <iostream>
#include <fstream>
#include <cstring>
int main() {
std::ifstream input("no_such_file.txt");
if (!input) {
std::cerr << "Unable to open file 'no_such_file.txt': " << std::strerror(errno) << std::endl;
return 1;
}
// The file opened successfully, so carry on
}
Input:
1 1 2 2 3
Desired Output:
3
Here is my code:
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <iostream>
using namespace std;
int main(){
vector<int> v;
vector<int>::iterator it;
// input variables
int input, a, arr[10000];
// input
cin >> input;
// comment all your loops, etc
for(int i = 0; i < input ; i++){
cin >> a;
arr[i] = a;
v.push_back(a);
}
for(int j = 0; j < input; j++){
int ch1 = arr[j];
for(int i = 0;i < input; i++){
if(i == j){
}
else{
if(ch1 == arr[i]){
v.erase(std::remove(v.begin(), v.end(), ch1),v.end());
}
else{
}
}
}
}
for(it = v.begin(); it != v.end(); it++){
cout << *it;
}
return 0;
}
erase() is not working here.
How can I solve this problem?
Your problem is that you define two variables with name v.
vector<int>v;
for(int v=0...
So you basically hide your vector with an int and the compiler tries to call erase() for int, which gives you error.
Just change the name of one of these variables.
I've got this code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr;
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
vecptr->push_back(temp);
}
veclen = vecptr->size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<vecptr[i]<<endl;
}
return 0;
}
My compiler(G++) throw me some errors: test2.cpp:28:17: error: no match for 'operator<<' in 'std::cout << *(vecptr + ((unsigned int)(((unsigned int)i) * 12u)))' ...
What's wrong? What can I do to fix it?
The program is still not completely right. You have to initialize the vector pointer and then give it a size and the use it. A full working code could be,
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr = new vector<string>(10);
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
(*vecptr)[i] = temp;
}
veclen = (*vecptr).size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<(*vecptr)[i]<<endl;
}
return 0;
}
Although I have mentioned the size as 10 you could make it a variant.
You need to dereference vecptr here to get the underlying vector:
cout << (*vecptr)[i] << endl;
You will also need to initialize vecptr.