Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subset

65 views
Skip to first unread message

mary...@gmail.com

unread,
Feb 7, 2014, 7:30:47 AM2/7/14
to
Hi.I write this program.but i should write it, too recursive.and Could you help me in this case.

#include <iostream>
using namespace std;
int pow(int a,int b)
{
int p=1;
for(int i=0;i<b;i++)
p=p*a;
return p;
}
int main()

{
int testcase=0;

int n;

long long *a;

a=new long long[200];

cin>>testcase;

int i=0;

int d,b,c;

while(i<testcase)
{
cin>>n;

for(int j=0,k=1;j<n;j++,k++)
{
a[j]=k;
}


for(int p=0;p<=pow(2,n)-1;p++)
{
cout<<'{';

d=p;
for(int m=0;m<n;m++)
{

c=pow(2,n-1);

b=c&d;

if(b!=0)

cout<<a[m]<<' ';

d=d<<1;
}

cout<<'}';

cout<<endl;
}

cout<<endl;

i++;
}
return 0;
}
Message has been deleted

Barry Schwarz

unread,
Feb 7, 2014, 2:04:40 PM2/7/14
to
It would make your code a lot easier to read if

You indented consistently.
You doubled space between lines only to separate sections of your
functions.
You used the space bar more frequently.

Why does the definition of testcase contain initialization?

Why is a defined as a pointer and not as an array?

In the 'm' for loop, why do you re-evaluate c each iteration when its
value never changes?

What is the output supposed to represent?

If you google for recursion in C, you should get several hits that
show how to convert an iterative solution to a recursive one. If you
don't find an example for pow, look at the one for factorial.
--
Remove del for email

woodb...@gmail.com

unread,
Feb 7, 2014, 7:06:00 PM2/7/14
to
On Friday, February 7, 2014 1:04:40 PM UTC-6, Barry Schwarz wrote:
> It would make your code a lot easier to read if
>
>
> You indented consistently.
> You doubled space between lines only to separate sections of your
> functions.
>
> You used the space bar more frequently.
>

I'm with you for the first two, but I've seen
some who I think are good programmers not use
many spaces within a line of code, and I write
it that way sometimes also:

http://webEbenezer.net/misc/direct.cc

>
> If you google for recursion in C,

Watch out for Google.

Duckduckgo cares about your privacy:

https://Duckduckgo.com


http://donttrack.us


Brian
Ebenezer Enterprises
http://webEbenezer.net

David Harmon

unread,
Feb 8, 2014, 5:33:26 AM2/8/14
to
On Fri, 07 Feb 2014 11:04:40 -0800 in comp.lang.c++, Barry Schwarz
<schw...@dqel.com> wrote,
>Why does the definition of testcase contain initialization?

Because you should never define an uninitialized variable without a
really good reason!

The given code looks like:
int testcase=0;
cin>>testcase;
while(i<testcase)

Now, there are many problems you might pick with that code, but
anything caused by initializing testcase to a default value in case
the cin input fails is not among them. Would you rather that the
subsequent loop go running off to some undetermined possibly huge
number just because testcase was never given a value?

Message has been deleted

David Brown

unread,
Feb 8, 2014, 7:05:23 AM2/8/14
to
You should never unnecessarily initialise a variable, because that stops
the compiler checking your code for you.

If you leave "int testcase;" uninitialised, and later use it without
first setting its value, then the compiler's warnings will tell you.
But if you've used "int testcase = 0;", then the compiler can't help you
spot that error.


Victor Bazarov

unread,
Feb 8, 2014, 8:25:24 AM2/8/14
to
Reliance on compiler warnings can only be done in conjunction with other
ways of ensuring correctness of the code. Code reviews and following
coding standards are other methods. However, in the end there is no
single action that would completely protect of making a mistake.
Besides, warnings are non-normative and therefore are not consistent
from compiler to compiler or even from one version of the same compiler
to another.

That said, a random value is worse than a known one. It can lead to
unpredictable behavior of the program. Repeatability, even if the
actions of the program are incorrect, helps tremendously in fixing the bugs.

V
--
I do not respond to top-posted replies, please don't ask
Message has been deleted

Öö Tiib

unread,
Feb 8, 2014, 10:37:24 AM2/8/14
to
On Saturday, 8 February 2014 15:43:03 UTC+2, Stefan Ram wrote:
> Victor Bazarov <v.ba...@comcast.invalid> writes:
> >That said, a random value is worse than a known one.
>
> n3290 does not call it »random«:
>
> »if no initialization is performed, an object with automatic
> or dynamic storage duration has indeterminate value.«

Victor's point still holds since random is just (more useful) subset
of indeterminate. So if random is worse than known then indeterminate
is even worse.
Message has been deleted

David Brown

unread,
Feb 8, 2014, 11:15:04 AM2/8/14
to
That's all true - compiler warnings are an aid to avoiding bugs, but
they certainly don't cover everything. However, appropriate use of
compiler warnings should be a normal part of your development
methodology. If you are using a compiler that does not have a
reasonable level of warnings, then it is a good idea to use an external
tool (a "lint" tool), or simply pass the same code though a compiler
that /is/ good at static error checking. Since gcc is a suitable
choice, and is free, there is no good excuse for not using it as a
minimum (llvm is an alternative).

The worst thing you can do is say that since your compiler is not
perfect at static error checking, you should ignore it!

> That said, a random value is worse than a known one. It can lead to
> unpredictable behavior of the program. Repeatability, even if the
> actions of the program are incorrect, helps tremendously in fixing the
> bugs.
>

In a case like this, the initial value is only ever used if there is a
bug in later code. Giving the variable an initial value hides the bug
from helpful tools, and it certainly does not remove the bug. It is a
far stretch to claim that it makes the bug less bad (by having a known
value rather than an indeterminate one) - the bug is there, and the
program will not work.


0 new messages