how can i count digits of a number using recursion? - c++

yes i know other ways to count digits and returning to main function from the recursion function, but i'd like to print it in the void function. im having difficulty with it. could somebody help?
#include <iostream>
using namespace std;
void recursive_function(int num)
{
int sum=0;
while(num!=0){
recursive_function(num/10);
sum++;
}
cout<<sum<<endl;
}
int main()
{
recursive_function(345289467);
return 0;
}

If you do not want to use the return-stack to count your digits, you will need to pass the current count throughout the call stack as function parameter:
void recursive_function(int num, int count=1)
{
if (num>=10) {
recursive_function(num/10, count+1);
} else {
cout<<count<<endl;
}
}

your recursive function should return integer.
#include <iostream>
using namespace std;
int recursive_function(int num)
{
if(num>9){
return 1+recursive_function(num/10);
}else
return 1;
}
int main()
{
cout << recursive_function(123456789);
return 0;
}

Related

Binary search optimisation

I implemented binary search in two ways and wondering which is more efficient? please help me know which is more efficient and how can it further be optimized? is time complexity remains same in both approach? I am a beginner in programming.
approach 1;
#include<iostream>
using namespace std;
bool BinarySearch(int*a,int n,int s ){
if(n==1){
if(a[0]==s)
return true;
else
return false;
}
else{
if(s<a[n/2]){
int U[n/2];
for(int i=0;i<n/2;i++){
U[i]=a[i];
}
return BinarySearch(U,n/2,s);
}
else{
int V[n-n/2];
for(int i=0;i<n-n/2;i++){
V[i]=a[i+n/2];
}
return BinarySearch(V,n-n/2,s);
}
}
}
int main(){
int array[10]={2,4,6,8,10,12,14,16,18,22};
cout<<BinarySearch(array,10,9);
}
approach 2:
#include<iostream>
using namespace std;
bool Bsearch(int arr[],int s,int l,int x){
cout<<"calling bsearch with arguments "<<s<<' '<<l<<' '<<x<<endl;
if(l==1)
return arr[s]==x;
int h=l/2;
if(x<arr[s+h])
return Bsearch(arr,s,h,x);
else
return Bsearch(arr,s+h,l-h,x);
}
int main(){
int marks[11]={17,18,20,22,24,26,28,30,32,34,36};
cout<<Bsearch(marks,0,11,32);
}
Thanks in advance for the kind help.
Other posters are correct that variable-length arrays are not good C++. If you define your main() as:
int main() {
std::array<int> marks{17,18,20,22,24,26,28,30,32,34,36};
cout<<Bsearch(marks,0,32);
}
or:
int main() {
std::vector<int> marks{17,18,20,22,24,26,28,30,32,34,36};
cout<<Bsearch(marks,0,32);
}
then you can drop the pointer/length pair in the parameter list to your binary-search function. The function prototype becomes something like:
bool Bsearch(const std::array<int>& arr, int s, int x);

how to efficiently appraoch for SPOJ Square free numbers?

I tried my best to solve Spoj problem No Squares numbers but I'm getting (TLE). Please tell how to approach. I'm unable to find any proper approach. Here is my code:
#include <bits/stdc++.h>
using namespace std;
#define size 10000004
int mark[size+1];
void sieve()
{
for(int i=2,k;(k=i*i)<=size;++i)
{
for(int j=k;j<=size;j=j+k)
mark[j]=-1;
}
}
int fn(int a,int b,int c)
{
int i,j,k,cnt=0;
for(i=a;i<=b;++i)
{
j=i;
if(mark[j]!=-1)
{
while(j>0)
{
if(j%10==c)
{
cnt+=1;
break;
}
else
j=j/10;
}
}
}
return cnt;
}
int main()
{
sieve();
int t,a,b,c;
cin>>t;
for(int i=0;i<t;t++)
{
cin>>a>>b>>c;
cout<<fn(a,b,c)<<endl;
}
}

Control reached end of Non Void Function

I am trying to call this recursive method,but I don't understand why it's not reaching the else statement to return mul. I am passing value 0,in the main function,which is less than n,and on x becoming greater than n,it should move to else function.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n,mul=0;
vector <int> a;
int calc(int x)
{ int mul,chk;
int l=n;
if(x<n)
{
chk=a[x]*(l-1);
l--;
if(mul<chk)
{
mul=chk;
}
calc(x+1);
}
else
{ return mul;}
}
int main() {
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());
int z=calc(0);
cout<<z;
return 0;
}
Wihtout knowing the exact error message, I am not 100% sure what is the problem, but I guess it is the following:
Your function looks like this (simplified):
int calc(int x) {
if(someCondition){
if(otherCondition){}
calc(x+1);
} else {
return mul;
}
}
The problem is that if someCondition is true, the function does not return anything. You probably want to do:
return calc(x+1);
instead of just
calc(x+1);

This code shows error "stu undeclared"?? what should i do

I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)

arrays and index 2

Merged with arrays and index [closed].
Original Post
Guys ive figured it out but when i type the same numbers again still it was not rejected even though i already insert it in the index and it limit only for 5 index...heres the code
#include <iostream>
using namespace std;
int main()
{
int x,y;
int number;
int arr[5];
for (x=0;x<=5;x++)
{
cout<<"enter nmber"<<endl;
cin>>number;
bool replace=true;
for (y=0;y<x;y++)
{
if (number == arr[y])
{
replace=false;
break;
}
}
if (replace)
{
arr[x]=number;
x++;
}
}
return 0;
}