Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

what is pseudo-code

Hi guys
Could any one tell me what does that code do please? in pseudo-code
#include <fstream.h>
#include <iostream.h>

bool differsByOneDigit ( int , int );
void outputResults ( ofstream & , int * , int , bool );

void main()
{

//CHANGE THESE PATHS TO YOUR OWN !!!!!!!!!!!! ***********************
ifstream input ( "c:\\programs\\data.txt" );
ofstream output ( "c:\\programs\\out.txt" );
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

if ( input.fail() || output.fail() )
{
cout <<"input or output file did not open!!! " << endl;
return;
}

int first , next ; //32bit int == 2147483647
int sequence [ 11 ];//sequence read
int seqLength = 0; //sequence length
int j;
int temp;
bool match; //matching sequence

input >> temp;
while ( ! input.eof() )
{

//read next sequence
while ( temp != -1 )
{
sequence [ seqLength ++ ] = temp;
input >> temp;
}
sequence [ seqLength ] = -1;
j = 0;
first = sequence [ j ];
next = sequence [ j + 1 ];
j += 2;
match = true;
while ( match && next != -1)
{

match = differsByOneDigit( first , next );
first = next;
next = sequence [ j ];
j++;

}
outputResults ( output , sequence , seqLength , match );
input >> temp;
seqLength = 0;
}
output.close();
input.close();

}
bool differsByOneDigit ( int first , int next )
{

int differentDigits = 0;

while ( first != 0 && next != 0 && differentDigits <= 1 )
{
if ( first % 10 != next % 10 ) //count different digits
differentDigits ++;
first /= 10;
next /= 10;

}
if ( differentDigits > 1 || first || next )
return false;
else
return true;

}
void outputResults ( ofstream & output , int * sequence , int length , bool isChainedSequence )
{

int j = 0;

output << "The sequence : ";

while ( j < length )
{
output << sequence [ j ++ ] << " " ;
if ( j % 7 == 0 )
output << endl;
}
if ( isChainedSequence )
output << endl << "Is a chained sequence " << endl;

else
output << endl << "is a not a chained sequence " << endl;;

output << endl <<"****************************************************" << endl;

}
[2542 byte] By [The_Master] at [2007-11-11 9:59:40]
# 1 Re: what is pseudo-code
It's the solution for your last opened thread, what do u want to know exactly, I don't know what's pseudo-code ?? do u mean an explaination for this code generally ?
Amahdy at 2007-11-11 20:59:34 >
# 2 Re: what is pseudo-code
pseudo code is a way to outline code without worrying about syntax. something like

for(index = 1...20)
{
find the smallest from index..20
swap smallest with index location
}

is high level pseudo code for a sort.
You can make it as much like code as you want or as general as you want, it depends on why you are making it in the first place.
jonnin at 2007-11-11 21:00:36 >
# 3 Re: what is pseudo-code
Ah thanks jonnin, here if it's finite steps we call it algorithm and in general programme logic . it's first time for me to know this name !
Amahdy at 2007-11-11 21:01:46 >
# 4 Re: what is pseudo-code
Well I'll try to explain and not using any rule in writing ,
In the main :

initialise data.txt for read and out.txt for write
if there is error opening or writing into files
>set error flag error in files

read first block from data.txt in tmp

while not end of data.txt
>while tmp not equal -1
>>sequence[position_index] = tmp
>>position_index = position_index + 1
>>read next block
>end while

>while "sequence current item and sequence next item are chained" AND "not end of the sequence"
>>read next sequence item and compare it with the last one
>end while

>if reach end of the sequence [all two consecutive items are chained]
>>print the sequence items and " is chained seq."
>else
>>print the sequence items and " is not chained."

>read next block
end while

#cheking if the sequence is chained occure in the differsByOneDigit () function which check if they differ by only one digit and return TRUE or other wise return FALSE .

#pinting results occure in the outputResults() function which check if all the sequence items are chained or not .

Does this make the code easier to understand ?
Amahdy at 2007-11-11 21:02:45 >