|
Aiko Aiko


Registered: 05/13/05
Posts: 6,395
Loc: Lazy River Road
Last seen: 3 hours, 37 minutes
|
Programing 101 question
#5439283 - 03/24/06 06:55 PM (17 years, 10 months ago) |
|
|
I'm trying to figure out arrays. I guess my main question is on declaring and initializing. would this be legal:
num teamNum [1] = "Goal Getters" num teamNum [2] = "The Force"
I'm trying to associate the name of a team and their respective team number. or would you declare each individual team number as:
num count[1] num count[2] char "Goal Getters" char "The Force"
then later use count[teamNum] as the actual array. If this makes any sense to anyone, or if anyone has any suggestions, or if you think I have no idea as to what the hell I'm doing (which I would take into consideration too) I would greatly appreciate it. Thanx, Gobb's.
-------------------- Easily test the dosage of your tabs at home! qtests.org Man says, "God, show me and I will believe." God says, "Believe and I will show you."
Edited by Aiko Aiko (03/24/06 06:57 PM)
|
Anno
Experimenter



Registered: 06/17/99
Posts: 24,166
Loc: my room
Last seen: 20 days, 18 hours
|
Re: Programing 101 question [Re: Aiko Aiko]
#5439364 - 03/24/06 07:34 PM (17 years, 10 months ago) |
|
|
Which language?
|
Aiko Aiko


Registered: 05/13/05
Posts: 6,395
Loc: Lazy River Road
Last seen: 3 hours, 37 minutes
|
Re: Programing 101 question [Re: Anno]
#5439390 - 03/24/06 07:51 PM (17 years, 10 months ago) |
|
|
It's not language specific, so the syntax doesnt have to be exact. The class is for understanding the logic and design of programs. For the problem I only have to just write pseudocode and a flowchart. I know my instructor is a cobol expert if that helps any.
-------------------- Easily test the dosage of your tabs at home! qtests.org Man says, "God, show me and I will believe." God says, "Believe and I will show you."
|
ChuangTzu
starvingphysicist



Registered: 09/04/02
Posts: 3,060
Last seen: 10 years, 3 months
|
Re: Programing 101 question [Re: Aiko Aiko]
#5440170 - 03/25/06 01:19 AM (17 years, 10 months ago) |
|
|
Code:
string teamNumber = []" #if the language allows dynamic arrays, or string teamNumber[n] #where n is >= number of teams
teamNumber[1] = "Goal Getters" teamNumber[2] = "The Force" . . .
This should be good enough for pseudo-code. Ideally you wouldn't code the team names into the program but read them from a file, but I guess that's not part of the assignment. Your first example won't work because team names aren't numbers. The second won't work because team names are strings not chars (containing variable numbers of chars).
|
Seuss
Error: divide byzero


Registered: 04/27/01
Posts: 23,480
Loc: Caribbean
Last seen: 2 months, 20 days
|
Re: Programing 101 question [Re: Aiko Aiko]
#5440247 - 03/25/06 02:56 AM (17 years, 10 months ago) |
|
|
> I know my instructor is a cobol expert if that helps any.
Bleck... I think we found your problem. Kind of like me teaching a class on modern automobile engine design when I'm an expert on the model-T engine.
All right, enough critique of your prof, lets answer your questions...
> guess my main question is on declaring and initializing.
Anno was right on when asking what language. The problem is that declaration and initialization of arrays is language specific. Flowcharts, which no sane comp sci actually uses, might include a box with "initialize array" as it's contents, but nothing more. Even this would be a level of detail not often given.
With pseudocode, the same thing applies. You might have a line indicating where the array is defined and initialized, to help identify scope, but you wouldn't bother with the exact semantics... because you don't know what the exact semantics are going to be until you pick a language.
> I'm trying to associate the name of a team and their respective team number.
This is a type of array known as an associate array. Higher level languages, such as PHP implement associate arrays natively. Lower level languages, such as C and C++ do not. Do not confuse a non-associate array with an associate array. Although the elements (values) in the array are accessed in the same manner, the internal implementation of the two are completely different.
With a regular non-associate array, you declare memory for a sequence of values. The memory declared is continuous. The array is indexed allowing any value in the array to be looked up quickly. For example, in C (or C++):
Code:
int myArray[10];
the above line will declare an array of signed integers. The compiler will allocate enough memory to hold 10 elements. In C/C++ these elements will be indexed to the numbers 0 to 9. When I request element number 3 (which is the forth value in the arry... 0, 1, 2, 3), the compiler will multiply my index (3) with the size of the data type stored in the array (an int is 4 byte in most C compilers), to find the offset in memory from the start of the array of the data value. (data in memory = start of array + index * size of data)
The above array is not initialized... the array occupies a block of memory, but that memory may contain random data. In C/C++, to initialize an array:
Code:
int myArray[4]={3, 5, 7, 11};
Now we have initialized an array with the first four prime numbers. To access a number, we index into the array:
Code:
int myPrime=myArray[3]; // assigns the value 11 to myPrime
(Remember, the first index into the array is 0, not 1... common error in C)
Finally, we can declare an array and initialize it later:
Code:
int myArray[4];
myArray[0]=3; myArray[1]=5; myArray[2]=7; myArray[3]=11;
A dynamic array is one that is declared at run time rather than compile time. A dynamic array is exactly like a regular array, only the memory to store the elements comes from the heap at run time. Pseudo-code for a dynamic array would look something like:
Code:
int myArray[];
myArray=new int[4]; myArray[0]=3; myArray[1]=5; ...
Again, the memory is continuous, and indexing works exactly as before.
Now, onto the associate array. The code for an associate array will look just like the code above, however, the internals of how an associate array work are much, much different.
An associate array associates a key to a value. In a regular array, the keys (or indexes) are always integer values. In an associate array, this is not the case. They can be integers, but they can be strings, chars, or any other scaler type. I might use an associate array to hold configuration variables. For example:
Code:
array myConfig;
myConfig['inputRows']=40; myConfig['inputCols']=5; myConfig['someKey']="some value";
Notice the differences. My keys are now strings and I have mixed values... both strings and numbers. Some associate array implementations will allow you to have mixed keys as well. PHP for example will store both a numeric index and the given scaler index for an associate array, both pointing to the same data element!
Internally, an associate array is not continuous in memory. A lot of extra logic is used to determine where the value is stored for a given key. This makes associate arrays slower than nonassociate arrays. However, it is easy to index odd data types with an associate array without extra code. This is the tradeoff you have to consider when designing code...
To access the above associate array:
Code:
int rows=myConfig['inputRows'];
-------------------- Just another spore in the wind.
|
Anno
Experimenter



Registered: 06/17/99
Posts: 24,166
Loc: my room
Last seen: 20 days, 18 hours
|
Re: Programing 101 question [Re: Aiko Aiko]
#5440318 - 03/25/06 04:48 AM (17 years, 10 months ago) |
|
|
|
Aiko Aiko


Registered: 05/13/05
Posts: 6,395
Loc: Lazy River Road
Last seen: 3 hours, 37 minutes
|
Re: Programing 101 question [Re: Anno]
#5440342 - 03/25/06 05:48 AM (17 years, 10 months ago) |
|
|
Alright, I guess my problem may have been in trying to associate in the first place. For some reason I like to try to make things more difficult then they have to be. Thanx alot for the replies guys.
-------------------- Easily test the dosage of your tabs at home! qtests.org Man says, "God, show me and I will believe." God says, "Believe and I will show you."
|
|