turning

can someone turn my code , to use pointers and functions ?

the more pointers and functions the better

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
42
43
44
45
#include <iostream> 
#include <cmath>
#include <climits>

using namespace std;

using INT = long double;
using Long = long long int;

bool isPerfectSquare(INT x)
{

	long double sr = sqrt(x);

	return ((sr - floor(sr)) == 0);
}

int main() {

	INT x;

	Long NUM, a = 0, b = LONG_MIN , c = 0;

	cin >> x;

	for (int i = 0; i < x; i++)
	{
		cin >> NUM;

		if (isPerfectSquare(NUM))
		{
			a = NUM;
		}
		else if (NUM > b)
		{
			b = NUM;
			c++;
		}
	}

	if(c > 0)
	cout << b;

	return 0;
}
So much pointers.
This is a terrible idea.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream> 
#include <cmath>
#include <climits>

using namespace std;

using INT = long double;
using Long = long long int;

bool valEqualsFloorOfVal(long double* pVal)
{
  return ((*pVal - floor(*pVal)) == 0);
}

auto getSquareRootOfNumber(Long* px)
{
  return sqrt(*px);
}

bool isPerfectSquare(Long* px)
{
        long double sr;
        long double* psr = &sr;
	*psr = getSquareRootOfNumber(px);

	return valEqualsFloorOfVal(psr);
}

int main() {

	INT x;
	INT* px = &x;

	Long NUM, a = 0, b = LONG_MIN , c = 0;
	Long* pNUM = &NUM;
	Long * pa = &a; 
	Long * pb = &b;
	Long * pc = &c;
	
	cin >> *px;

	int i = 0;
	for (int* pi = &i; *pi < *px; (*pi)++)
	{
		cin >> *pNUM;

		if (isPerfectSquare(pNUM))
		{
			*pa = *pNUM;
		}
		else if (*pNUM > *pb)
		{
			*pb = *pNUM;
			(*pc)++;
		}
	}

	if(*pc > 0)
	cout << *pb;

	return 0;
}

Last edited on
Another not-so-great idea: using INT = long double;
I can understand wanting shorter aliases for longer POD types, but how does this improve readability and understanding? A long double is not an integer as your alias would suggest.

INT (or LINT, or LLINT or even LL) might be IMO a decent alias for long long int. If I were to create an alias for a long double I might (MIGHT) do this: using LDOUB = long double;. Or using LD = long double;.

The key is readability not just for you. Making code easy to understand for others can be crucial.
hey yall

using pointers and more functions wasnt my idea they forced us to turn our code to pointers (and i agree its really stupid to use that many pointers but ty for turning my code )


and for furry guy : ty for ur tip about aliases , imma keep ur help in mind and use it in my future codes


thank you both : )
The temptation to give you MUCH more pointers and functions is almost unbearable. But I will resist, I will...
Go on. Add function pointers :)
I'm itchin' to plop in a lot of smart pointers, with a soupçon of std::function liberally strewn about.

M'ok, that particular bit of evilness passed. ;)
Last edited on
Topic archived. No new replies allowed.