int main()
{
int n, inInt;
vector <int> list;
ifstream ifs("1.txt");
float a;
ifs >> a;
std::vector<int> result;
int temp;
while(! ifs.eof())
{
ifs >> temp;
result.push_back(temp);
}
int b;
b = result.size();
float array[b+1];
int i;
array[0] = a;
for(i = 1;i < b+1;i++) {
array[i] = (array[i-1] + result[i]-2*array[i-1] * result[i]/a);
}
cout << array[b];
system("pause");
return 0;}
Basically in my code, I built a vector and build an array and try to use the data from the vector in the array.However,when I text the code,it gave me a huge incorrect number.
temp is not an array, temp is an int. You're trying to use operator[] on an int you cannot do that. Also, VLAs (Variable Length Arrays) are a GCC extension, and therefore not standard. I suggest you replace this line:
float array[b+1];
With:
std::vector<float> array(b+1);
I see at least two problems. First, your input loop is incorrect and inserts an invalid entry into result. You can fix this by using the following input loop:
while (ifs >> temp)
{
result.push_back(temp);
}
Second, the vector result contains b elements so the valid indices are [0..b-1]. Your loop end condition i < b+1 will read one element past the end of result. I think you can fix this issue by doing this instead:
for(i = 0; i < b; i++)
{
array[i+1] = (array[i] + result[i]-2*array[i] * result[i]/a);
}
When I made these changes and used 3 2 2 for input, the output was 1.66667
Related
As I'm new to c++ I get runtime error for first example(I mean I tested my program with 5 examples it actually happens automatically by a site for testing) of my program I know that's because of exceeding time for running it but I dunno how to fix this.
My program get n numbers from user and finds the largest one and prints it.
#include<iostream>
#include<curses.h>
using namespace std;
int main()
{
int n;
cin >> n;
int *p = new int(n);
for(int i = 1; i<=n; i++){
cin >> *(p+i);
}
int largest = *p;
for(int i = 1; i<=n; i++){
if(largest < *(p+i))
largest = *(p+i);
}
cout << largest;
return(0);
}
int *p=new int(n);
The line above allocates just a single int, and sets the value to n. It does not allocate an array of n integers.
That line should be:
int *p=new int[n];
And then delete [] p; to deallocate the memory.
But better yet:
#include <vector>
//...
std::vector<int> p(n);
is the preferred way to utilize dynamic arrays in C++.
Then the input loop would simply be:
for(int i=0;i<n; i++)
{
cin >> p[i];
}
That same input loop could have been used if you had used the pointer version.
Then you have this error:
for(int i=1;i<=n;i++)
Arrays (and vectors) are indexed starting from 0 with the upper index at n-1, where n is the total number of elements. That loop has an off-by-one error, where it exceeds the upper index on the last loop.
Basically any loop that uses <= as the limiting condition is suspect. That line should be:
for(int i=0; i<n; i++)
(Note that I changed the code above to fix this error).
However ultimately, that entire loop to figure out the largest can be accomplished with a single line of code using the std::max_element function:
#include <algorithm>
//...
int largest = *std::max_element(p, p + n);
and if using std::vector:
#include <algorithm>
//...
int largest = *std::max_element(p.begin(), p.begin() + n);
I've commented on suggested changes in this slightly modified version:
#include <iostream>
int main()
{
unsigned n; // don't allow a negative amount of numbers
if(std::cin >> n) { // check that "cin >> n" succeeds
int* p=new int[n]; // allocate an array of n ints instead of one int with value n
for(int i=0; i < n; ++i) { // corrected bounds [0,n)
if(not (std::cin >> p[i])) return 1; // check that "cin >> ..." succeeds
}
int largest = p[0];
for(int i=1; i < n; ++i) { // corrected bounds again, [1,n)
if(largest < p[i])
largest = p[i];
}
delete[] p; // free the memory when done
std::cout << largest << '\n';
}
}
Note that using *(p + i) does the same as using p[i]. The latter is often preferred.
This would work if all cin >> ... works, but shows some of the hazards when using raw pointers. If extracting the n ints failes, the program will return 1 and leak the memory allocated with new int[n].
A rewrite using a smart pointer (std::unique_ptr<int[]>) that automatically deallocates the memory when it goes out of scope:
#include <iostream>
#include <memory> // std::unique_ptr
int main()
{
unsigned n;
if(std::cin >> n) {
std::unique_ptr<int[]> p(new int[n]);
for(int i=0; i < n; ++i) { // corrected bounds [0,n)
if(not (std::cin >> p[i])) return 1; // will not leak "p"
}
int largest = p[0];
for(int i=1; i < n; ++i) {
if(largest < p[i])
largest = p[i];
}
std::cout << largest << '\n';
} // p is automatically delete[]ed here
}
However, it's often convenient to store an array and its size together and to do this, you could use a std::vector<int> instead. It comes with a lot of convenient member functions, like, size() - and also begin() and end() which lets you use it in range-based for loops.
#include <iostream>
#include <vector> // std::vector
int main()
{
unsigned n;
if(std::cin >> n) {
std::vector<int> p(n); // a vector of n ints
// a range-based for loop, "elem" becomes a refrence to each element in "p":
for(int& elem : p) {
if(not (std::cin >> elem)) return 1;
}
int largest = p[0];
for(int i = 1; i < p.size(); ++i) { // using the size() member function
if(largest < p[i])
largest = p[i];
}
std::cout << largest << '\n';
}
}
That said, you don't need to store any number in an array to figure out what the largest number is. Instead, just compare the input with the currently largest number.
#include <iostream>
#include <limits> // std::numeric_limits
int main()
{
unsigned n;
if(std::cin >> n) {
// initialize with the smallest possible int:
int largest = std::numeric_limits<int>::min();
while(n--) {
int tmp;
if(not (std::cin >> tmp)) return 1;
if(largest < tmp)
largest = tmp;
}
std::cout << largest << '\n';
}
}
This question already has answers here:
Segmentation Fault (SIGSEGV) when getting data as input in the vector of struct
(2 answers)
Closed 2 years ago.
Getting SIGESV error in following cpp code, kindly check.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
int sa = 0, sb = 0, c = 0;
cin >> n >> m;
vector<int> a;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b;
for (int i = 0; i < m; i++) {
cin >> b[i];
}
int k = min(n, m);
while (k--) {
sa = accumulate(a.begin(), a.end(), 0);
sb = accumulate(b.begin(), b.end(), 0);
if (sb >= sa) {
swap(*min_element(a.begin(), a.end()),
*max_element(b.begin(), b.end()));
c++;
} else {
break;
}
}
if (sb >= sa) {
cout << "-1" << endl;
} else {
cout << c << endl;
}
}
return 0;
}
The code asks for two arrays and swaps only if array a has a summation less than array b.
Getting error as SIGESV, also can take 'b' array as an input, what to do?
When you're doing cin >> a[i], a is still empty, so attempting to access its i-th element produces a segfault. Resize it before with a.resize(n) or initialise it with the proper size: vector<int> a(n);
The size and capacity of vector a is 0 and never changes.
This code is broken:
vector<int> a;
for(int i=0;i<n;i++){
cin>>a[i];
}
operator[]() does not grow the vector. You are assigning values to memory that does not belong to a.
You want a.push_back(i) at a minimum. What you probably really want is:
vector<int> a;
a.reserve(n);
for (int i = 0; i < n; ++i) {
int tmp;
cin >> tmp;
a.push_back(tmp);
}
Reserving memory stops the vector from re-allocating. Not doing it in a constructor also stops default initialization of all elements.
The same goes for the b vector.
You also never change the values of sa or sb, but that shouldn't crash anything, it's just a logic error from what I can see.
The file as a whole is also rife with bad practices. "Competitive" coding sites actively make you a worse programmer.
When you know size you need while declaring vector you should declare vector like this
vector<int>arr(n);
vector<int>arr2(m);
this will create an vector of size n will value being zero at all indexes in vector at initialization.
Don't use <bits/stdc++.h> and trying coming up for better name for variables it will really help you as well as us. I know this is compeitive programming question but still.
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
struct Node{
string data;
Node* next;
Node(){
data = "";
next = NULL;
}
};
int computeHash(string s, int m){
int p = 1000000007;
int x = 263;
unsigned long long sum = 0;
unsigned long long val = 0;
for(int i = 0; i < s.length(); i++){
val = pow(x, i);
sum = (sum + s[i] * val) % p;
}
sum = sum % m;
return sum;
}
int main(){
int buckets;
cin >> buckets;
int n;
cin >> n;
string tag;
string s;
vector< vector<string> > myStore(n);
for(int i = 0; i < n; i++){
cin >> s;
myStore.at(i).push_back(s);
cin >> tag;
myStore.at(i).push_back(tag);
}
Node** arr= new Node*[buckets];
for(int i = 0; i < n; i++){
if(!myStore[i][0].compare("add")){
s = myStore[i][1];
int hash = computeHash(s,buckets);
cout << hash << endl;
}
}
return 0;
}
I am trying to write a program to implement hashing with chains. I am trying to create an array of nodes so that I can append if two strings have the same hash value.
But I am having a problem with the initialization of array of Nodes. I thought the nodes in the array would be pointing to NULL. But when I tried to debug in gdb it is showing some other thing.
Can someone explain where I am wrong on comment about this behavior. Why arr1 and arr[2] are pointing to some memory location instead of null. I also tried to remove the default constructor but still getting the same results. Any help would be appreciated.
You're allocating an array of pointers. Pointers don't have constructors, or default initialization; you're getting random memory (from the allocation).
If you want the array to be NULL-ed out, you need to do so yourself (eg: memcpy, etc.).
You have initialized vector of size n of vectors of size 0 of strings.
Then you want to get the '[1]' (the second element of empty vector of strings)
You have to inutialize them separately.
e.g. in "for" cycle.
Updated. Use myStore.at(i).at(1) instead of myStore[i][1] to achieve checking of boundary conditions. (Try it, you will understand that the problem with vector indeed)
So i was trying to make the challage: Breadth First Search: Shortest Reach on HackerRank, but i keep getting the bad alloc exception when the tests have great numbers of node/edges. The program works on the first test, so i don't think, it's something wrong with the implementation.
So here is the implementation:
(sorry for the indentation , my first question)
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <limits.h>
using namespace std;
int main() {
//test numbers
int t;
//the cost
int cost = 6;
cin >> t;
//for each test
for (int nt = 0; nt < t; ++nt) {
int n, e;
int snode;
queue <int> *que = new queue<int>();
//read the node/edges
cin >> n >> e;
//distance/visited/parents arrays/adjlist vector
int dist[n + 1] = {-1};
bool visited[n + 1] = {false};
int parents[n + 1] = {-1};
vector< vector<int> > adjList(n + 1);
//read into the adjlist, unoriented graph, each edge has 6 weight
for (int ne = 0; ne < e; ++ne) {
int x, y;
cin >> x >> y;
adjList[x].push_back(y);
adjList[y].push_back(x);
}
//read the starting node
cin >> snode;
dist[snode] = 0;
//do actual bfs
que->push(snode);
visited[snode] = true;
while(!que->empty()) {
int c_node = que->front();
que->pop();
for (int i = 0; i < adjList[c_node].size(); ++i) {
if (visited[adjList[c_node].at(i)] == false) {
que->push(adjList[c_node].at(i));
parents[adjList[c_node].at(i)] = c_node;
dist[adjList[c_node].at(i)] = dist[parents[adjList[c_node].at(i)]] + cost;
visited[adjList[c_node].at(i)] == true;
}
}
}
//print at output the distance from the starting node to each other node
//if unreachable, print -1
for (int i = 1; i < n + 1; ++i) {
if (i == snode) {
} else if (dist[i] == 0 && i != snode) {
cout << "-1 ";
} else {
cout << dist[i] << " ";
}
}
cout << "\n";
}
return 0;
}
Am i doing something wrong, i haven't seen anyone else complain on this matter in the discussion section of the site.
How can i avoid the exception to be thrown and from where does it come?
Thank you!
I don't know, exactly, what is the cause of your exception; and I don't know ho to reproduce your problem because depends (I suppose) from the input values. A lot of input values, I suppose.
But I see some weak points (IMHO) of your code, so I try to point your attention to them.
1) you alloc a std::queue in your for cycle
queue <int> *que = new queue<int>();
but you never free it; it's a waste of memory
2) you're using C-style variable-length arrays
int dist[n + 1] = {-1};
bool visited[n + 1] = {false};
int parents[n + 1] = {-1};
They aren't valid C++ standard code. I suggest you the use of standard containers (std::vector or std::queue).
3) you're initializing your C-style variable-length arrays with a initializers lists with only an element (-1 or false). I suppose your intention was initialize all n+1 elements with -1 and false. But this syntax initialize only the first element of the array with -1 and false.
If you want to initialize all n+1 element to -1 and false, the solution is (again) use standard containers; by example
std::vector<int> dist(n+1, -1);
std::vector<bool> visited(n+1, false);
std::vector<int> parents(n+1, -1);
4) you access arrays without bounds checking. By example:
cin >> snode;
dist[snode] = 0;
where snode is a int variable; if you insert a negative value, or a value over n, you write dist out of its bounds, devastating the memory. This, I suppose, can explain your "bad alloc exception".
Suggestion: use standard containers (again) instead of C-style array and use at() (that perform bounds checking) instead []; so
cin >> snode;
dist.at(snode) = 0;
5) sorry for my bad English (ok, I'm joking: this isn't one of your weak points; this is one of mine).
I've read about 2d dynamic arrays but I obviously haven't quite got my head around it as this program doesn't work. The program seems to lie in displaying the array.
The input file is a text file with V and E on the first line with a 'tab indent' between them. The input vertices are on the next lines again tab indented with a new set on each line. On DevCpp it says there is a segmentation fault. Any help would be very much appreciated. thanks.
#include <iostream>
#include <fstream>
using namespace std;
#define maxV 100
#define unseen 0
typedef int Vertex;
class Graph {
private:
int V, E;
int**adj;
public:
Graph(char filename[]);
void display();
};
// constructor ask you for file name
Graph::Graph(char fname[]) {
Vertex u,v;
int j;
ifstream f;
f.open(fname, ios::in);
if(!f) {
cout << "\nError: Cannot open file\n";
return;
}
//Input number of vertices and edges
f >> V >> E;
int** adj = new int*[V];
for (int i=0;i<=V;i++)
{
adj[i]= new int[V];
}
for(int x=0;x<=V; ++x) // initially 0 array
{
for (int y=0;y<=V;++y)
adj[x][y] = 0;
}
// Set diagonal to 1
for(int z=0; z<=V; ++z)
adj[z][z]=1;
for (j =0;j<=E;++j)
{
f>>u>>v;
adj[u][v] = 1;
adj[v][u] = 1;
}
}
// This method displays the adjacency lists representation.
void Graph::display(){
int a,b,c;
for (a=0;a<=V;++a)
{
cout << a << " ";
}
cout << endl;
for (b=0;b<=V;++b)
{
cout << b << "| ";
for (c=0;c<=V;++c)
{
cout<<adj[b][c]<<"| ";
}
cout<<endl;
}
}
int main()
{
char fname[20];
cout << "\nInput name of file with graph definition: ";
cin >> fname;
Graph g(fname);
g.display();
}
//Input number of vertices and edges
f >> V >> E;
// You're hiding your member variable in the following line, leading to an incorrect initialization
// int** adj = new int*[V];
adj = new int*[V];
for (int i=0;i<=V;i++)
{
adj[i]= new int[V];
}
I see two significant problems just in the code that initializes the data array. First, a loop like this
for (int i=0;i<=V;i++)
loops over one more element than actually exists in the array. The correct form of a loop if the array is V elements long is
for (int i=0;i<V;i++)
That's "less than" rather than "less than or equal".
Secondly, you allocate both the array of pointers to be V pointers long, and than the individual columns to be V elements long as well; but later you use the same array and expect it to be V x E in size. Altogether, then, I think the allocation code ought to be
int** adj = new int*[V];
for (int i=0;i<V;i++)
{
adj[i]= new int[E];
}
There are likely to be other errors elsewhere, but at least I've got you started.
I don't know which line is causing the segmentation fault but here are some things to look at:
for (j =0;j<=E;++j)
{
f>>u>>v;
adj[u][v] = 1;
adj[v][u] = 1;
}
Are u and v guaranteed to be less than V? If not you could be writing outside the bounds of the matrix.
What happens when j == E? You are trying to read a line past the last line in the file. You should be checking instead for j < E. A better way still would be to ignore E all together and just do this:
while(f >> u >> v)
{
adj[u][v] = 1;
adj[v][u] = 1;
}
More likely though the segmentation fault is here:
for (b=0;b<=V;++b)
{
cout<<(b+1)<<"| ";
for (c=0;c<=V;++c)
{
cout<<adj[b][c]<<"| ";
}
cout<<endl;
}
the for loop conditionals should be checking b < V and c < V not <=. when either b or c == V you are definitely reading outside the matrix.