Troubles with Assignment

I am currently frustrated with the current problems from my assignments and not sure how to do the following.

1
2
3
4
bool IsAnInt( float a ); // Returns true if a is an integer, else false.  i.e. -2.0 is true, -2.1 is false.
float Sin( float a ); // Use the following truncated Taylor series to approximate Sin(a): Sin(a) = a – a3/6 + a5/120 – a7/5040.  Sin(0)=0; Sin(PI/2)=1; Sin(PI/6)=0.5.
float Cos( float a ); // Find the Taylor series for Cosine, and use the four term truncated Taylor series to approximate Cos(a).  Cos(0)=1; Cos(PI/3)=0.5; Cos(PI/2)=0;
char toUpper( char a ); // If the passed character is a lower-case letter, returns the corresponding upper-case letter; else, returns the passed character unchanged. 


For the first one,

 
bool IsAnInt( float a ); // Returns true if a is an integer, else false.  i.e. -2.0 is true, -2.1 is false. 


I tried isdigit but I am not sure how to put it in the way I want it. Such as my completed ones that look like this.

1
2
3
4
5
6
7
8
9
10
int main()
{
 //Rounding 
  cout << endl << "int Round( float a ); // Rounds a to the nearest integer.\n";
  cout << "The interger 4.5 rounds to " << Round(4.5) << '\n'; //Outputs 5
}
int Round(float a)
{
	return int(a+0.5);
}
Anybody?
Firstly, you did not put much thought in the assignment or tried to do the work. Secondly, thanks for being honest and saying you have trouble with your assignment. bool IsAnInt( float a ); // Returns true if a is an integer, else false. i.e. -2.0 is true, -2.1 is false. you would need a if statement as it say return true if number is equal to integer and false if number is not equal to integer. So what do you think? how would you start of the code? Good Luck.
I did this, but didn't turn out well right.


1
2
3
4
5
6
7
8
9
10
11
12
bool IsAnInt( float a )
{
	if (isdigit(a))
	{
		return false;
	}
        else
        {
                return true;
        }

}


All it returns is 0 or 1. How do I make it display the message?
Last edited on
Here is my result
1
2
 TrueThe boolean of 2.5 is 0
Press any key to continue . . .


Here is my full code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
  cout << endl << "bool IsAnInt( float a ); // Returns true if a is an integer, else false.  i.e. -2.0 is true, -2.1 is false.\n";
  cout << "The boolean of 2.5 is " << IsAnInt(2.5) << endl;
  return 0;
}

bool IsAnInt( float a )
{
	if (isdigit(a))
	{
		cout << " False";
		return true;
	}
	else
	{
		cout << " True";
		return false;
	}
}

Last edited on
Anybody?
As its late my brain is on sleep mode. Is that how you think the assignment should be like? I cannot help cause of less information I need to know more on what you need to do on the first one or that how its need be?
So basically the question is asking

bool IsAnInt( float a ); // Returns true if a is an integer, else false. i.e. -2.0 is true, -2.1 is false.


Asking to use

1
2
3
bool IsAnInt( float a )
{
}

as a function to be called in my main to tell if a integer is a decimal or not, if it is a decimal, then it is false.

cout should be "The value 2.5 is a FALSE integer."
Anybody?
You need to make use of the modf function.

1
2
3
4
bool IsAnInt( float a )
{
      //make use of modf function in here.
}


the modf function can be found in header <cmath> - so #include <cmath>

here is information on the modf function
http://www.cplusplus.com/reference/clibrary/cmath/modf/
Not sure how it works, in this

1
2
3
4
5
6
  double param, fractpart, intpart;

  param = 3.14159265;
  fractpart = modf (param , &intpart);
  printf ("%lf = %lf + %lf \n", param, intpart, fractpart);
  return 0;


Can you explain what the '%' and '&' does?

To my question, I am using isdigit, doesn't that work as well?

I did
1
2
3
4
5
6
7
8
9
10
11
12
13
bool IsAnInt( float a )
{
	if (isdigit(a))
	{
		cout << " False";
		return true;
	}
	else
	{
		cout << " True";
		return false;
	}
}


basically checking if 'a' is a decimal or not? But it isn't working!
i assume that this test is all about checkingi f a float is a whole number ( by this we mean if it is something like 4.000 as opposed to something like 4.198) then
this should do it.
It is laid out is a nice easy to follow fashion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <cmath>

using namespace std;




bool IsAnInt( float a )
{

    float integralPart, decimalPart;

    decimalPart= modf(a, &integralPart);

    if (decimalPart)
    {
        return false;
    }
    else
    {
        return true;
    }

}

int main()
{

    if (IsAnInt(2.0f)) // call the function with some float value
    {
        cout << "Has no decimal part" << endl;
    }
    else
    {
        cout << "has decimal part" << endl;
    }

    return 0;
}
Topic archived. No new replies allowed.