Can anyone help me with this code

P

Peter Nilsson

Flash Gordon said:
... note that on 2's complement systems the bit pattern
with all bits set is allowed to be a trap representation.

Not unless there are padding bits.
I don't know of any systems that do this, but it is
allowed.

Are you really thinking of a sign bit of 1 and all value
bits zero? That is allowed to be a trap representation
in two's complement, under C99.
 
H

Harald van Dijk

No padding bits are required.

Padding bits are required.
In any complement system,
(-INT_MAX) can be equal to INT_MIN.

Yes, but in two's complement, the trap representation that could have been
-INT_MAX-1 isn't the bit pattern with all bits set. This is what Peter
Nilsson pointed out in the rest of his message, which you snipped. In
two's complement, the bit pattern with all bits set is -1, which must be
representable.
 
F

Flash Gordon

Peter Nilsson wrote, On 23/09/08 03:50:
Not unless there are padding bits.


Are you really thinking of a sign bit of 1 and all value
bits zero? That is allowed to be a trap representation
in two's complement, under C99.

Yes, I was thinking of the situation where INT_MIN == -INT_MAX, I just
wasn't thinking hard enough about it.
 
U

user923005

(e-mail address removed) said:
Instead of showing C#code, why not describe, in English, the problem you
are trying to solve, and show us your best shot at a C solution?
Once we know what you're trying to do (in English) and how you're trying to
do it (in C), we have a fighting chance of being able tohelpyou achieve
your objective.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Im trying to do the Partation (number theory) to print the results.

this is the closest got using C.

#include<stdio.h>
#include<string.h>

void partation(int n, int m, char str[]);
int main()
{
        int no;
        printf("Enter a positive integer: ");
        scanf("%d",&no);

        if(no < 0)
        {
                printf("%d is not a positive integer.\n",no);
                printf("Please enter a positive integer and retry...\n");
                return -1;
        }

        char result[10]="";
        partation(no,no,result);
        return 0;

}

void partation(int n, int m, char str[])
{
        int i;
        if(n<m)
                i=n;
        else
                i=m;

        if(n>0)
        {
                for(;i>0;i--)
                {
                        char s[2];
                        sprintf(s,"%i",i);
                        strcat(str,s);
                        partation(n-i,i,str);
                }
        }
        else
        {
                printf("%s\n",str);
                sprintf(str,"");
        }

}

In thiscodewhen i input the value 4, i get the result as
4
31
22
11
1111

but the result should be
4
31
22
211
1111

P.S.: Thnkx for ur replies...


/*
Modified from some Hungarian code I found.
*/
#include <stdio.h>
#include <assert.h>

#define MAX 100

int t[MAX];

void integer_partition(const int n, const int e)
{
assert((e - 1) >= 0);
assert(e < MAX);

if (n <= t[e - 1]) {
int s,
i;

t[e] = n;

printf("%i=", t[0]);
for (i = 1, s = 0; s + t < t[0]; s += t[i++]) {
printf("%i+", t);
assert(i + 1 < MAX);
}
printf("%i\n", t);
} else {
t[e] = t[e - 1] + 1;
}

while (--t[e] > 0) {
integer_partition(n - t[e], e + 1);
}
}


int main(int argc, char *argv[])
{
int n = 4;

if (argc > 1) {
n = atoi(argv[1]);
}
assert(n < MAX);
t[0] = n;
integer_partition(n, 1);
return 0;
}

/*
From:
"PROGRAMMING CHALLENGES
The Programming Contest Training Manual"
by Steven S. Skiena Miguel A. Revilla

Definition for how to compute them:
Integer Partitions - An integer partition of n is an unordered set of
positive integers which add up to n. For example, there are seven
partitions
of 5, namely, (5), (4, 1), (3, 2), (3, 1, 1), (2, 2, 1), (2, 1, 1, 1),
and
(1, 1, 1, 1, 1). The easiest way to count them is to define a function
f(n,
k) giving the number of integer partitions of n with largest part at
most k.
In any acceptable partition the largest part either does or does not
reach
with limit, so f(n, k) = f(n-k, k)+f(n, k-1). The basis cases are f(1,
1) =
1 and f(n, k) = 0 whenever k > n.
*/
 
D

David Thompson

James Kuyper wrote, On 19/09/08 19:33:

Also note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation. I don't know of any systems
that do this, but it is allowed.

YM 1sC, where as already said it can be negative zero or a trap rep.
In 2sC, all bits set must be negative one.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
B

Ben Bacarisse

David Thompson said:
YM 1sC, where as already said it can be negative zero or a trap rep.
In 2sC, all bits set must be negative one.

Yes, but there is still a small bit of information missing:

2sC systems /are/ permitted a trap representation without padding
bits: when the sign bit is one and the values bits are all zero, just
like sign+magnitude systems. This is probably what Flash Gordon is
(mis-)remembering.
 
F

Flash Gordon

Ben Bacarisse wrote, On 01/10/08 12:28:
My error was corrected ages ago and I acknowledged the correction and
clarified what I meant.
Yes, but there is still a small bit of information missing:

2sC systems /are/ permitted a trap representation without padding
bits: when the sign bit is one and the values bits are all zero, just
like sign+magnitude systems. This is probably what Flash Gordon is
(mis-)remembering.

Oh, I remembered it al right, I just failed to say what I intended to say.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top