how do I declare leastfreq in scope? - c++

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;
}

Related

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;
}

Variable size multidimensional array

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;
}

Why sometimes free() : invalid next size (fast) occur and sometimes not?

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;
}

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.