I'm having problem with vector, (in the usage of push_back) but it only appears when using additional g++ flag -O2 (I need it).
#include <cstdio>
#include <vector>
typedef std::vector<int> node;
typedef std::vector<node> graph;
int main()
{
int n, k, a, b, sum;
bool c;
graph g(n, node());
c = scanf("%i%i", &n, &k);
for(int i=0; i<n; i++)
{
sum=2;
for(int j=0; j<i; j++)
sum*=2;
for(int j=0; j<sum; j++)
{
if(j%2==0)
c = scanf("%i", &a);
else
{
c = scanf("%i", &b);
a += b;
g[i].push_back(a); //---------------LINE WHICH CAUSES SEGMENTATION FAULT
}
}
}
for(int i=n-2; i>=0; i--)
{
for(size_t j=0; j<g[i].size(); j++)
{
if(g[i+1][(j*2)] >= g[i+1][(j*2)+1])
g[i][j] = g[i+1][j*2];
else
g[i][j] = g[i+1][(j*2)+1];
}
}
printf("%i\n", g[0][0]);
return 0;
}
I think you have:
graph g(n, node());
c = scanf("%i%i", &n, &k);
in the reverse order. As it stands, the variable 'n' which you use to size graph is not initialised.
Initializing the vector with n before the input operation means you're invoking the dreaded Undefined Behavior. As stated here, the program is allowed to do anything after that.
Works perfectly if you initialize n as I already mentioned in my comment. Change the first lines to:
int n, k, a, b, sum;
int c;
c = scanf("%i%i", &n, &k); // initialize n *first*
if(c != 2) return -1; // scanf does not return bool but the number of parsed arguments
graph g(n, node());
Related
I wrote the following code-
#include <iostream>
#include <tuple>
int sumarr(int64_t arr[], int length)
{
long long int sum= 0;
for(int i=0; i<length; i++)
{
sum += arr[i];
}
return sum;
}
void minimaxval(int64_t arr[], int length,int64_t *x,int64_t *y)
{
long long int c=0;
long long int d=10e9;
for(int j=0; j<5; j++)
{
if(arr[j] >= c)
{
c = arr[j];
}
if (arr[j] <= d)
{
d= arr[j];
}
}
*x=c;
*y=d;
}
int main()
{
int64_t arr[5];
int64_t p, q;
int64_t sum;
for(int k=0; k<5; k++){
std::cin>> arr[k];
if (arr[k]<1 || arr[k]>10e9){return 0;}
}
sum= sumarr(arr, 5);
minimaxval (arr, 5, &p, &q);
std::cout << sum-p << " " << (sum-q);
return 0;
}
This is my first question on stackoverflow. So I dont know a lot about format.
Input for the que. will be 5 space-separated integers.
Output should be the minimum sum and the maximum sum we can get by removing only one integer from the list.
It does not show any compiler issues.
It works fine for smaller integers but outputs a negative value for larger integers.
Where did I go wrong.
In 'summar' you have wrong return type. If you change it to int64_t everything will be fine.
P. S. And yes this is also my first answer)
I am trying to implement quick sort to sort a sequence of integers.
I am getting a segmentation default with the following code:
I implemented partition and quick sort recursive calls.But for some c++ reason I am getting an access to memory or an infinite loop I cant understand why.
#include <fstream>
#include<vector>
#include<iostream>
using namespace std;
int pivotSelection(vector<int> A){
return 0;
}
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=0; j< A.size(); j++) {
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i;
}
vector<int> readArray(char* file){
ifstream inFile;
vector<int> A;
inFile.open(file);
int x;
while (inFile >>x ) {
A.push_back(x);
}
return A;
}
void quickSort(vector<int> &A, int l,int r){
if(r==1) {
return ;
}
if(r>l){
int p= partition(A,l,r);
quickSort(A,l,p-1);
quickSort(A,p+1,r);
}
}
int main(){
vector<int> A;//= readArray((char*)"/home/brunoeducsantos/AlgorithmFoundation/quicksort/data.txt");
A.push_back(3);
A.push_back(5);
A.push_back(7);
A.push_back(1);
int length = A.size();
quickSort(A,0,length-1);
for(int i=0;i<length;i++) cout<<A[i]<<endl;
return 0;
};
The expected result is: 1 3 5 7
Fixes noted in comments:
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=i; j<=r; j++) { // fix
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i-1; // fix
}
Getting the error
no matching function for call to 'begin(int [n])'
I am using vectors and array and set but can't find out the reason for the error.
P.S. - I googled it but also couldn't find out anything relevant.
I tried debugging it but couldn't do it.
Here's my code!
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, flag = 0;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int index1 = distance(begin(a), find(begin(a), end(a), 2));
std::set<int> sa(a, a + n);
std::vector<int> vec(sa.size());
std::copy(sa.begin(), sa.end(), vec.begin());
int arr[vec.size()];
copy(vec.begin(), vec.end(), arr);
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < n; j++) {
if (arr[i] == a[j]) {
int index1 = distance(begin(a), find(begin(a), end(a), i));
int index2 = distance(begin(a), find(begin(a), end(a), j));
if (index1 < n && index2 < n) {
flag = 1;
break;
}
}
}
}
if (flag) { cout << "Truly Happy\n"; }
else if (!flag) {
cout << "Poor Chef\n";
}
}
return 0;
}
Your problem is variable sized arrays, which are not part of C++ standards, and you are using it in your code.
Use std::vector<> instead of them. That means, change this
int a[n]; // to ----------------------------> std::vector<int> a(n);
and this line
std::set<int> sa(a, a + n); // to ----------> std::set<int> sa(a.begin(), a.end());
also
int arr[vec.size()]; // to -----------------> std::vector<int> arr(vec.size());
copy(vec.begin(), vec.end(), arr); // to ---> copy(vec.begin(), vec.end(), arr.begin());
then your code will work.
However,
Here
for (int i = 0; i < n; i++) { scanf("%d", &a[i]); }
it looks like you are trying to have an array filling elements from
0, 1, 2,... , n-1. This could be easily done by
std::iota. That means, the following is equivalent to the for loop, which you wrote.
std::iota(a.begin(), a.end(), 0);
Secondly, you are doing so many copying, which does not look like a
good algorithm. Especially, coping with the set sa and again coping it back to another vector(vec). This is definitely, not you
want I guess.
You do not need to use std::find on vector/ array a. Since it is
in sorted order the relation between array index and array
element is of difference 1, make use of this information to find
the index.
PS: Do not use #include<bits/stdc++.h>, see this post for more info and
using namespace std; is not a good coding practices.
I am having a problem in getting the contents of the variable 'arr' which is an array of pointers.
I tried,p *arr#n, but it gives the following output: $1 = {0x603010, 0x603030}.
What should I do?
int n, q;
scanf("%d %d", &n, &q);
int lastAnswer=0, index_size[n], *arr[n]; // <-- here
for(int i=0; i<n; i++)
index_size[i] = 0;
for(int i=0; i<n; i++) {
int *temp = malloc(sizeof(int)*n);
arr[i] = temp;
}
while(q--) {
int w, x, y, seq;
scanf("%d %d %d", &w, &x, &y);
if(w == 1) {
seq = ((x ^ lastAnswer) % n);
arr[seq][index_size[seq]++] = y;
}
else {
seq = ((x ^ lastAnswer) % n);
lastAnswer = y%n;
printf("%d\n", lastAnswer);
}
}
return 0;
If you print out a pointer itself, it would just give you an address in your memory block.
So print *arr#n would simply give you the content of the first dimension (an array of address in your output)
If you want to print out the deeper content. You might want to do something like this:
print **arr#n;
or
print *arr[0]#n
Another method would be define a pretty print function inside your program and call it in gdb.
void print(int arr[][], n, m)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
printf("%d ", arr[i][j]);
printf("\n");
}
}
And call it in gdb by
call print(arr, n, m)
I don't think gdb support printing 2D array itself, why?
Because the definition of print *array#3 isn't printing the first three elements in array, instead, it is "priting *array (or array[0]) and the three elements following array[0].
print **arr#n#n
would not work in this case, (although it print out an nice format)
#include<stdio.h>
#include<stdlib.h>
int* getEvenNumbers(int arr[], int N)
{
int i, k = 0 , a[50], p;
for (i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
{
arr[k]=arr[i];
k++;
}
}
return arr[k];
}
int main ()
{
int i, arr[5000000], N, a[500000], k, *p;
printf("\nEnter your desired length of the array:\n\n");
scanf("%d", &N);
for (i = 0; i < N; i++)
arr[i]= rand();
getEvenNumbers (arr, N);
printf("\n\nEven numbers in the array are as follows:\n\n");
for (i = 0; i < N; i++)
{
a[i]= *(p+i);
printf("\n[%d] = %d", (i+1), a[i]);
}
}
please i know this is probably very easy for you guys but i need help figuring out how to return a pointer to the array without all my values of my array getting deleted, also i can't use global variables and it has to be a function that returns a pointer pointing to the array
First of all, decrease the size of those arrays, you don't need that much space. Second of all, you made your
getEvenNumbers
function return an int *, and not an int. arr[k] is not an int *. I also don't get why you are returning something if nothing is being assigned when you call the function. You can just change the return type to void.
void getEvenNumbers(int arr[], int N)
You also never allocate any memory for p. You can do
p = (int*) malloc(sizeof(int));
And since you never allocated any memory for p, the following line of code
a[i]= *(p+i);
is assigning a[i] to a random address. You should just try to rewrite this program. There a lot of errors in this program that I didn't even correct. Go on Google an look up finding even numbers in array program or something similar and look at the code of those examples.
EDIT:
I found some code examples for you to use. I hope it helps!
StackOverflow
sanfoundry.com
The caller already knows the address of the array, so you just need to return the new length. If we also remove the unused variables and take advantage of C++ declarations, we will have:
int getEvenNumbers(int* arr, int N)
{
for (int i = 0, k = 0; i < N; i++) {
if (arr[i] & 1 == 0) { // even if lowest bit is zero
arr[k] = arr[i];
k++;
}
}
return k;
}
Now you can print the even numbers easily:
int k = getEvenNumbers(arr, N);
printf("\n\nEven numbers in the array are as follows:\n\n");
for (i = 0; i < k; i++) {
printf("\n[%d] = %d", (i+1), arr[i]);
}
Dynamically allocate memory from heap.
int* a= new int [N];
//Now store the elements from index 1.
// at a[0] store the number of even number you have found in this function.
return a;
In main you know how many even numbers are there.
int *a1=getEvenNumbers(arr,n);
count_even=a1[0];
for(index=1;index<=count_even;index++)
cout<<a1[index];
The code is given here-
#include<stdio.h>
#include<stdlib.h>
int* getEvenNumbers(int arr[], int N)
{
int i, k = 1 , p;
int* a=new int[N+1];
for (i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
a[k++]=arr[i];
}
a[0]=k-1;
return a;
}
int main ()
{
int i, N;
printf("\nEnter your desired length of the array:\n\n");
scanf("%d", &N);
int arr[N];
for (i = 0; i < N; i++)
arr[i]= rand();
int *a=getEvenNumbers (arr, N);
printf("\n\nEven numbers in the array are as follows:\n\n");
for (i = 1; i <= a[0]; i++)
printf("\n[%d] = %d", (i), a[i]);
delete []a;
}
A better option is to use std::vector.You can read it here.