Here's my code. Whenever I run the program, I expect that the cout statement in the for loop in the function Binary() to execute. But whenever I input something, it gives me 0
#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
int temp=0,res=0;
for(int i=x;i<0;i--){
temp++;
res=res+ pow(10,temp)*(i%2);
i=i/2;
cout<<i<<" "<<res<<endl;
}
return res;
}
int main(){
int x; cin>>x;
cout<<endl<<Binary(x);
return 0;
}
I think the condition in your for loop is the opposite way round than your expect it. You seem to want it to continue looping until i<0. But it will actually loop while i<0. The condition is already not true when you enter the loop, so it will finish immediately without doing any iterations.
You just have to change the loop i check.
#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
int temp=0,res=0;
for(int i=x;i>0;i--){
temp++;
res=res+ pow(10,temp)*(i%2);
i=i/2;
cout<<i<<" "<<res<<endl;
}
return res;
}
int main(){
int x; cin>>x;
cout<<endl<<Binary(x);
return 0;
}
Check the live version here http://cpp.sh/95z74
Related
I have given an array and I need to find is there any duplicate element present in array or not? (I have to take input from the user.)
This is my first code which is giving the wrong answer for some test cases:
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
unordered_map<int,int> mp;
int temp=1;
for(int i=0,i<n;i++)
{
int x; cin>>x;
if(mp.find(x)!=mp.end())
{
temp=-1; break;
}
mp[x]++;
}
if(temp==-1) cout<<"YES"<<endl; //if duplicate present
else cout<<"NO"<<endl;
mp.clear();
}
return 0;
}
And this code is running perfectly on all the test cases:
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
unordered_map<int,int> mp;
int temp=1;
for(int i=0,i<n;i++)
{
int x;
cin>>x;
mp[x]++;
}
if(mp.size()<n) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
mp.clear();
}
return 0;
}
what is the reason behind it?
You used break; in the first code, but didn't use that in the second code.
Using break; will prevent the code from reading part of test case and treat that as next text case, and it will lead to Wrong Answer.
For example, using this input
2
5 5 5 1 2 3
2 2 2
Your first code will print
YES
NO
while the correct output is
YES
YES
To prevent this, the break; should be removed.
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I have a C++ program, in which I have to create a recursive function that writes all the digits of a given positive integer in an array - in this case a vector.
However, when I compile the program and enter a number, it stops working. I want to ask why this happens?
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
int fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
temp = fill_Array(num)%10;
arr.push_back(temp);
num/=10;
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
In the given code, recursion function does not returning any value, so return type for that function have no use.
calling the function for recursion is in the wrong place. Correct code given below:
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
void fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
else{
temp = num%10;
arr.push_back(temp);
fill_Array(num/=10);
}
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
A couple of reasons I can see:
There is no conditional to stop the recusion so it will keep going until it runs out of stack or memory. I presume you want to stop when num is zero
fill_Array has no return value so will assign temp with some random value which will be pushed into the array
Also why use recursion for this when iterative would be easier and more obvious what it is doing
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m;
while(m!=n){
m=i*j;
cout<<m;
i++;
j++;
}
return 0;
}
I want to display Pronic Number ie . 0,2,6,12,20,30,42... When I insert value of n as Pronic Number Code run fine ... and desire output is given.. Bug is when you insert value of n=15 while loop goes to infinity but i want to display till 15 or less than
15.. Here 15 is not a Pronic Number..
Initialize m before using it, plus change your condition to stop the loop when m>n
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m=0;
while(m<=n){
cout<<m<<" ";
i++;
j++;
m=i*j;
}
return 0;
}
# include <iostream>
# include <cmath>
using namespace std;
int main()
{
int p;
int n;
int q;
cin>>n;
int r;
r=0;
for (int i=0,n; n>1; i=i+1,n=n/2)
{
p=n%2;
q= p*(pow(10,i));
r=r + q;
}
cout<<r;
system("pause");
return 0;
}
I am not supposed to use arrays. It compiles fine but when executed and a number is entered, it doesn't produce the desired results.
For instance, when 22 is entered, it gives -2147483648 whereas the desired output would be 10110.
your way is limited and not effient in converting to binary
you should use string it's more helpful and the range is big enough for any number
this is my code for decimal-to-binary
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
long long n;
string s,bin;
stack<string> res;
cin>>n;
while(n>0)
{
if(n%2==0)
s='0';
else
s='1';
res.push(s);
n=n/2;
}
while(!res.empty())
{
bin=bin+res.top();
res.pop();
}
cout<<bin<<endl;
return 0;
}
I hope it will help you.
int i=0,n;
should be
int i=0;
I don't know what you thought you were doing there, but what you are actually doing is declaring another variable n. Because the second n variable doesn't have a value the rest of the code doesn't work.
That's not the only problem with your code, but I'm sure you can figure out the rest.