Float variable calculation using int error - c++

I run the following code:
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x=4,y=2,z=11;
float p;
p=7+z/y-x*2;
cout<<p;
return 0;
}
The x, y, z is declared as integer. Why the p get an int while p is declared as float?
Thanks.

If a and b are integers then result of a/b will always be integer, not float. Dividing result is just truncated to integer value and float part is thrown away.
In expression 7+z/y-x*2 only integer variables are used, that is why result is also integer.
As an option you can cast one of the operands on right side to float, then whole expression will be threated as float:
7+z/(float)y-x*2

pls change your integer datatype to float so u got
#include <iostream>
#include <string>
using namespace std;
int main()
{
float x=4,y=2,z=11;
float p;
p=7+z/y-x*2;
cout<<p;
return 0;
}

Related

issue with float value printing in cpp

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int solve(int t){
float ans = static_cast<float>(320)/100;
cout<<fixed<<setprecision(2)<<ans;
}
int main(){
int t=320;
cout<<solve(t);
return 0;
}
output:3.204745728
how should I get output as 3.20
You are using cout twice, when you only want to output one thing. You are also outputting the return value of solve when you don't return anything from solve, this explains the extra digits that you see.
Either do the cout in solve or do it in main, don't do it in both places.
First way
#include <iostream>
using namespace std;
void solve(int t){
float ans = static_cast<float>(320)/100;
cout<<fixed<<setprecision(2)<<ans; // print the answer
}
int main(){
int t=320;
solve(t);
return 0;
}
Second way
#include <iostream>
using namespace std;
float solve(int t){
float ans = static_cast<float>(320)/100;
return ans; // return the answer
}
int main(){
int t=320;
cout<<fixed<<setprecision(2)<<solve(t);
return 0;
}
Another issue with your code is that you are passing the parameter 320 to solve, but you aren't using it there. You might want to change that
float ans = static_cast<float>(t)/100;
It's quite common for beginners to get confused between returning a value from a function, and printing a value in a function, but these are quite different things. The first code above prints the value in the function, the second code above returns the value from the function (and prints it in main).

C++ return std::pair<int *,int *>?

#include <iostream>
#include <string>
#include <utility>
using namespace std;
string num1="123456789123456789";
std::pair<int*,int*> cpy(){
int a[(num1.size()%9==0)? num1.size()/9 : num1.size()/9+1];
int b[(num1.size()%9==0)? num1.size()/9 : num1.size()/9+1];
return make_pair(a,b);
}
int main(void){
return 0;
}
-------------------------------------------------------
//if by this style, it can be compiled
std::pair<int*,int*> cpy(){
const int N=5;
int a[5];
int b[5];
return make_pair(a,b);
}
I am writing a program to calculate Big Number such as 19933231289234783278, So I need split the number using 1 000 000 000 system
Why can't return a pair by this way?
You can't return a pair this way, because you are passing wrong types. In this case, array doesn't decay to pointer.
If you change the last line in the function to this :
return make_pair(&a[0],&b[0]);
it will compile, but it will still not work, as you are returning pointers to arrays, which are destroyed, once the function cpy() ends.
By the way, variable lenghts array are not standard c++.
You are trying to return pointer to a temporary value. As result the pointers will be point to a garbage.
Also you trying to cast string with decimal notation to integer.
You should return the pair of integers, which will be obtained by parsing the string at the numbers.
You can see the link below.
http://interactivepython.org/runestone/static/pythonds/BasicDS/ConvertingDecimalNumberstoBinaryNumbers.html

C++ Function supposed to return Long, returning Integer like value instead

While working on a fairly large project, I happened to notice that one of my functions that is supposed to return a Long value is either returning an Integer. I reproduced the error in a very small environment thinking that it would make the problem clear to me, but I'm still not seeing the issue. The input is 1.123, and the return value is 1. If I input any Long, for example; 123.456, it will only return 123. What am I not seeing?
Source1.cpp
#ifndef HEADER_H
#define HEADER_H
using namespace std;
class testClass
{
private:
long m_testLong = 0.0;
public:
long getTestLong();
void setTestLong(long sn);
};
#endif
Header.h
#include "Source1.cpp"
#include <string.h>
void testClass::setTestLong(long sn)
{
m_testLong = sn;
}
long testClass::getTestLong()
{
return m_testLong;
}
Source.cpp
#include <iostream>
#include "Source1.cpp"
#include "Header.h"
using namespace std;
int main(void)
{
testClass *myClass = new testClass;
cout << "Setting test long value using setTestLong function -- 1.123" << endl;
myClass->setTestLong(1.123);
long tempTestLong = 0.0;
tempTestLong = myClass->getTestLong();
cout << tempTestLong << endl;
system("Pause");
return 0;
}
OK, so the answer was painfully simple. I hadn't worked with longs before, but I thought I knew what they were. I didn't.
So longs and integers both are whole numbers, and having the type listed as long made me assume an integer wouldn't work, and I tested the function with a double because of my misunderstanding. Thanks for the help!
The long and int types are integral types, they can only hold whole numbers like 7 or 42.
You should be using float or double as a type, preferably the latter for increased range and precision. That will allow you to hold real numbers such as 3.141592653589 or 2.718281828459.
Long is an integer. Assigning a floating point value to integer causes rounding.
You want double or float.

Cant understand the reason of error Illegal use of pointer?

#include <iostream>
#include <conio>
#include <math>
using namespace std;
void main()
{
double b[1000], mxrs[1000],mnrs[1000], ls[1000]; int t, i;
clrscr();
cin>>t;
for(i=0;i<t;i++)
cin>>b[i]>>ls[i];
for(i=0;i<t;i++)
{
mxrs[i]=sqrt(ls*ls+b*b);
mnrs[i]=sqrt(ls*ls-b*b); cout<<mnrs[i]<<' '<<mxrs[i]<<'\n';
}
getch();
}
I get 4 errors in this code...
main.cpp|16|Error E2087 : Illegal use of pointer in function main()|
main.cpp|16|Error E2087 : Illegal use of pointer in function main()|
main.cpp|17|Error E2087 : Illegal use of pointer in function main()|
main.cpp|17|Error E2087 : Illegal use of pointer in function main()|
||=== Build finished: 4 errors, 0 warnings ===|
Please help me in correcting the errors, it would be helpful even if you told me only reason of errors. Thanks in advance!
When you write double b[1000], you can access the array elements either by writing b[n] or *(b + n), taking care to keep n within the bounds of the array.
So *b and b[0] are equivalent. In other words, b is a pointer to the zeroth element of the array. Writing b * b is an attempt to multiply two pointers and that makes no sense. This is what the compiler is telling you.
To multiply elements of the array, use b[n] * b[m] or *(b + n) * *(b + m).
(And fix the return type of main: it should be int.)
ls,bs refers to the base address of the array. so they are addresses not values. I think this is what you intend to do.
#include <iostream>
#include <conio>
#include <math>
using namespace std;
void main()
{
double b[1000], mxrs[1000],mnrs[1000], ls[1000]; int t, i;
clrscr();
cin>>t;
for(i=0;i<t;i++)
cin>>b[i]>>ls[i];
for(i=0;i<t;i++)
{
mxrs[i]=sqrt(ls[i]*ls[i]+b[i]*b[i]);
mnrs[i]=sqrt(ls[i]*ls[i]-b[i]*b[i]); cout<<mnrs[i]<<' '<<mxrs[i]<<'\n';
}
getch();
}
let's take an simple example to understand the concept:
int a[5]={1,2,3,4,5};
cout<<*a; //prints the value at base address which is 1 as a refers to a[0]
cout<<a[0]; //prints 1
cout<<*(a+1); //prints 2 ... as a+1 refers to the address next to its base which is a[1] and which is 2
cout<<a[1];//prints 2
You put #include <math>, it should have been #include <math.h>. Same thing for <iostream> and <conio>. You forgot .h on all of them.

To compute Euler’s pentagonal number theorem via dynamic programming

Here is a link to the code and I have posted it below too.
#include<math.h>
void pentagon(int n)
{
int k,p[10],a[10],b[10];
if(n<0)
p[n]=0;
if(n==0)
p[n]=1;
for(k=1;k<n;k++)
{
a[k]=((3*pow(k,2))-k)/2;
b[k]=((3*pow(k,2))+k)/2;
}
for(k=1;k<n;k++)
{
p[n]=pow(-1,k-1)(pentagon(n-a[k])+pentagon(n-b[k]));
}
cout<<p[n];
}
int main()
{
pentagon(4);
return(0);
}
I am getting the following error :
In function 'void pentagon(int)':
Line 11: error: call of overloaded 'pow(int&, int)' is ambiguous
compilation terminated due to -Wfatal-errors
Replace 2 with 2.0 as the second argument to pow (line 11, 12).
See also: http://www.cplusplus.com/reference/clibrary/cmath/pow/
Collecting and correcting all the errors (and warnings) leads to the following code (codepad). I made some comments about what changed.
#include <math.h>
#include <iostream> // Some compilers will complain if missing
using namespace std; // Some compilers will complain if missing
// returns int instead of being void
int pentagon(int n)
{
int k,p[10],a[10],b[10];
// recursion end - we want to jump out here right away
if(n<0) return 0;
if(n==0) return 1;
for(k=1;k<n;k++)
{
a[k]=((3*(int)pow(k,2.0))-k)/2; // pow needs double as second argument
b[k]=((3*(int)pow(k,2.0))+k)/2; // and returns double
}
for(k=1;k<n;k++)
{
// pow casting and double as second argument, multiplication with pentagon
p[n]=(int)pow(-1,k-1.0)*(pentagon(n-a[k])+pentagon(n-b[k]));
}
cout<<p[n]<<endl; // cleaner output
return p[n]; // return the calculated value
}
int main()
{
pentagon(4);
return(0);
}
but I guess the underlying algorithm is still wrong, as the output is:
-1084535312
-1084535311
1074838088
0
3
4
0
Here are some errors or improvements I spotted:
Added iostream & using namespace std:
#include <cmath>
#include <iostream>
using namespace std;
Changed pow(k,2) to k*k:
a[k]=((3*(k*k))-k)/2;
b[k]=((3*(k * k))+k)/2;
Add multiplication symbol to p[n] assignment:
p[n] = pow(-1.0,k-1) * (pentagon(n-a[k]) + pentagon(n-b[k]));
The pentagon method needs to return a value in order to use it in the above statement.
You're missing the summation part of the equation.
See http://en.wikipedia.org/wiki/Pentagonal_number_theorem.
Your pentagon function only calculates one term at a time. There is no code that sums all of the terms.