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 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;
}
I wrote this code to solve N-Queens problem.
and sometimes ex) 6 and 10 there are an error ; free(): invalid next size (fast) but sometimes like 8 and 2 it isn't
I don't know why it happened in specific case....
#include <iostream>
using namespace std;
bool check(int *a,int num){
for(int i=0;i<num;++i){
if((double)(a[i]-a[num])/(i-num)==1.0||(double)(a[i]-a[num])/(i-num)==-1.0||a[i]==a[num])
return false;
}
return true;
}
int func(int *array,int num,int n){
int sum=0;
for(int i=0;i<n;++i){
array[num]=i;
if(num==n-1){
if(check(array,num))
sum+= 1;
}
if(check(array,num)) sum+=func(array,num+1,n);
}
return sum;
}
int main(){
int N=0;
cin>>N;
for(int i=0;i<N;i++){
int n=0;
cin >> n;
int *array=new int[n];
cout<<"+++++++++"<<n<<endl;
int a = func(array,0,n);
delete[] array;
cout<<a<<endl;
}
return 0;
}
I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.
# 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.