#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).
Related
#include<bits/stdc++.h>
using namespace std;
void foo(float a[]){
for(int i=0;i<5;i++){
cout<<a[i]<<endl;
}
}
int main()
{
int a[]={1,2,3,4,5};
foo((float*)a);
return 0;
}
I'm trying to pass an int array to a function accepting float array as typecast. But, the results are flabbergasted as I'm getting negative exponential floating number. Please help me with explaining what is happening to the function.
So my doubt is,
I was trying out call by value,
While running the given code, Swapping happens when I write the function definition after int main()
But if I cut and paste the function definition above int main(), the swap does not take place. Why is that?
#include<iostream>
#include<string>
#include<vector>
#include<bitset>
#include<fstream>
using namespace std;
#define ADDU 1
#define SUBU 3
#define AND 4
#define OR 5
#define NOR 7
#define MemSize 65536
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main(){
// int a = 20;
// int *p = &a;
// cout<<"P: "<<p<<endl<<"*P gives: "<<*p<<endl<<"&p gives: "<<&p<<endl<<"&a : "<<&a;;
int x,y;
x = 10;
y = 20;
cout<<"Before Swapping: "<<"x: "<<x<<endl<<"y: "<<y<<endl;
swap(x,y);
cout<<"After Swapping: "<<"x: "<<x<<endl<<"y: "<<y<<endl;
}
Your swap function doesn't really swap anything, because it takes its arguments by value rather than by reference. All you're doing is manipulating variables that are local to that function.
When you don't introduce it until after main, it's not in scope when you call it, so std::swap is used instead. std::swap works correctly.
Although you didn't specifically say std::swap, you wrote using namespace std; which removes that requirement (good reason not to do it!!). And, although you did not #include <algorithm>, you cannot guarantee which standard headers may end up including other ones by virtue of how the implementation is constructed.
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;
}
I tried several time to find where is the problem, but I can not find any thing.So, could anyone help me to find the problem and why I can not see a result?
It might seem stupid question, but I new to programming world :)
This is my code :
#include <iostream>
using namespace std;
// There is the declraction of all functions
float max();
float min();
// This is the main program
int main ( int argc, char ** argv )
{
// Here you can find max
max(504.50,70.33);
// Here you can find min
min(55.77, 80.12);
return 0;
}
// This is the max function
int max(float a, float b){
float theMax;
if (a>b) {
theMax = a;
cout <<theMax ;
}
else{
theMax = b;
cout << b;
}
return theMax;
}
// This is the min function
int min( float c, float d){
float theMin;
if (c >d ) {
theMin =c;
cout << theMin;
}
else {
theMin =d;
cout << theMin;
}
return theMin;
}
You're calling std::max and std::min. That's because you wrote using namespace std, and did not declare your own min and max prior to using them. (You did declare two other min and max functions, but those take zero arguments, not two). So, when the compiler sees max(504.50,70.33); the only candidate is std::max.
You declare these overloads:
float max();
float min();
which are functions that take no arguments and return float.
You're calling
max(504.50,70.33);
and
min(55.77, 80.12);
which are functions that takes two doubles and may or may not return anything.
These match std::max and std::min, not the prototypes you declared.
You then define
int min( float c, float d){
which also doesn't match the prototypes you declared.
In other words, these functions are unknown in main, and the functions that are actually called are std::min and std::max.
Don't use using namespace std; - what you save in typing is lost in clarity and debugging.
You should also rename the functions - it's not a good idea to reuse standard library names.
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.