Variable size multidimensional array - c++

What is problem with my code for variable size multidimensional array .How to fix this problem.
My code is not passing all test cases.Can anyone help me to fix it.This is question from hackerrank challenge.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,q;
cin>>n;
cin>>q;
int *arr[n];
for(int i=0;i<n;i++)
{
int x;
cin>>x;
int b[x];
for(int j=0;j<x;j++)
{
cin>>b[j];
}
arr[i]=b;
}
while(q--)
{
int i,e;
cin>>i>>e;
cout<<arr[i][e]<<endl;
}
return 0;
}

Here is correct code.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,q;
cin>>n;
cin>>q;
int *arr[n]; // passed all test cases
for(int i=0;i<n;i++)
{
int x;
cin>>x;
int *b=new int[x];
for(int j=0;j<x;j++)
{
cin>>b[j];
}
arr[i]=b;
}
while(q--)
{
int i,e;
cin>>i>>e;
cout<<arr[i][e]<<endl;
}
return 0;
}

Related

how do I declare leastfreq in scope?

Here in the code, I have created a function to calculate the least frequent element. Whenever I run the program it says leastfreq was not declared in the scope.
Can someone tell me how to solve this error and what is this error about?
Error : "error: 'leastfreq' was not declared in this scope"
#include<iostream>
using namespace std;
int main(){
int n,i;
cout<<"Enter the value of n:";
cin>>n;
int a[n];
for (i=0;i<n;i++){
cout<<"Enter element "<<i<<":";
cin>>a[i];
}
for (int i=0;i<n;i++){
printf("%d",a[i]);
}
leastfreq(a,n);
}
int leastfreq(int a[],int arrsize){
int currentct,leastct=0;
int leastelm;
for(int j=0;j<arrsize;j++){
int temp = a[j];
for(int i=0;i<arrsize;i++){
if(a[i]=temp){
currentct++;
}
if(currentct<leastct){
currentct = leastct;
leastelm = a[j];
}
}
}
return leastelm;
}
Solution 1: Put the function leastfreq's definition before main() as shown below:
#include<iostream>
using namespace std;
//leastfreq defined before main
int leastfreq(int a[],int arrsize){
int currentct,leastct=0;
int leastelm;
for(int j=0;j<arrsize;j++){
int temp = a[j];
for(int i=0;i<arrsize;i++){
if(a[i]=temp){
currentct++;
}
if(currentct<leastct){
currentct = leastct;
leastelm = a[j];
}
}
}
return leastelm;
}
int main(){
int n,i;
cout<<"Enter the value of n:";
cin>>n;
int a[n];
for (i=0;i<n;i++){
cout<<"Enter element "<<i<<":";
cin>>a[i];
}
for (int i=0;i<n;i++){
printf("%d",a[i]);
}
leastfreq(a,n);
}
Solution 2: Added a function declaration for leastfreq before main() as shown below:
#include<iostream>
using namespace std;
//added declaration for `leastfreq`
int leastfreq(int a[],int arrsize);
int main(){
int n,i;
cout<<"Enter the value of n:";
cin>>n;
int a[n];
for (i=0;i<n;i++){
cout<<"Enter element "<<i<<":";
cin>>a[i];
}
for (int i=0;i<n;i++){
printf("%d",a[i]);
}
leastfreq(a,n);
}
int leastfreq(int a[],int arrsize){
int currentct,leastct=0;
int leastelm;
for(int j=0;j<arrsize;j++){
int temp = a[j];
for(int i=0;i<arrsize;i++){
if(a[i]=temp){
currentct++;
}
if(currentct<leastct){
currentct = leastct;
leastelm = a[j];
}
}
}
return leastelm;
}

What is wrong with my code in finding the length of Maximum Bitonic Subarray

Logic used is simple if a triplet x,y,z occurs such that x>y<z then give the length of the array to m and continue and it gives right output in all the test cases I can think of
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l=0;
int m=0;
for(int i=0;i<n;i++)
{
l++;
if(i>0&&i<n-1)
{
if(a[i]<a[i-1]&&a[i]<a[i+1])
{
l=1;
}
}
if(m<l)
m=l;
}
if(m)
cout<<m<<endl;
else
cout<<l<<endl;
}
return 0;
}

Recursion function is not responding

# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.

A function for user defined array

Hi this is program I created for making a user defined array.
This is only a small part of my project and I would like to make it into a static function named 'input(n)',where n is the size of the array.
int main() {
int* a=0;
int n,x;
std::cout<<"Enter size ";
std:: cin>>n;
std::cout<<"Enter elements ";
a=new int(n);
for(int i=0;i<n;i++){
std::cin>>x;
a[i]=x;
}
for(int j=0;j<n;j++ ){std::cout<<a[j];}
getch();
}
Any hints on how to get it started?
#include <iostream>
using namespace std;
static int* getInput(int n){
int* a=0;
int x;
a=new int[n];
for(int i=0;i<n;i++){
cin>>x;
a[i]=x;
}
return a;
}
int main() {
int *a;
int n=5;
a=getInput(n);
for(int j=0;j<n;j++ )
{
cout<<a[j];
}
delete[] a;
}
DEMO
int * input(size_t n)
{
int *p =new int[n];
int x;
for(size_t i=0;i<n;i++)
{
std::cin>>x;
p[i]=x;
}
return p;
}
Then,
a=input(n);
Don't forget to free memory.

Trying to implement heapify procedure , could not find error where I am wrong

I am using a simple approach to convert an array into heap in bottom up manner. I could not find the mistake. Can some one please have a look and let me know.
Basically I am making out of a random array in bottom up manner.
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
There is some issue with heapify procedure ,which sometimes give correct result and most of times wrong.
`#include<iostream>
#include<conio.h>
using namespace std;
int min(i
nt x,int y,int z)
{
return ((x>y?x:y)>z?(x>y?x:y):z);
}
int left(int i)
{
return (2*i);enter code here
}
int right(int i)
{
return (2*i+1);
}
int parent(int i)
{
return (i/2);
}
int heapify(int i,int n,int arr[])
{
int temp;
int l=left(i);
int r=right(i);
temp=i;
if(l<=n&&arr[l]<arr[i])
temp=l;
else if(r<=n&&arr[temp]>arr[r])
temp=r;
if(temp!=i)
{
int x=arr[temp];
arr[temp]=arr[i];
arr[i]=x;
heapify(temp,n,arr);
}
//else return 0;
}
int deletemin(int n,int arr[])
{
cout<<arr[1]<<"\ndeLeted\n";
arr[1]=arr[n];
}
int insert(int x,int size,int arr[])
{
arr[size]=x;
int i=(size);
do
{
i=parent(i);
if(arr[i]!=min(arr[i],arr[left(i)],arr[right(i)]))
heapify(i,(size),arr);
else
return 0;
}while(i!=1);
}
int main()
{
int n,t,arr[100];
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>arr[i];
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
deletemin(n,arr);
n--;
heapify(1,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
int y;
cout<<"what value do you want to insert\n";
cin>>y;
n++;
insert(y,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
}
getch();
return 0;
}
`
One simple observation is that your min function is wrong! It doesn't return the min of 3 numbers. Do it with ifs for the moment!