C++ Reverse a number of any length - c++

I want to reverse the number given by the user. I wrote the code in such a way that it takes the number of digits of number and the input and reverses it. As even the limit of 'long long' is 19 digits, what can I do so that the code works even if the number of digits in the input are greater than 20? [Without using third party libraries]
#include<iostream>
using namespace std;
void make_int(int a[],long long int n)
{
int i=0;
while(n)
{
a[i]=n%10;
i++;
n=n/10;
}
for(int j=0;j<i;j++)
cout<<a[j];
}
int main()
{
int N;
cin>>N;
int *tc = new int[N];
long long int num;
cin>>num;
make_int(tc,num);
return 0;
}

Read the number in as a std::string and reverse that, since it's actual numerical value is unimportant for what you want to do.

Related

Why this code doesn't allow me to receive the whole array?

#include <bits/stdc++.h>
using namespace std;
string bin(int n){
string x="";
while(n!=0)
{
int z=n%2;
x+=to_string(z);
n%=2;
}
return x;
}
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];
string x=bin(a[i]);
int u=x.size();
int cnt=0;
for(int g=0;g<u;g++)
{
if(x[g]=='1')
++cnt;
}
cout<<cnt<<' ';
}
cout<<'\n';
}
}
This code is given several test cases and each test case will have an array of n integers, for each element in the array I should count the number of ones in the binary representation of it. I wrote a function that expects an integer and returns a string containing the binary representation of it. But I wonder why my code does not end, and not allowing me to receive other numbers in array.
For instance, there's one test case and and only array of 2 integers if I inputted 1 and wait for ever to enter the second number, what's happening?
This is your bin function reduced to the bare minimum:
string bin(int n){
while(n!=0)
{
n%=2;
}
return {};
}
If n is even you will set it to 0 on the first iteration, otherwise you set it to 1 and never change it afterwards (1%2==1). Hence you have a endless loop. I won't spoil you the "fun" of completing the exercise, so I will just point you to using a debugger. If you step trough your code line by line you could have observed how n never changes and why the loop wont stop.
PS: (spoiler-alert) you might want to take a look at std::bitset (end of spoiler)

Why is this program asking for more input than needed?

It is a simple program to calculate the next number of a given number by adding one to it. By during execution it asks more inputs than needed and gives a wrong result. But when I give input through file, it gives right answer. I tried debugging but only thing i found was my input loop was running more than the number of times i specified. I am a newbie to this. Please help.
//Simple program to add 1 to the number given. Digits in number can be upto 10^9
#include <iostream>
#include <cstdio>
#define DD cout<<"Working!"
using namespace std;
void JNEXT()
{
long n;//total length of the digits, you will give
cin>>n;
long all_nine=0;//for checking whether all are nine or not
long a[n+1]={0};//initializing the array with zero
for(long i=0;i<n;i++)//taking input and storing in a[]
{
cin>>a[i];
if(a[i]==9)
all_nine++;
DD;
}
if(all_nine==n)//if all digits are nine then print next number by adding one
{
cout<<1;
for(long i=0;i<n;i++)
cout<<0;
cout<<endl;
}
else
{
int carry=1;
for(long i=n-1;i>=0;i--)
{
a[i]+=carry;
if(a[i]==10)
a[i]=0;
else carry=0;
}
for(long i=0;i<n;i++)
cout<<a[i];
cout<<endl;
}
}
int main()
{
int t;//Number of Queries you want to check(process)
cin>>t;
while(t-- > 0)
JNEXT();
return 0;
}
Edits: I got the answer Thanks!

Google code jam minimum scalar product not working on my selection sort (c++)

I was practicing previous year's code jam problems and found minimum scalar product.
Problem Link : https://code.google.com/codejam/contest/32016/dashboard#s=p0
I know how its algorithm works. We sort both arrays v1 and v2 then multiply v1[j]*v2[n-1-j]. The algorithm works fine when I use c++ default sort() function. But if i use my own sorting function (selection sort), I get different output.
Further by observing the correct output file i noticed that if all the input numbers and positive my output is correct. However for negative numbers it is incorrect.
Here is the code of my sorting function:
`void sorted(long long int *a,int n)
{
long long int temp;
int minIndex;
for(int i=0;i<n;i++)
{
minIndex=i;
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
minIndex=j;
}
if(minIndex!=i)
{
temp=a[minIndex];
a[minIndex]=a[i];
a[i]=temp;
}
}
}
`
Note that for this problem we have to use long long int because input numbers exceed int limits.
This is my main function:
#include <iostream>
#include<fstream>
using namespace std;
void sorted(long long int *a,int n);
int main()
{
ifstream inp("input.in");
int T;
inp>>T;
int n[T];
long long int *x[T], *y[T];
for(int i=0;i<T;i++)
{
inp>>n[i];
x[i]=new long long int[n[i]];
y[i]=new long long int[n[i]];
for(int j=0;j<n[i];j++)
inp>>x[i][j];
for(int j=0;j<n[i];j++)
inp>>y[i][j];
}
long long int minProduct[T];
ofstream out("output.txt");
for(int i=0;i<T;i++)
{
minProduct[i]=0;
sorted(x[i],n[i]);
sorted(y[i],n[i]);
for(int j=0;j<n[i];j++)
minProduct[i]=minProduct[i]+(y[i][n[i]-1-j]*x[i][j]);
out<<"Case #"<<i+1<<": "<<minProduct[i]<<endl;
}
return 0;
}
if i replace
sorted(x[i],n[i]);
sorted(y[i],n[i]);
with
sort(x[i],x[i]+n[i]);
sort(y[i],y[i]+n[i]);
and including algorithm header file, my output is correct.
What is the mistake in my sorting algorithm?
In the loop where you find the minimum index, you must compare the current item at index j with the item at the current index of the minimum item:
minIndex = i;
for (int j = i + 1; j < n; j++) {
if (a[minIndex] > a[j])
minIndex = j;
}
You always compare with the item at index i and hence don't account for updates on minIndex.

I need to make a number input by user to output individual digits using C++

I need to make a number that was input by a user into individual digits using only while and for loops.
I used while loops to count the number of digits in the number but for my for-loop I cannot seem to get it processed. What am I doing wrong?
I do know I need to do 10^ number of digits, but I don't know how to do that since ^ does not exist in C++.
#include <iostream>
using namespace std;
int main(){
int n;
cout<< "please enter an integer: ";
cin>>n;
int a=n;
int count=0;
while(a>0){
a/=10;
count++;
}
int num= 10*(count-1);
for (int n=0;num<0;n++){
cout<<n/num<<endl;
n%=num;
num/=10;
}
return 0;
}
The code fails for numbers which have greater than 2 digits, because value of num must 10^(count-1) not 10*(count-1)
There is a function pow(base,exp) in C++ which can be used in this case as num=pow(10,count-1)
or you could try creating num variable while counting the number of digits:
int count=0,num=1;
while(a>0){
a/=10;
count++;
num*=10;
}
num = num/10;
for (int i=0;i<count;i++){
cout<<n/num<<endl;
n%=num;
num/=10;
}
return 0;
Adding last line just to remove an extra 10, since num=10^(count-1)

Unexpected results converting decimal number to binary

# 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.