6 Replies - 5251 Views - Last Post: 24 December 2013 - 11:45 AM

#1 AnalyticLunatic   User is offline

  • D.I.C Lover

Reputation: 239
  • View blog
  • Posts: 1,073
  • Joined: 25-June 12

The difference in integer subscript arrays (C, Ada, Perl)?

Posted 11 December 2013 - 04:09 PM

Since most of my experience is in VB/C# (.NET), I thought I would ask those with more experience under their belt what exactly the difference in regard to arrays (with integer subscripts) is between C, Ada, and Perl.

I was able to find some information on C, Perl, and even a little on Ada, but was wondering if some people in the community could offer their own input as well.
-------------------------------------------------------------------
On Exforsys I found the following with regards to C (which of course is similar to the next gen C#):

Declaring Single Dimension Arrays
<data type> array_name[size_of_array];
char game_map[4];
int emp_code[3]; 


Can be initialized 2 ways, on Declaration or by Assignment:

Declaration:
<data type> array_name[size_of_array] = {element 1, element 2, ...};
char game_map[3] = {'S', 'R', 'D'};


Assignment
char game_map[3]; 
game_map[0] = 'S';
game_map[1] = 'R';
game_map[2] = 'D';


Accessing Single Dimensional Array Elements:

array_name[index_of_element];
array_name[index_of_element] = value;
game_map[0]; //value of 'S'
game_map[1]; //value of ' R'
game_map[2]; //value of 'D' 



Quote

Trying to access a value outside the bounds of index 1 through size_of_array – 1, results in runtime errors. Your compiler will not complain, but your program will crash when it executes.

game_map[3];


will lead to a runtime error and you program would crash because you tried to access a memory location outside the bound of the array, otherwise known as a segmentation fault.


---------------------------------------------------------------------------
PERL Arrays

Initialization:
@array = ( 1, 2, 3 );
@array = function_generating_a_list();
@array = @another_array;



Quote

The key points are that the assignment to an array gives list context to the right hand side; the right side can be any expression which results in a list of zero or more scalar values. The values are inserted in the array in the same order as they occur in the list, beginning with array index zero. For example, after executing

@array = ( 'a', 'b', 'c' );


element 0 will contain 'a', element 1 will contain 'b', and so on.

Whenever an array is assigned to en masse like this, any contents it may have had before the assignment are removed!

Quote

Accessing Elements in Array

The first element of an array is accessed at index 0:

$first_elem = $array[0];


Get count of elements
To get the "length" or "size" of an array, simply use it in a scalar context. For example, you can "assign" the array to a scalar variable:

$count = @array;


and the scalar variable will afterwards contain the count of elements in the array. Other scalar contexts work as well:

print "# Elements: " . @array . "\n";


(Yes, print gives its arguments list context, but the dot (string concatenation) operator takes precedence.)

You can always force scalar context on an array by using the function named scalar:

print "# Elements: ", scalar(@array), "\n";

Quote

Get the highest index

Often, you want to know what is the highest index in an array — that is, the index of its last element. Perl provides a special syntax for obtaining this value:

$highest_index = $#array;

Quote

Remove an element from the end

The function to remove a single element from the end of an array is pop. Given the code:

@array = ( 'a', 'b', 'c' );
$x = pop @array;


$x will contain 'c' and @array will be left with two elements, 'a' and 'b'.

Quote

Remove an element from the beginning

The shift function removes one value from the beginning of the array. That is, it removes (and returns) the value in element zero, and shifts all the rest of the elements down one, with the effect that the number of elements is decreased by one. Given the code:

@array = ( 'a', 'b', 'c' );
$x = shift @array;

------------------------------------------------------------------------------------------
Searched: ADA

Array Declaration:

procedure demo is
            squares: array (1 .. 10) of integer;
        begin
            for i in 1 .. 10 loop
                squares(i) := i ** 2;
            end loop;
        end demo;


Aggregate Assignment:

 procedure demo is
            squares: array (1 .. 5) of integer;
        begin
            squares := (1, 4, 9, 16, 25);  -- aggregate assignment
            for i in 1 .. 10 loop
                put(squares(i));
            end loop;
        end demo;


Array Attributes:

Attributes of array types:
  • 'first
  • 'last
  • 'range
  • 'length


Attributes of array variables:
  • 'first
  • 'last
  • 'range
  • 'length


Attributes allow code to be written that is independent of the array bounds.

Is This A Good Question/Topic? 0
  • +

Replies To: The difference in integer subscript arrays (C, Ada, Perl)?

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: The difference in integer subscript arrays (C, Ada, Perl)?

Posted 11 December 2013 - 04:34 PM

Sorry, but I'm not sure what the question is? Different languages have different array initialisation syntax, and array methods.. but the concept of arrays, and integer indexes, is pretty standard.

There are exceptions of course: Python lists, Javascript 'arrays as objects', and I think some functional languages have tables and/or lists (even matrices and vectors).

Perhaps you can clarify your question.
Was This Post Helpful? 0
  • +
  • -

#3 AnalyticLunatic   User is offline

  • D.I.C Lover

Reputation: 239
  • View blog
  • Posts: 1,073
  • Joined: 25-June 12

Re: The difference in integer subscript arrays (C, Ada, Perl)?

Posted 11 December 2013 - 04:56 PM

View Postandrewsw, on 11 December 2013 - 11:34 PM, said:

Perhaps you can clarify your question.


Sorry Andrew, I guess I got to tackling this and lost track. When it comes right down to it, I'm attempting to compare/contrast integer-subscripted arrays in C, Ada, and Perl.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: The difference in integer subscript arrays (C, Ada, Perl)?

Posted 11 December 2013 - 09:16 PM

Are you asking from the usage aspect of arrays in those languages, or from the implementation aspect of the arrays in those languages? From your original post, you seem to be more concerned about the usage side.
Was This Post Helpful? 0
  • +
  • -

#5 AnalyticLunatic   User is offline

  • D.I.C Lover

Reputation: 239
  • View blog
  • Posts: 1,073
  • Joined: 25-June 12

Re: The difference in integer subscript arrays (C, Ada, Perl)?

Posted 11 December 2013 - 09:18 PM

View PostSkydiver, on 12 December 2013 - 04:16 AM, said:

Are you asking from the usage aspect of arrays in those languages, or from the implementation aspect of the arrays in those languages? From your original post, you seem to be more concerned about the usage side.


I suppose both, really.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: The difference in integer subscript arrays (C, Ada, Perl)?

Posted 24 December 2013 - 11:21 AM

Quote

Trying to access a value outside the bounds of index 1 through size_of_array – 1

This is from the C section and is wrong. The lower bound for C is 0, not 1.

For Ada the lower and upper bound of the array is specified, and doesn't have to start at 1 (or 0). For example
years: array (2000 .. 2020) of integer;
This defines an array that only excepts the values from 2000 to 2020 (inclusive) as valid indexes. Oddly enough, you can do this in C# also.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: The difference in integer subscript arrays (C, Ada, Perl)?

Posted 24 December 2013 - 11:45 AM

View PostMomerath, on 24 December 2013 - 06:21 PM, said:

Oddly enough, you can do this in C# also.

To clarify, it is possible to achieve this (a non-zero lower-bound) in C# using Array.CreateInstance. Just because it is possible though, doesn't mean it is a good idea ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1