Illegal use of floating point in c++ - c++

I am getting an illegal use of floating point error in my programme:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int number,reverse,check,i,j,k=0,x;
cout<<"Please enter number: ";
cin>>number;
//Obtaining no. of digits:
for(i=1;check==0;i++)
{
check/=10;
}
//Reversing number:
if(i%2==0) //case even digits
{ for(j=i;j>0;j--)
{
x=(number%pow(10,j))/pow(10,j-1); //here
reverse+=x*pow(10,k);
k++;
}
}
cout<<"Reverse number: "<<reverse;
getch();
}
I have no idea why this error is appearing, it would be great if someone could help me with this

pow is a function that returns a double. and the C++ modulo operator % works only with integer numbers. That's because the mathematical modulo operator is defined for integers. Hence the illegal use.
Besides, you use the check variable without initialization. Initialize all you variables before use, in order to avoid further surprises.
EDIT
Here are some other corrections to do:
check = number before the first for loop.
for(i = 0; check != 0; i++)
Finally, try to find another way to get the digits of the number, without using pow. There are simple ways using consecutive integer division.

Related

Why does my program(code chef- two numbers) crash when i give higher values to variables

Question: https://www.codechef.com/problems/TWONMS.
My code:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
int t,a,b,n,c,d,FinalNumber;
cin>>t;
for(int i= 0;i<t;i++){
cin>>a;
cin>>b;
cin>>n;
c=a;
d=b;
int n1 = n/2;
int n2 = n - n1;
while(n1 != 0){
c=c*2;
n1--;
}
while(n2 != 0){
d=d*2;
n2--;
}
if(c>d){
FinalNumber = c/d;
cout<<FinalNumber<<endl;
}
else{
FinalNumber = d/c;
cout<<FinalNumber<<endl
}
}
}
This program crashes when i enter the value of N above 30,what can i do to make it handle bigger numbers?And also is my program correct?
Based on your code, n1 which is initially assigned n/2 is used as a counter for a doubling operation on c: c=c*2;.
Depending on the value of c, d. and n, there could certainly be overflow happening which may cause a range of problems.
Note that these coding exercises are not a good way to learn how to program well. Although they could be stimulating you in improving your skills, they often encourage users to write bad code quality.
In your code, c and d goes into integer overflow: your number is way too big to fit in a 32 bit integer number.
Websites like codechef expects you to think further: applying the brute force algorithm will only work on small inputs, you would have to find a better approach to solve the problem with the constrains that are given.

SIGABRT error for - https://www.codechef.com/problems/PRIME1

I am using sieve of eratosthenes to solve this problem but it is giving me SIGABRT error although my code is working fine on codeblocks....
Please help me modify this code to remove error....
My code is...
#include<vector>
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
unsigned long int t, n, m,i,j;
vector<int> prime;
cin>>t;
while(t--)
{
cin>>m;
cin>>n;
while(!(1<=m&&m<=n&&n<=1000000000&&n-m<=100000))
cin>>m>>n;
prime.resize(n);
for(i=0;i<n;i++)
prime[i]=1;
prime[0]=0;
prime[1]=0;
for(i=2;i<sqrt(n);i++)
{
if(prime[i]==1)
{
for(j=i;i*j<=n;j++)
prime[i*j]=0;
}
}
for(i=m;i<=n;i++)
{
if(prime[i]==1)
cout<<i<<endl;
}
cout<<endl;
prime.resize(0);
}
return 0;
}
Your j loop allows i*j to equal n, but the vector of size n must be indexed from 0 to n-1. The existing code permits referencing an element out of bounds.
The same problem can occur in the last loop, too.
The SIGABRT is issued by library routines.
You have two library routines in your program: std::vector and sqrt.
Either assign sqrt(n) to a const variable or replace the condition with:
(i * i) < n;
You need to verify that:
prime[i*j]
is a valid location. In other words, (i * j) < n.
Some information about primes (that can help you code your program):
Prime numbers are odd except the value 2.
Your test value can start at 3 and add 2 to get to the next value.
You may be able to save some time by looking values in an array of
known values.
Multiplication is usually faster than division. Try rewriting your
test to use multiplication and not division.
Use a data type that can contain the maximum value.

error C2064: term does not evaluate to a function taking 1 arguments for the function 1/n(n+1)

Trying to use the function (1/n(n+1)) to calculate the xNumber of a user prompted integer "n". However, it seems that this particular function gives an error when I try to run it. The functions 1/n and 1/(n+1) work just fine in the program but for some reason adding the n to multiply causes the program to not run.
#include<stdio.h>
int main(){
int n,i=1,a=1,nstart;
float xNumber=1;
printf("Please enter a positive integer number: ");
scanf("%d",&n);
printf("Number\t\txNumber\n");
printf("------\t\t----\n");
for(i=nstart;i<=n;i++){
xNumber += 1/(n(n+1)) ;//(n(n+1));
}
printf("%d\t\t%d\n\n",n,xNumber);
return 0;
}
nstart is uninitialized so this is undefined behavior. Simply initialize at declaration:
int n,i=1,a=1,nstart = 0;
Here's the reason for your error:
n(n+1) is valid mathematical notation, but in programming it is a function call.
Unfortunately for you, n is not a function.
You must write this formula like so:
n*(n+1)
Other answerers have pointed out tangential issues with your code which you must also address.
Couple of corrections and initializations:
int n,i=1,a=1,nstart=0;
and
xNumber += 1/(n*(n+1)) ;//(n(n+1));
Here is the program:
#include<stdio.h>
int main(){
int n,i=1,a=1,nstart=0;
float xNumber=1;
printf("Please enter a positive integer number: ");
scanf("%d",&n);
printf("Number\t\txNumber\n");
printf("------\t\t----\n");
for(i=nstart;i<=n;i++){
xNumber += 1/(n*(n+1)) ;//(n(n+1));
//^^^^^^^^^
}
printf("%d\t\t%d\n\n",n,xNumber);
return 0;
}

decimal to binary converter c++?

int inputNumber=0;
int divisionStore=0,modStore=0;
vector<int> mainVector;
cout << "\nEnter a Number to Convert to Binary.\n" << endl;
cin >> inputNumber;
do
{
modStore=inputNumber%2;
inputNumber=inputNumber/2;
mainVector.push_back(modStore);
}while(inputNumber!=1);
for (int i=0;i<mainVector.size();i++)
{
cout<<endl<<mainVector[i]<<endl;
}
Seems like there is a logical error but I cant find whats wrong with it? The program does not print the correct conversion as it seems like the loop ends before it can push the last number.
I think you need to change:
}while(inputNumber!=1)
to:
}while(inputNumber!=0)
Why not use the STL - i.e. bitset
See http://www.cplusplus.com/reference/bitset/bitset/to_string/
Probably do it in a couple lines of code!
Seams to be a very complicated and inefficient way (if I don't misunderstand you). You are probably looking for bit-manipulation operators, not /, % etc. If you really want to stick it in a vector this should do it:
while (inputNumber) {
mainVector.push_back(inputNumber & 1);
inputNumber >>= 1;
}
Note however that this will put the least significant bit at the beginning of the vector, may not be what you want but looks like it is what your code is trying to do as well.
I answered similar question Here, I used recursion function
void decimal_to_binary(int decimal)
{
int remainder = decimal % 2;
if (decimal < 1)
return;
decimal_to_binary(decimal / 2);
cout << remainder;
}
inputNumber%2 is the least significant bit, so your vector will contain the bits in reversed order. Simply loop the vector in reversed order.
while(inputNumber!=1) - You have to loop while inputNumber!=0, otherwise you won't process the last bit.
#include<iostream>
using namespace std;
int main(){
int x,bin;
x=0 ,bin=0;
int ar[10000];
cout<<"Enter Decimal Number "<<endl;
cin>>x;
int n=0;
while(x>0){
bin=x%2;
x=x/2;
ar[n]=bin;
n++;
}
cout<<endl;
n=n-1;
for(n;n>=0;n--){
cout<<ar[n]; }
cout<<endl;
return 0;
}
/* Try this */

Beginner C++: Strange Behaviour

thanks for taking the time to read this question!
The program is to find the smallest prime number after one billion. at the end of int main() i included a console input cin>>x;with the intention of preventing the command prompt from closing too quickly so i can see the result. however, i realised that i must first enter something before it shows me the result i want.
SO the question is: why is this so even though the console output statement cout<<i;is before the input statement cin>>x;?
#include <iostream>
#include <math.h>
using namespace std;
int is_prime(int x);
int main()
{
for (int i=100000000;;i++){
if(is_prime(i)){
cout<<i;
break;}
int x;
cin>>x;
}
}
int is_prime(int x)
{
double maxvalue = sqrt(static_cast<double>(x));
for ( int i=2;i<=maxvalue;i++){
if (x%i == 0 ) return false; }
return true;
}
It seems like the cin >> x is inside the for loop. So, every iteration of the loop, you will try to read something from the stream. So, you need to enter some numbers before the i becomes prime.
EDIT: Apparently, 1000003 is prime, so you don't have to enter a lot of numbers.
why is this so even though the console output statement cout<<i; is before the input statement cin>>x;?
Because the break changes the order of execution, letting the control skip over cin >> x once the prime is found. You need to move the cin >> x out of the loop.
There are several things that you can do to optimize things quite a bit: rather than trying to divide by every number 1 through sqrt(N), you should divide out only the prime numbers that you have found so far. This would speed things up a lot. You can also drop the call of sqrt by using i*i < x as your exit condition.