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,
Related
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;
}
I am filling up an adjacency list of vector with pairs given by :
vector<pair<int, int>> adj[1000];
I am doing a depth first search on the list but experiencing some weird behaviour. The first print statement prints some value which means I have some items in adj[s][0], adj[s][1], adj[s][2] and so on. However when I calculate the size of adj[s] in the next line it prints out to be zero. Am I missing something here?. Is my definition for vector of pairs correct?. The adjacency list is correctly filled because when I ran cout << adj[s][0].first << endl; in dfs, it was correctly showing me the neighbors of each and every node.
Complete code
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <utility>
#include <climits>
#include <algorithm>
using namespace std;
vector<pair<int, int>> adj[1000];
bool visited[1000];
int nodeweight[1000];
void initialize()
{
for(int i = 0; i < 1000; i++)
visited[i] = false;
for(int i=0; i < 1000; i++)
adj[i].clear();
for(int i = 0; i <1000; i++)
nodeweight[i] = INT_MAX;
}
void dfs(int s)
{
visited[s] = true;
cout << adj[s][1].first << endl;
int minimum = INT_MAX, tovisit = 0;
for(int i = 0; i < adj[s].size(); i++)
{
cout << adj[s][i].second;
if(!visited[adj[s][i].first] && adj[s][i].second < minimum)
{
minimum = adj[s][i].second;
tovisit = adj[s][i].first;
}
}
nodeweight[tovisit] = minimum;
//dfs(tovisit);
}
int main() {
int N, E;
cin >> N >> E;
while(E--)
{
int i, j, w;
cin >> i >> j >> w;
adj[i].push_back(make_pair(j,w));
adj[j].push_back(make_pair(i,w));
}
initialize();
for(int i = 1; i <= N; i++)
{
dfs(i);
}
return 0;
}
You are clearing adj again after filling in initialize().
First you fill adj in the while loop in main. Then you call initialize() which includes this loop clearing all vectors in it:
for(int i=0; i < 1000; i++)
adj[i].clear();
Then you have cout << adj[s][1].first << endl; in dfs which is undefined behavior because there are no elements in adj[s]. The fact that you seem to get the correct results is just coincidental undefined behavior (although practical it is because the memory holding the vector data was not cleared.)
adj[s].size() is correctly reported as 0.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i;
int n=0;
int F[10];
F[0]=0;
F[1]=1;
cin>>n;
for(i=2; i<n+1; ++i)
{
F[i]=(F[i-1])+F[i-2];
cout <<F[i]<<endl;
}
getch();
return 0;
}
now this is a sort of a fibonacci number generator, but it outputs all previous numbers in the fibonacci series. I want it to print the last one. For example, if the input is 8, i want it to output "21" instead of 1 2 3 5 8 13 21.
#include <iostream>
#include <conio.h>
int main()
{
int F[10];
F[0] = 0;
F[1] = 1;
int n = 0;
cin >> n;
for (int i = 2; i <= n; ++i)
{
F[i] = F[i-1] + F[i-2];
}
std::cout << F[n] << std::endl;
getch();
}
Since you already know the index of the last element (n), you can just print that after the loop. I also did some other cleanup that didn't change the functionality of the program.
Note that the program originally and still assumes that n is less than 10.
Just store only 2 last values:
#include <iostream>
using namespace std;
int main()
{
int F[2] = { 1, 1 };
int n = 0;
cin>>n;
for(int i=2; i<n; ++i)
{
swap( F[0], F[1] );
F[1] += F[0];
}
std::cout << F[1] << std::endl;
return 0;
}
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 have an array in data.txt file like 9 3 9 4 5 4 3 7 1 9 6
i need to find duplicate numbers and remove them from the array.
After that i need to collect them at the end of the array.
i wrote a code and the output is 9 3 4 5 7 1 6 9 3 4 9, but i need to put the duplicated numbers in array, in the sequence they appear in the original array.
So i need to get { 9, 3, 4, 5, 7, 1, 6, 9, 4, 3, 9 } as output.
What can i do with the code to reach my goal ?
#include <iostream>
#include <fstream>
using namespace std;
#define SZ 11
int main(){
ifstream fs("data.txt");
if (!fs)
return 0;
int a[SZ];
for (int i = 0; i < SZ; ++i)
fs >> a[i];
for (int k=0; k<SZ; k++) {
for (int j=k+1; j< SZ ; j++) {
if (a[j]==a[k]) {
for (int l=j; l<SZ-1; l++) {
a[l]=a[l+1];
}
a[10]=a[k];
}
}
}
for (int i = 0; i < SZ; ++i)
cout << a[i];
return 1;}
Here's one strategy.
Keep the notion of whether an entry is duplicate or not in a parallel array.
Print the numbers that are not duplicates first.
Then print the numbers that are duplicates.
#include <iostream>
#include <fstream>
using namespace std;
#define SZ 11
int main()
{
ifstream fs("data.txt");
if (!fs)
return 0;
int a[SZ];
int isDuplicate[SZ];
for (int i = 0; i < SZ; ++i)
{
fs >> a[i];
isDuplicate[i] = false;
}
for (int k=0; k<SZ; k++) {
for (int j=k+1; j< SZ ; j++) {
if (a[j]==a[k])
{
isDuplicate[j] = true;
}
}
}
// Print the non-duplicates
for (int i = 0; i < SZ; ++i)
{
if ( !isDuplicate[i] )
cout << a[i] << " ";
}
// Print the duplicates
for (int i = 0; i < SZ; ++i)
{
if ( isDuplicate[i] )
cout << a[i] << " ";
}
cout << endl;
// Not sure why you have 1 as the return value.
// It should be 0 for successful completion.
return 0;
}
If you want to keep the order, you have to compare each number to the previous ones instead of comparing it to the next ones. Your program becomes :
#include <iostream>
#include <iostream>
#include <fstream>
using namespace std;
#define SZ 11
int main(){
ifstream fs("data.txt");
if (!fs)
return 0;
int a[SZ];
for (int i = 0; i < SZ; ++i)
fs >> a[i];
// kk limits the number of iteration, k points to the number to test
for (int k=0, kk=0; kk<SZ; kk++, k++) {
for (int j=0; j< k ; j++) {
if (a[j]==a[k]) {
for (int l=k; l<SZ-1; l++) {
a[l]=a[l+1];
}
a[SZ - 1]=a[j];
// a[k] is a new number and must be controlled at next iteration
k -= 1;
break;
}
}
}
for (int i = 0; i < SZ; ++i)
cout << a[i];
return 1;}
I'm inclined to try out a solution that uses std::remove_if and has a deduping unary predicate. That should retain the order of your duplicate elements.
The OP's (#kuvvetkolu) original example has O(SZ^3) complexity, which is brutal. #RSahu's solution is O(SZ^2), an improvement (and correct), but this should not require O(N^2)...
Here's a version that incurs only space overhead (assuming O(1) hash table lookup). You can use a unordered_set (a hash table) to track whether you've already seen a particular number, put it in the appropriate vector and then merge the vectors at the end.
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <vector>
int main() {
std::ifstream fs("data.txt");
if (!fs)
throw std::runtime_error("File not found!");
std::vector<int> a;
std::vector<int> dups;
std::unordered_set<int> seen;
int d;
while (fs) {
fs >> d;
if (seen.find(d) == seen.end())
{
a.push_back(d);
seen.insert(d);
}
else
{
dups.push_back(d);
}
}
a.insert(a.end(), dups.begin(), dups.end());
for (auto n : a)
std::cout << n << " ";
return 0;
}