how to handle large array size in struct - c++

i am working on a code which increases size of diagonal elements by 1
here is the working implementation
consider 5x5 matrix
#include<iostream>
using namespace std;
struct abc
{
int b[100];
}arr[100];
int main()
{
for(int i=0;i<5;i++)
arr[i].b[i]+=1;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
cout<<arr[i].b[j]<<" ";
cout<<endl;
}
//system("pause");
return 0;
}
The code works fine for small range of arr,but i need it to work for 10^5.Any Suggestions?

I think its better to perform such memory allocations on the heap.

Related

What is causing SIGSEGV error in my code?

I am neither using pointer nor using freed memory but i do not understand what is causing sigsegv error.
For some test cases the algorithm is working without any error while for other test case it is showing SIGSEGV.
#include<iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
int arr[n],arrh[m];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<m;i++)
{
cin>>arrh[i];
}
int arrcc[m][n]={0}; //Precomputation Cumulative
int val;
for(int j=0;j<m;j++)
{
val=0;
for(int i=0;i<n;i++)
{
if(arr[i]==(j+1))
{
val++;
}
arrcc[j][i]=val;
}
}
int q,l,r;
cin>>q;
int k;
int arrc[m];
while(q--)
{
k=0;
for(int i=0;i<m;i++)
{
arrc[i]=0;
}
cin>>l>>r;
// for(int j=0;j<m;j++) //Time limit exceed need precomputation
// {
// for(int i=(l-1);i<r;i++)
// {
// if(arr[i]==(j+1))
// {
// arrc[j]++;
// }
// }
// }
for(int j=0;j<m;j++) //Calculating from cumulative
{
if(l!=1)
arrc[j]=(arrcc[j][r-1]-arrcc[j][l-2]);
else
arrc[j]=(arrcc[j][r-1]-0);
}
for(int i=0;i<m;i++)
{
if(arrc[i]!=0)
{
if(arrc[i]!=arrh[i])
{
cout<<"0"<<"\n";
k++;
break;
}
}
}
if(k==0)
{
cout<<"1"<<"\n";
}
}
}
link to problem the problem-
https://drive.google.com/open?id=1sYEbtdFTT9ZE67y4Wygvv_AKJWGWgXiI
https://www.hackerearth.com/challenges/competitive/april-circuits-20/algorithm/happy-segments-e290faa6/
cin>>n>>m;
int arr[n],arrh[m];
Firstly, the program is ill-formed in C++. The size of an automatic array must be compile time constant, which n and m are not.
Secondly, in case you intentionally use an extended C++ language: The storage for objects in automatic storage is typically strictly limited. The size of the execution stack is by default about 1-8 mega bytes. For large n or m, those arrays may overflow the stack which, if you are lucky, will cause the program to crash.
If you need an array of runtime size, then use dynamic storage. Simplest solution is to use std::vector. This removes the chance of the array overflowing the stack, and makes the program well-formed C++ in regard to the array size.

output is not what expected

#include<iostream>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
int swapper(int n[],int s
)
{
for(int i=0;i<s;i++)
{
int temp=n[i];
n[i]=n[i+1];
n[i+1]=temp;
}
cout<<"\n Array Swapped !!!";
for (int i=0;1<s;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
int main()
{
int n[20]; int s;
cout<<"\n enter array size:";
cin>>s;
cout<<"enter no in array according to size given";
for(int j=0;j<s;j++)
{
cin>>n[j];
}
swapper(n,s);
return 0;
getch();
}
the output to this program does not swap the array elements,
instead it starts producing numbers in plenty
the entire code is written here
all other suggested changes have been made
the function is supposed to take in an integer array and its size as parameters and display an array with its adjacent elements swapped.
The call
swapper(n[],s);
is not right. Assuming the first argument of swapper is a int*, it needs to be:
swapper(n,s);
Remove the [] in your swapper function.

Bubble sort method for array with dynamically determined size

I am trying to use bubble sort method for array with dynamically determined size. Here's the code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter n";
cin>>n;
int arr[n],swap;
cout<<"Enter number"<<endl;
cin>>arr[n];
for(int i=0;i<n-1;i++)
for(int j=0;i<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
When I define the elements of the array in this way the program works:
const n=5;
int arr[n]={1,2,3,4,5)
But I need to enter the size of the array (n) and its elements from the keyboard. But when I run my code the program crashes after I enter the first number. Is there a way to fix it?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, swap, temp;
vector<int> arr;
cout<<"Enter n";
cin>>n;
// Loop and accept the n values.
// You may need to take care of the new line.
for(int i = 0; i < n; ++i)
{
cout << "Enter a number : ";
cin >> temp;
arr.push_back(temp);
}
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
Notice how a loop is used to extract n values from user. Also using a std::vector relieves you from writing code for a runtime sized array with new and delete.
Also, you inner loop was checking i<n-i-1 and incrementing j which should be j<n-i-1 instead, otherwise j will increment indefinitely till INT_MAX.
The key word is dynamic allocation. In C, the function is malloc. In Cpp, it can be new and delete. Although a vector can work well, it is just a kind of method by STL. Notice that my code may have safe problem.
#include <iostream>
using namespace std;
int main()
{
int n,temp;
cout<<"Enter n:";
cin>>n;
//dynamic allocation
int *arr=new int[n];
cout<<"Enter number."<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
//bubble sort
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//output the array
for(int i=0;i<n;i++){
cout<<"arr["<<i<<"]="<<arr[i]<<endl;
}
delete [] arr;
return 0;
}
You can't take an integer array like that, you need to run a loop. You can take a string like that. There are lot of errors in the bubble sort logic as well, try the below code snippet. It should work fine. You need to dynamically allocate the array for arr
int n,r,swap,i,*arr;
cout<<"Enter n\n";
cin>>n;
arr = (int *)malloc((n)*sizeof(int));
cout<<"Enter numbers\n"<<n<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)//You're checking for i. you need to check for j
{
if(arr[j+1]<arr[j])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
//now print your arr
Includes : #include<stdlib.h>
As mentioned in my comment, you CANNOT dynamically declare the size of arrays in C++ (use std::vector if you are willing to achieve that).
You can then do this:
....
cin >> n;
vector<int> arr(n); // reserves space for `n` integers in the memory
....

Count of Maximum code chef.Runtime Error(SIGSEGV)

Hi I started doing code chef beginner problems and got stuck at this one.I tried reducing the stack memory by declaring the arrays globally but that doesn't work too.Here is my code
#include<iostream>
using namespace std;
#define max 101
int main()
{
int t,n;
cin>>t;
while(t--)
{
int a[max];
int c[max]={0};
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
c[a[j]]++;
}
int temp=0;
int x=0;
for(int k=0;k<n;k++)
{
if(c[k]>temp)
{
temp=c[k];
x=k;
}
}
cout<<x<<" "<<temp<<endl;
}
}
You may need a data structure that does not limit the input value to be bounded, or just change your algorithm.
Either use std::map<int, int> in place of c to count occurence of each number, or just sort a to aggregate same values and count.

I am getting thread1:SIGNAL sigbart in output

this is my code, PLease help me ! im using xcode.. i want to generate a sequence for a polynomial and the terms are xor'ed and made a feedback to the first input bit since it is 8 bit it is done 2^8-1 times.Alternate code will also be helpful Thanks in advance
#include "32bit.h"
#include<iostream>
using namespace std;
int main()
{
bool input[8];
int n;
bool out=0;
cout<<"Enter the no of terms ";
cin>>n;
int temp1[n];
int gen=0;
bool store[255];
cout<<"Input power of x in increasing order, Omit x^0";
for(int i=0;i<n;i++)
cin>>temp1[i];
cout<<"Enter key to generate ";
cin>>gen;
for(int m=0;m<255;m++)
{
store[m]=input[gen];
bool temp2[n];
int var=0;
for(int j=0;j<n;j++)
{
var=temp1[j];
temp2[j]=input[var];
}
int c=0;
for(int k=0;k<n;k++)
{
if(temp2[k]%2==1)
c++;
}
if(c%2==1)
out=1;
else
out=0;
for(int l=0;l<8;l++)
input[l+1]=input[l];
input[0]=out;
}
for(int p=0;p<255;p++)
cout<<store[p];
}
There is an out of bounds array access here:
for(int l=0;l<8;l++)
input[l+1]=input[l];
since input is only of size 8 and you are trying to write to input[8] (i.e. the non-existent 9th element) on the last iteration of this loop. I'm guessing it should probably be:
for(int l=0;l<7;l++)
input[l+1]=input[l];