FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Software development algorithms





Denvis
Question: You have a list of letters within an array (5 words) you want to get a word from the list and then display the number of characters within that word.

For example:

0 - David
1 - John
2 - Jessica
3 - Kevin
4 - Amy

And I chose 0, 0=David, the result would be:

5

How would I write that in pseudocode?

What I was thinking was to have a temporary array which stores each letter of a word as its own element. The number of indexes used within the temp array will be added up using some form of counter. I'm unsure how I am to split it. I can give each letter its own value but that will be inefficient. What if the array was made up of 2 billion words Confused

Any help will be much appreciated!
ocalhoun
It would be much easier to put the letters into one string variable.

php has a function that counts the characters in a string and returns the number, probably other programming languages do as well.

Which language are you working in?
Denvis
ocalhoun wrote:
It would be much easier to put the letters into one string variable.

php has a function that counts the characters in a string and returns the number, probably other programming languages do as well.

Which language are you working in?


Oh, it's not a language. PHP has its own function i think it was str_split or something similar. This is pseudocode, it's basically human language (not coding language). It's something you can do to prior to coding or converting one language to another.

Pseudocode is pretty much 'behind the scenes' of coding.

Like for example, if i wanted to locate an element in an array which is -ve.

$arr = array('3','4','-18','72');

In PHP you can probably use a simple function where the language automatically searches for the -ve whereas in an algorithm it would look like this:

BEGIN
counter=0
WHILE list(counter) => 0
add 1 to index
ENDWHILE
location = index
END

What that's saying is while the index of the array is greater than or equal to 0, add one to the index (the index is the counter) and it does that until the element in the array is less than 0 which in result will have the index of that negative number.

So in a desk check for the example I stated above it would look something like this,

counter element
0 3 (3 is greater than 0 so 1 is added to the counter)
1 4 (4 is greater than 0 so 1 is added to the counter)
2 -18 (-18 is not greater than 0 so nothing is added to the counter)
The loop ends and the location will equal the index of that element so, location = 2

2 in the array is where the -ve is.

That's a very simple algorithm in pseudocode.
SonLight
Most languages have a function comparable to C's strlen(), so generally the programmer would simply use that function. What it appears you are interested in is studying the nature of algorithms. Perhaps the best way to write pseudocode for your task, is:

First, assume the presence of an strlen() or equivalent function. Write high-level pseudocode assuming you have an array of strings, from which you select one and get its length with a simgle function statement.

Second, since you apparently want to study algorithms (and perhaps tradeoffs between different ways of doing things). write pseudocode for an strlen-like function.

What you seem to have discovered in your first post is that how data is _represented_ may be more important than the details of how it is processed. Most high-level languages "cheat" and give you variables which can contain a string as a value, and hide the details of how it is done. C does not have true string variables, so the structure of string data is always visible to the programmer. In C, the string "David" would be represented by a six-element array, { 'D', 'a', 'v', 'i', 'd', '\0' }. The last value, '\0' is a character whose binary value is zero.

An alternative way to represent strings is with a length field first, then the character array. For example, { 5, 'D', 'a', 'v', 'i', 'd' }. Of course the length may not be compatible with the representation of the letters and so cannot be considered an element within the same array. A tradeoff here is that you have to decide how big to make the length field. If you can figure out an efficient way to represent any string with lengths from zero to more than 32 bits (about 4 billion) while allowing for short strings to have only one or two bytes for the length, you might get the algorithm named after you.
Denvis
Thanks for your response,

Quote:
First, assume the presence of an strlen() or equivalent function. Write high-level pseudocode assuming you have an array of strings, from which you select one and get its length with a simgle function statement.


In pseudocode there is no simple function, in fact there are only loops which allows the creator to make a function. However if it's something as simple as getting an element from an array, the word 'get' could be used. Even if it's trying to delete something for example an element in an array. The word 'delete' isn't allowed as it doesn't describe the process of removing something. list(index)="" would probably be used to remove it

My first step could be getting an element from an array, but how could i randomly get an element? I would need a way to randomly generate a number between x and y. I don't know how to do that Confused

Once I get that element how would I structure my algorithm so that each character of that string is its own element? You said to determine the length of that string but how do i create a function that does that? I'm not allowed to use strlen() Confused

I was thinking something like this:

BEGIN
x=random (0 - 19)
GET from list(x)
word=list(x)

^
Might be able to get a random element in an array of strings (20 elements)

However splitting that element, I don't know how Confused
Peterssidan
Denvis wrote:
In pseudocode there is no simple function, in fact there are only loops which allows the creator to make a function. However if it's something as simple as getting an element from an array, the word 'get' could be used. Even if it's trying to delete something for example an element in an array. The word 'delete' isn't allowed as it doesn't describe the process of removing something. list(index)="" would probably be used to remove it

I have only seen pseudocode used on a higher level than normal code so that you can leave out small implementation details that is not relevant to the problem. Small details like strlen is just saying "the length of x".

Denvis wrote:
Once I get that element how would I structure my algorithm so that each character of that string is its own element? You said to determine the length of that string but how do i create a function that does that? I'm not allowed to use strlen() Confused

I guess you are doing this for school or something so you have a reason to do the way you do it. But honestly is what kind of strings you use really relevant to the problem? Here is pseudocode for the strlen function.
Code:
FUNCTION strlen(str)
  INPUTS:
    str, array of characters terminated by '\0'
  len = 0;
  WHILE (char at position len in str) != '\0'
    len++ (or "increment len by 1" if you prefer)
  RETURN len

By the way. Most OOP implementations of strings stores the length so you don't have to traverse the string every time you need its length.

Normally you don't want to go into these kind of details because pseudocode can be more general and do not need to specify how everything is done. If you say use this random function and specify a random function to be used. But why can't they use another random function? there might already be an already ready random function in the implementation language so it's silly to have to write one in most cases.

To you first problem I would write something like:

Code:
FUNCTION foo(strArr, n)
  INPUTS:
    strArr, the array of strings
    n, the index of the string we seek the length of
  str <- get element n in strArr
  len <- get length of str
  RETURN len
Related topics
Strange Fire fox problem
Earn U$D4500 - No Joke !
HELP me with Frontpage PLEASE
Programming
XBOX 360 or PS3
Software development
Mod Projects lots of info
What is this called "Bioinformatics"
What's the best free software for software development?
Software Design
Mobile Development Services
What is Software (Engineering) ?
Weblineindia Software Development & Web Designing Compan
Some interesting Software
Reply to topic    Frihost Forum Index -> Computers -> Computer Problems and Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.