# getting logic of program

Discussion in 'C Programming' started by leenanemade@gmail.com, Aug 29, 2007.

1. ### Guest

Hello
I want logic or algorithm for following c function.
void hmethod( float **array, float **zeta, float **attrib, int
*linext,
char *alphabet, int obj, int att )
{
//H start computations ---------------------------------------------

int b = 0,
i = 0,
j = 0,
k = 0,
q = 0,
r = 0;
float ** hvalue,
count = 0.0,
total = 0.0,
rank = 0.0,
meanvalue = 0.0;

hvalue = ( float ** )malloc( sizeof( float * ) );
for( i = 0; i < obj; i++ )
{
hvalue[ i ] = ( float * )malloc( sizeof( float ) * obj );
}
//Here we compare the adjacent elements
//First store the elemnts for one attirbute from 'array' in the first
row of an array 'attrib'

while( q < att )
{
fflush( stdin );
printf( "\nRow elements: q=%d,r=%d\n", q, r );

for( i = 0; i < obj; i++ )
{
attrib[ r + 0 ][ i ] = array[ i ][ q ];
attrib[ r + 1 ][ i ] = array[ i ][ q ];
}

//Display the array

for( i = 0; i < obj; i++ )
{
printf( "%f ", attrib[ r + 1 ][ i ] );
}

sortbubble( attrib, obj, r );

//rank the elements

count = 0;
total = 0;
rank = 0;

for( i = 1; i < obj + 1; i++ )
{
if( attrib[ r + 1 ][ i ] == attrib[ r + 1 ][ i - 1 ] )
{
count++;
continue;
}
else
{
if( count == 0 )
{
attrib[ r + 2 ][ i - 1 ] = ( float )i;
}
else
{
for( k = i; k >= i - count; k-- )
{
total = ( float )( k + total );
}
rank = ( float )( ( total ) / ( count + 1 ) );

for( k = i - 1;
k >= i - 1 - count; k-- )
{
attrib[ r + 2 ][ k ] = rank;
}
}
if( i == ( obj - 1 ) )
{
attrib[ r + 2 ][ i ] = ( float )( i + 1 );
}
count = 0;
total = 0;
rank = 0;
}
}

/*
//Display the ranks..

printf("\nRanked elements of sorted array: \n");

for(i=0;i<obj;i++)
{
printf("%f ",attrib[r+2]);
}
*/

//Search for the appropriate object in the original matirx and
assign them the ranks

for( i = 0; i < obj; i++ )
{
for( j = 0; j < obj; j++ )
{
if( attrib[ r + 0 ][ i ] == attrib[ r + 1 ][ j ] )
{
attrib[ r + 3 ][ i ] = attrib[ r + 2 ][ j ];
break;
}
else
{
continue;
}
}
}

/*
//Display the ranks arranged in order..

printf("\nThe ranks arranged in order:\n");

for(i=0;i<obj;i++)
{
printf("%f ",attrib[r+3]);
}
*/

q++;
r = r + 4;
}

//Compute the H matrix

for( i = 0; i < obj; i++ )
{
j = 0;
meanvalue = 0;
while( j < ( att * 4 ) )
{
meanvalue = meanvalue + attrib[ j + 3 ][ i ];
j = j + 4;
}
hvalue[ 0 ][ i ] = ( float )meanvalue / att;
hvalue[ 1 ][ i ] = ( float )meanvalue / att;
}

/*
//Display the mean values

printf("\nThe mean values:\n");

for(i=0;i<obj;i++)
{
printf("%f ",hvalue[0]);
}
*/

//Generate the alphabets for the objects

for( i = 0; i < obj; i++ )
{
if( i >= 26 )
{
alphabet[ i ] = ( char )b + 65;
b++;
}
else
{
alphabet[ i ] = ( char )i + 97;
}
}

//Display H matrix in proper format

printf( "\n\nRank matrix:\n\n" );
printf( "Obj " );
for( i = 0; i < att; i++ )
{
printf( "R(q(%d)) \t", i + 1 );
}
printf( "H" );
printf( "\n\n" );
for( i = 0; i < obj; i++ )
{
j = 0;
meanvalue = 0;
printf( "%c ", alphabet[ i ] );
while( j < ( att * 4 ) )
{
printf( "%f\t", attrib[ j + 3 ][ i ] );
j = j + 4;
}
printf( "%f\n\n", hvalue[ 0 ][ i ] );
}

// Sort the H matirx values

sortbubble( hvalue, obj, 0 );

for( i = 0; i < obj; i++ )
{
linext[ i ] = 0;
}

//Display of the objects in order

printf( "\n\nThe order induced by H:\n\n" );

for( i = 0; i < obj; i++ )
{
count = 0;
for( j = 0; j < obj; j++ )
{
if( hvalue[ 1 ][ i ] == hvalue[ 0 ][ j ] )
{
if( count >= 1 )
{
printf( " =" );
i++;
}
else
{
printf( " <" );
}
printf( " %c", alphabet[ j ] );
count++;
linext[ i ] = j + 1;
}
else
continue;
}
}

printf( "\n\nThe linear extension for H start:\n\n" );
for( i = 0; i < obj; i++ )
{
printf( "%d\n", linext[ i ] );
}
}

if anyone can understands the logic then pl reply me.
at
pl send logic as early as possible

thanking you,

, Aug 29, 2007

2. ### karthikbalaguruGuest

wrote:
> Hello
> I want logic or algorithm for following c function.
> void hmethod( float **array, float **zeta, float **attrib, int
> *linext,
> char *alphabet, int obj, int att )
> {
> //H start computations ---------------------------------------------
>
> int b = 0,
> i = 0,
> j = 0,
> k = 0,
> q = 0,
> r = 0;
> float ** hvalue,
> count = 0.0,
> total = 0.0,
> rank = 0.0,
> meanvalue = 0.0;
>
> hvalue = ( float ** )malloc( sizeof( float * ) );
> for( i = 0; i < obj; i++ )
> {
> hvalue[ i ] = ( float * )malloc( sizeof( float ) * obj );
> }
> //Here we compare the adjacent elements
> //First store the elemnts for one attirbute from 'array' in the first
> row of an array 'attrib'
>
> while( q < att )
> {
> fflush( stdin );
> printf( "\nRow elements: q=%d,r=%d\n", q, r );
>
> for( i = 0; i < obj; i++ )
> {
> attrib[ r + 0 ][ i ] = array[ i ][ q ];
> attrib[ r + 1 ][ i ] = array[ i ][ q ];
> }
>
>
> //Display the array
>
> for( i = 0; i < obj; i++ )
> {
> printf( "%f ", attrib[ r + 1 ][ i ] );
> }
>
> sortbubble( attrib, obj, r );
>
> //rank the elements
>
> count = 0;
> total = 0;
> rank = 0;
>
> for( i = 1; i < obj + 1; i++ )
> {
> if( attrib[ r + 1 ][ i ] == attrib[ r + 1 ][ i - 1 ] )
> {
> count++;
> continue;
> }
> else
> {
> if( count == 0 )
> {
> attrib[ r + 2 ][ i - 1 ] = ( float )i;
> }
> else
> {
> for( k = i; k >= i - count; k-- )
> {
> total = ( float )( k + total );
> }
> rank = ( float )( ( total ) / ( count + 1 ) );
>
> for( k = i - 1;
> k >= i - 1 - count; k-- )
> {
> attrib[ r + 2 ][ k ] = rank;
> }
> }
> if( i == ( obj - 1 ) )
> {
> attrib[ r + 2 ][ i ] = ( float )( i + 1 );
> }
> count = 0;
> total = 0;
> rank = 0;
> }
> }
>
> /*
> //Display the ranks..
>
> printf("\nRanked elements of sorted array: \n");
>
> for(i=0;i<obj;i++)
> {
> printf("%f ",attrib[r+2]);
> }
> */
>
> //Search for the appropriate object in the original matirx and
> assign them the ranks
>
> for( i = 0; i < obj; i++ )
> {
> for( j = 0; j < obj; j++ )
> {
> if( attrib[ r + 0 ][ i ] == attrib[ r + 1 ][ j ] )
> {
> attrib[ r + 3 ][ i ] = attrib[ r + 2 ][ j ];
> break;
> }
> else
> {
> continue;
> }
> }
> }
>
> /*
> //Display the ranks arranged in order..
>
> printf("\nThe ranks arranged in order:\n");
>
> for(i=0;i<obj;i++)
> {
> printf("%f ",attrib[r+3]);
> }
> */
>
> q++;
> r = r + 4;
> }
>
> //Compute the H matrix
>
> for( i = 0; i < obj; i++ )
> {
> j = 0;
> meanvalue = 0;
> while( j < ( att * 4 ) )
> {
> meanvalue = meanvalue + attrib[ j + 3 ][ i ];
> j = j + 4;
> }
> hvalue[ 0 ][ i ] = ( float )meanvalue / att;
> hvalue[ 1 ][ i ] = ( float )meanvalue / att;
> }
>
> /*
> //Display the mean values
>
> printf("\nThe mean values:\n");
>
> for(i=0;i<obj;i++)
> {
> printf("%f ",hvalue[0]);
> }
> */
>
>
> //Generate the alphabets for the objects
>
> for( i = 0; i < obj; i++ )
> {
> if( i >= 26 )
> {
> alphabet[ i ] = ( char )b + 65;
> b++;
> }
> else
> {
> alphabet[ i ] = ( char )i + 97;
> }
> }
>
> //Display H matrix in proper format
>
> printf( "\n\nRank matrix:\n\n" );
> printf( "Obj " );
> for( i = 0; i < att; i++ )
> {
> printf( "R(q(%d)) \t", i + 1 );
> }
> printf( "H" );
> printf( "\n\n" );
> for( i = 0; i < obj; i++ )
> {
> j = 0;
> meanvalue = 0;
> printf( "%c ", alphabet[ i ] );
> while( j < ( att * 4 ) )
> {
> printf( "%f\t", attrib[ j + 3 ][ i ] );
> j = j + 4;
> }
> printf( "%f\n\n", hvalue[ 0 ][ i ] );
> }
>
> // Sort the H matirx values
>
> sortbubble( hvalue, obj, 0 );
>
>
> for( i = 0; i < obj; i++ )
> {
> linext[ i ] = 0;
> }
>
> //Display of the objects in order
>
> printf( "\n\nThe order induced by H:\n\n" );
>
> for( i = 0; i < obj; i++ )
> {
> count = 0;
> for( j = 0; j < obj; j++ )
> {
> if( hvalue[ 1 ][ i ] == hvalue[ 0 ][ j ] )
> {
> if( count >= 1 )
> {
> printf( " =" );
> i++;
> }
> else
> {
> printf( " <" );
> }
> printf( " %c", alphabet[ j ] );
> count++;
> linext[ i ] = j + 1;
> }
> else
> continue;
> }
> }
>
> printf( "\n\nThe linear extension for H start:\n\n" );
> for( i = 0; i < obj; i++ )
> {
> printf( "%d\n", linext[ i ] );
> }
> }
>
> if anyone can understands the logic then pl reply me.
> at
> pl send logic as early as possible

The simplest logic to understand the logic of some unknown logics is
to printf's at different instances in the source code.

Karthik Balaguru

karthikbalaguru, Aug 29, 2007

3. ### Nick KeighleyGuest

On 29 Aug, 06:08, wrote:
> Hello
> I want logic or algorithm for following c function.
> void hmethod( float **array, float **zeta, float **attrib, int
> *linext,
> char *alphabet, int obj, int att )
> {
> //H start computations ---------------------------------------------
>
> int b = 0,
> i = 0,
> j = 0,
> k = 0,
> q = 0,
> r = 0;
> float ** hvalue,
> count = 0.0,
> total = 0.0,
> rank = 0.0,
> meanvalue = 0.0;
>
> hvalue = ( float ** )malloc( sizeof( float * ) );
> for( i = 0; i < obj; i++ )
> {
> hvalue[ i ] = ( float * )malloc( sizeof( float ) * obj );
> }
> //Here we compare the adjacent elements
> //First store the elemnts for one attirbute from 'array' in the first
> row of an array 'attrib'
>
> while( q < att )
> {
> fflush( stdin );
> printf( "\nRow elements: q=%d,r=%d\n", q, r );
>
> for( i = 0; i < obj; i++ )
> {
> attrib[ r + 0 ][ i ] = array[ i ][ q ];
> attrib[ r + 1 ][ i ] = array[ i ][ q ];
> }
>
> //Display the array
>
> for( i = 0; i < obj; i++ )
> {
> printf( "%f ", attrib[ r + 1 ][ i ] );
> }
>
> sortbubble( attrib, obj, r );
>
> //rank the elements
>
> count = 0;
> total = 0;
> rank = 0;
>
> for( i = 1; i < obj + 1; i++ )
> {
> if( attrib[ r + 1 ][ i ] == attrib[ r + 1 ][ i - 1 ] )
> {
> count++;
> continue;
> }
> else
> {
> if( count == 0 )
> {
> attrib[ r + 2 ][ i - 1 ] = ( float )i;
> }
> else
> {
> for( k = i; k >= i - count; k-- )
> {
> total = ( float )( k + total );
> }
> rank = ( float )( ( total ) / ( count + 1 ) );
>
> for( k = i - 1;
> k >= i - 1 - count; k-- )
> {
> attrib[ r + 2 ][ k ] = rank;
> }
> }
> if( i == ( obj - 1 ) )
> {
> attrib[ r + 2 ][ i ] = ( float )( i + 1 );
> }
> count = 0;
> total = 0;
> rank = 0;
> }
> }
>
> /*
> //Display the ranks..
>
> printf("\nRanked elements of sorted array: \n");
>
> for(i=0;i<obj;i++)
> {
> printf("%f ",attrib[r+2]);
> }
> */
>
> //Search for the appropriate object in the original matirx and
> assign them the ranks
>
> for( i = 0; i < obj; i++ )
> {
> for( j = 0; j < obj; j++ )
> {
> if( attrib[ r + 0 ][ i ] == attrib[ r + 1 ][ j ] )
> {
> attrib[ r + 3 ][ i ] = attrib[ r + 2 ][ j ];
> break;
> }
> else
> {
> continue;
> }
> }
> }
>
> /*
> //Display the ranks arranged in order..
>
> printf("\nThe ranks arranged in order:\n");
>
> for(i=0;i<obj;i++)
> {
> printf("%f ",attrib[r+3]);
> }
> */
>
> q++;
> r = r + 4;
> }
>
> //Compute the H matrix
>
> for( i = 0; i < obj; i++ )
> {
> j = 0;
> meanvalue = 0;
> while( j < ( att * 4 ) )
> {
> meanvalue = meanvalue + attrib[ j + 3 ][ i ];
> j = j + 4;
> }
> hvalue[ 0 ][ i ] = ( float )meanvalue / att;
> hvalue[ 1 ][ i ] = ( float )meanvalue / att;
> }
>
> /*
> //Display the mean values
>
> printf("\nThe mean values:\n");
>
> for(i=0;i<obj;i++)
> {
> printf("%f ",hvalue[0]);
> }
> */
>
> //Generate the alphabets for the objects
>
> for( i = 0; i < obj; i++ )
> {
> if( i >= 26 )
> {
> alphabet[ i ] = ( char )b + 65;
> b++;
> }
> else
> {
> alphabet[ i ] = ( char )i + 97;
> }
> }
>
> //Display H matrix in proper format
>
> printf( "\n\nRank matrix:\n\n" );
> printf( "Obj " );
> for( i = 0; i < att; i++ )
> {
> printf( "R(q(%d)) \t", i + 1 );
> }
> printf( "H" );
> printf( "\n\n" );
> for( i = 0; i < obj; i++ )
> {
> j = 0;
> meanvalue = 0;
> printf( "%c ", alphabet[ i ] );
> while( j < ( att * 4 ) )
> {
> printf( "%f\t", attrib[ j + 3 ][ i ] );
> j = j + 4;
> }
> printf( "%f\n\n", hvalue[ 0 ][ i ] );
> }
>
> // Sort the H matirx values
>
> sortbubble( hvalue, obj, 0 );
>
> for( i = 0; i < obj; i++ )
> {
> linext[ i ] = 0;
> }
>
> //Display of the objects in order
>
> printf( "\n\nThe order induced by H:\n\n" );
>
> for( i = 0; i < obj; i++ )
> {
> count = 0;
> for( j = 0; j < obj; j++ )
> {
> if( hvalue[ 1 ][ i ] == hvalue[ 0 ][ j ] )
> {
> if( count >= 1 )
> {
> printf( " =" );
> i++;
> }
> else
> {
> printf( " <" );
> }
> printf( " %c", alphabet[ j ] );
> count++;
> linext[ i ] = j + 1;
> }
> else
> continue;
> }
> }
>
> printf( "\n\nThe linear extension for H start:\n\n" );
> for( i = 0; i < obj; i++ )
> {
> printf( "%d\n", linext[ i ] );
> }
>
> }
>
> if anyone can understands the logic then pl reply me.
> at
> pl send logic as early as possible
>
> thanking you,

Nick Keighley, Aug 29, 2007
4. ### Nick KeighleyGuest

On 29 Aug, 06:08, wrote:

> I want logic or algorithm for following c function.

??

isn't C code a perfectly succinct way of expressing an
algorithm? I suppose you could draw a flow diagram (<spit>)
or something. Or annotate it with pre and post conditions.

> hvalue = ( float ** )malloc( sizeof( float * ) );

don't cast malloc() it may hide a bug and isn't necessary.
Why do you want an array of ptrs-to-float

> fflush( stdin );

you can't do this. It exhibits Undefined Behaviour. The standard
only allows you to flush an *output* stream. Perhaps you want
to get characters until you get a '\n'.

<snip>

> if anyone can understands the logic then pl reply me.
> at
> pl send logic as early as possible

riight...

--
Nick Keighley

You are in a clearing. You can see a spire in the distance.
You can also see a copy of "C Unleashed".
: INV
You have;
a DeathStation 900 laptop,
a voucher for a free pizza,
and a torch.
: TAKE BOOK
You can't. It's too heavy.
Bill Godfrey (clc)

Nick Keighley, Aug 29, 2007
5. ### Barry SchwarzGuest

On Tue, 28 Aug 2007 22:08:40 -0700, wrote:

>Hello
>I want logic or algorithm for following c function.

snip

>
>if anyone can understands the logic then pl reply me.
>at
>pl send logic as early as possible

Since the code invokes undefined behavior in multiple ways, this is
only a guess. I think the code calculates the last digit in the
hexadecimal representation of pi (we know the decimal representation
has no last digit), finds four integers that satisfy Fermat's last
theorem, constructs a map that requires five colors, and prints a text
on net etiquette that you should read carefully.

Remove del for email

Barry Schwarz, Aug 31, 2007