Discuss C++ Challenges - Advanced

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

baseball435

Newbie
Full Member
Jun 3, 2012
16
322
0
  • Hey everyone, so I have a few challenges for you and all of them are more for the advanced C++ programmer. Try not to use Google if you can. When you are done post your solution and which challenge it is for. Here are the rules:

    Rules:
    • No use of any functions
    • No use of the '%', '*', and '/' operators
    • No use of any classes

    The challenges below should be placed in their own method and you should try and make it as small as you can, 1 line being the best if possible. Here are the challenges:

    Challenges:
    1. Make a function that takes a number as a parameter. Return an integer that is the power of two of the inputted number. So for example if the parameter is 5, it will return 25.
    2. Make a function that takes a number as a parameter. Return an integer that is the number divided by two. So for example if the parameter is 10, it will return 5.
    3. Make a function that takes a number as a parameter. Return a boolean value where true will be returned if the parameter is even or false if the parameter is odd. So for example if the parameter is 3 it will return false because it is odd.

    HINT:
    Use Bitwise operators!!!

    Goodluck Everyone!
    ~Baseball435
 

BlackPitchPL

Coder
Full Member
Nobleman
May 24, 2012
166
783
1
You tell us to not use function but in quest u tell us to write function :p

C++:
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
  double number;
  cout << "Enter number to pow: ";
  cin >> number;
  cout << "pow(" << number << ")=" << pow(number,2) << endl;
 
  return( 0 );
}
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
solution for your first question. The code is in c#
I did it c# since the vc# ide was open when I read this post. So don't say "Ah! c#? I asked in c++". The point is your problem getting solved.

public int pow(int i) //c++ equivalent
{
int j;
p=0
for(j=0;j<i;j++)
{
p=p+i;
}
return p;
}
using System;
class MainCall
{
public static void Main()
{
int a,p=0;
a=Convert.ToInt32(Console.ReadLine());
for (int j = 0; j < a; j++) // This is the core stuff. This loop perform the power function use this loop within any function
{
p = p + a;
}
Console.WriteLine("The answer is {0}",p);
Console.ReadLine();
}
}
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
And about your third problem:

public bool eoo(int i) // c++ equivalent
{
int j;
for(j=0;j<=i;j=j+2)
{
if(j==i)
{
return true;
}
else
{
continue;
}
}
return false;
}

The code is in c#:
using System;
class MainCall
{
public static void Main()
{
int a,p=0;
a=Convert.ToInt32(Console.ReadLine());
FunctionEX fe = new FunctionEX();
Console.WriteLine(fe.eoo(a));
Console.ReadLine();
}
}

class FunctionEX
{
public bool eoo(int i)
{
for (int j = 0; j <= i; j = j + 2)
{
if (j == i)
{
return true;
}
else
{
continue;
}
}
return false;
}
}
 

Crazywink

Hacker
Meme Tier VIP
Dank Tier Donator
Jul 18, 2012
626
4,613
17
Solution for the second, C++. :)
C++:
#include <iostream>
using namespace std;
int main()
{
	unsigned int Value;
	cout << "Enter value to be divided: ";
	cin >> Value;
	Value = Value >> 1;
	cout << "\n" << "Your new value is: " << Value << "\n";
}

Crazywink
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
@Crazywink: is there any other method available other than bit operations?
Btw, Nice and a simple one!
 

Crazywink

Hacker
Meme Tier VIP
Dank Tier Donator
Jul 18, 2012
626
4,613
17
guru said:
@Crazywink: is there any other method available other than bit operations?
Btw, Nice and a simple one!
Thanks :)
You mean another method that doesn't use "/" or bit operation?
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
Ya! Just trying an out of box solution. He has gave an hint that to use bit wise operator. Apart from bit wise, is it possible to solve the 2nd problem without bit wise operation?
 

Crazywink

Hacker
Meme Tier VIP
Dank Tier Donator
Jul 18, 2012
626
4,613
17
guru said:
Ya! Just trying an out of box solution. He has gave an hint that to use bit wise operator. Apart from bit wise, is it possible to solve the 2nd problem without bit wise operation?
Hmmm.. That's a tough one.. Not being able to use "*", "/" and "%" and bit operators.. Pretty much leaves you with "+" and "-". XD
I'm going to keep thinking on this though..

Crazywink
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
@crazywink: I forgot about it. Try post the solution for the other two problems in bit wise operations :). It would be glad to have various implementation for a single problem.
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
I found the solution for 2nd problem. Dividing the number by 2 without using symbols mentioned!

C++:
public double div(double i)
{
int j;
double p=i;
for(j=1;j<=i;j++)
{
p=p-0.5; // this is equivalent to dividing by number 2
}
return p;
}
 

baseball435

Newbie
Full Member
Jun 3, 2012
16
322
0
Haha they are all very good! I ment to say in the thread no for loops or while loops but you worked with them well! Good job! I was looking for bitwise operation solutions but these are very good! If you guys want I can post the solution using bitwise operations in a spoiler in the main thread
 

guru

Coder
Full Member
Nobleman
Jun 8, 2012
115
673
1
Sure! Post it! And about using loops, this is a high level programming way who feels too lazy to perform a bit operations :)
 

BlackPitchPL

Coder
Full Member
Nobleman
May 24, 2012
166
783
1
So I'll make some more advance task for you now .

What to do:
Fully work AVL TREE with some nice show of tree ( natural representative of tree)
How the program should work :

1. Menu
2. Insert Node
3. Delete Node
4. Show empty tree
5 Show tree
6. Add to node random nodes ( user can tell program how many node's need to be add)
7. Exit :p

251px-Non-AVLtree.png
This is how AVL TREE LOOK LIKE and this is how it's should be show in program ( ofc without circle ).
 

xploiitz

Coder
Fleep Tier Donator
Trump Tier Donator
Nobleman
Jul 26, 2012
155
1,698
7
So I'll make some more advance task for you now .

What to do:
Fully work AVL TREE with some nice show of tree ( natural representative of tree)
How the program should work :

1. Menu
2. Insert Node
3. Delete Node
4. Show empty tree
5 Show tree
6. Add to node random nodes ( user can tell program how many node's need to be add)
7. Exit :p
This is how AVL TREE LOOK LIKE and this is how it's should be show in program ( ofc without circle ).
This is quite advanced :p If I have time I'll take a wack at it.... I just did this stuff over the summer in my Uni class.

Only thing that doesn't sound correct to me is, since this is a an AVL binary tree.... that is, self balancing, I dont think it makes much sense to implement #6 that is, add a node to random nodes? because that defeats the purpose of any tree! especially the AVL tree :p

anyway, I hope I have time... this is fun :D
 

BlackPitchPL

Coder
Full Member
Nobleman
May 24, 2012
166
783
1
yeah 6 is a bit wrong :p my bad :) i was mean that you add random number to node and tree should self balanced and this is not so advance but i recommend to make first of all the binary tree than just make it AVL
 

Departure

Newbie
Silenced
Full Member
Jun 24, 2012
21
272
1
BlackPitchPL instead of getting people to do your school work for you, why don't you first produce your solution as an example and then others will try and make there solution and present alternatives to you.
 

voidisalive

Jr.Coder
Full Member
Nobleman
May 27, 2012
67
458
3
I think this was a challange not school work but if it is o'well his lost.

Greets,

voidisalive
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods