Hi all,
1. In C++, how can we print "Hello World" say 100 times witout using any loops (for, while/do while, for_each or recursion)?
2. I need to make a Base class underivavle. Could somebody tell me why "virtual" keyword is necessary in following code snippet?

#include <vector>
#include <iostream> 

class Base;
class Helper{
    friend class Base;
    Helper(){}
    Helper(const Helper& LFQ){}
};

class Base : public virtual Helper{
public:
    Base(){}
};

class Der : public Base{};

int main(){
    Base b;
    Der d;
    return 0;
}

Thanks
Prem

1. Are you allowed to use recursion? If so it's easy.
2. Please use code tags when posting code.

1. Are you allowed to use recursion? If so it's easy.
2. Please use code tags when posting code.

Will be careful next time.
Yeah, no recursion allowed.

You can use goto and if condition.

#include <iostream>
#include <string>

using namespace std;

int main() {
	string helloworld = "Hello world!\nHello world!\n"; // 2 times helloworld
	helloworld.append(helloworld); // 4 times hello world
	helloworld.append(helloworld); // 8 times hello world
	helloworld.append(helloworld); // 16 times hello world
	helloworld.append(helloworld); // 32 times hello world
	string helloworld32 = helloworld; // saves 32 times hello world to reach 96 and not 128
	helloworld.append(helloworld); // 64 times hello world
	helloworld.append(helloworld32); // 96 times hello world
	helloworld.append("Hello world!\nHello world!\nHello world!\nHello world!\n"); // 100 times hello world
	cout << endl << helloworld << endl;
	
	return 0;
}

Absolutely ugly, I know, but "less looping" than the if-goto way.
What a sick assignment you got - I'll never understand this kind of don't-do-this-don't-do-that exercises...

I think its solution has to do with some c++ feature like constructor call....1-2 line solution.
also what about the 2nd question. What difference does it make by including "virtual" keyword?

You may have fun looking into this and this.

>1. In C++, how can we print "Hello World" say 100 times witout
>using any loops (for, while/do while, for_each or recursion)?
You can't. Sorry. Often questions like this (given out by clueless teachers) have a loophole in the wording where you can use recursion or an unconventional loop with conditional jumps. You can take it to the extreme and fork processes in an unconventional form of recursion, but ultimately any solution will use some form or loop or some form of recursion.

Of course, there's always the option of completely unrolling the loop. If the number of iterations is known at compile-time, you can do it the brain dead naive way (and all of its variants, which is almost never the correct answer to the given question):

#include <iostream>

int main()
{
  // Print "Hello, world!" 100 times
  std::cout<<"Hello, world!\n";
  std::cout<<"Hello, world!\n";
  // ...and so on 100 times
}

>2. I need to make a Base class underivavle.
Why? In my experience it's more trouble than it's worth, and the solution is tedious.

>Could somebody tell me why "virtual" keyword
>is necessary in following code snippet?
It all boils down the the requirement that the class of the object being created needs to call the constructor for the virtual base class. This idiom works because of that rule and because the constructor of the virtual base class is private (and thus, inaccessible). If you take away the virtual keyword, the rule goes away, and the Helper constructor isn't required to be called from the Der class.

1. No loops, no (run-time ;) ) recursion:

template <int n>
struct Hello: public Hello<n-1> {
    Hello() { std::cout << "Hello, world!\n"; }
};

template <> struct Hello<0> {};

int main()
{
    Hello<100> woodpecker;
    return 0;
}

Compile-time recursion is still recursion.

Compile-time recursion is still recursion.

It seems formally no recursion at all in this case. It's an ordinar inheritance. Hello<n> and Hello<n-1> are different classes ;)
The same case as a famous "stack variable" instead of automatic: no stacks in C++ except call stack in exception handling and stack container in STL.
It's a compiler problem how it processes an inheritance...

>It seems formally no recursion at all in this case.
That's debatable.

>It's an ordinar inheritance. Hello<n> and Hello<n-1> are different classes
I tried semantic nitpicking once with a so called "professor" concerning a problem of this nature. If he wouldn't let me solve it using process forking then I won't let you solve it with template metaprogramming. So there. ;)

I think there is another true problem with forking: it's not pure C++ solution ( +=Unix needed, or Windows with CreateProcess etc).

The more such professors the less young rivals we'll have ;)...

Add two integers without any operators and executable statements (and declarations) in C++...

I dont know whether this is a loop or non..Try this at ur own risk..

void CALL_ME CLSL::functB()
{
   std::cout<<"hello world"<<std::endl; //,..,
   functA();
}

void CALL_ME CLSL::functA()
{
   functB();
}

>I dont know whether this is a loop or non..
That's called indirect recursion.

I guess thread spawning would class as recursion too?

Embed Python code into your C++ and do the following.

print "Hello World\n"*100

:D

Chris

Would array initialization be legal?

class Foo {
Foo() { cout << "Hello, World\n"; }
};

int main()
{
    Foo myarray[100];
}

Yes, it's legal construct, but (in Narue's opinion;) ) it's run-time loop :(...

Then I'm all for the 'pay by the line of code' solution. Just paste 100 prints in main and call it good.

>but (in Narue's opinion ) it's run-time loop
If I were the teacher giving the assignment[1], I would probably accept it. Still not your compile-time recursion though, unless you can find more descriptions of the idiom than not that describe it as something other than recursion.

[1] For the record, I'd never give such a stupid assignment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.