GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Mar-2010, 19:02
Hollywood99 Hollywood99 is offline
New Member
 
Join Date: Mar 2010
Posts: 3
Hollywood99 is on a distinguished road

First and last element in an array


how would you call the first and last element in an array?
  #2  
Old 08-Mar-2010, 21:03
ahbi82 ahbi82 is offline
Regular Member
 
Join Date: Jul 2006
Posts: 325
ahbi82 has a spectacular aura aboutahbi82 has a spectacular aura about

Re: array help


CPP / C++ / C Code:
#define SIZE 10

void main(void)
{
    int arrInt[SIZE];    // array of 10 integers

    // First element of array is at index 0.
    arrInt[0];


    // Last element of array is at index size -1.
    arrInt[SIZE - 1];
}

Not really sure what's your question. Hope my illustration answered your query.
  #3  
Old 09-Mar-2010, 11:32
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: First and last element in an array


Quote:
how would you call the first and last element in an array?
Understand that you don't "call" an array element.
But there are several different ways that you can "access" an array element
to get or change the value stored there.

Maybe a little about determining the array size instead of just having a #defined size.
CPP / C++ / C Code:
#include <stdio.h>

int main(void)
{
  /*  Here is defined an array of 10  int size elements:                         */

  int array[10] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
  int last = 0;

  /*  The array elements are numbered (indexed) 0 thru 9.
      ie: the 1st element is index 0 , the 2nd is index 1 etc.

      So the first element will always be at index 0.
      To find the index of the last element we use the "sizeof" operator to 
      find both the size of the whole array  AND  the size of one element:
  */

  printf("\nsizeof(array): %u \nsizeof(array[0]): %u \n", sizeof(array), sizeof(array[0]) );

  /* So if the size is 10 then the last index is 9. */

  last = ( sizeof(array) / sizeof(array[0]) ) - 1;

  printf("The last element is at index %d and the value stored there is %d. \n\n", last, array[last] );

  return 0;
}
Code:
Output: ~> gcc -Wall -W -pedantic 100309_last_element.c -o 100309_last_element ~> ./100309_last_element sizeof(array): 40 sizeof(array[0]): 4 The last element is at index 9 and the value stored there is 20.
  #4  
Old 09-Mar-2010, 11:52
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: First and last element in an array


...As long as it's a static array. The OP never stated whether he was using a static or dynamic array, so just to clarify:

CPP / C++ / C Code:
int x [3];
int * y = new int [3];
printf("%d %d %d\n", sizeof(x), sizeof(*y), sizeof(y));

The output is 12 4 4, because x is an int [3], *y is an int, and y is an int *.
__________________
www.blake-foster.com
  #5  
Old 10-Mar-2010, 21:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: First and last element in an array


Quote:
Originally Posted by Hollywood99
how would you call the first and last element in an array?
I'd call them alpha and omega


Quote:
Originally Posted by Blake
...As long as it's a static array. The OP never stated whether he was using a static or dynamic array...
Why do people always have to bring up advanced concepts to obfuscate any help given? If they don't say they are using advanced concepts, they probably aren't so leave the confusion (and ego boost*) out of the discussion.


* "Gee, ain't I smart. I know advanced stuff. Watch me show my expertise!"
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #6  
Old 10-Mar-2010, 23:48
ahbi82 ahbi82 is offline
Regular Member
 
Join Date: Jul 2006
Posts: 325
ahbi82 has a spectacular aura aboutahbi82 has a spectacular aura about

Re: First and last element in an array


Quote:
Originally Posted by WaltP
I'd call them alpha and omega



Why do people always have to bring up advanced concepts to obfuscate any help given? If they don't say they are using advanced concepts, they probably aren't so leave the confusion (and ego boost*) out of the discussion.


* "Gee, ain't I smart. I know advanced stuff. Watch me show my expertise!"

I don't consider that "advanced stuff" advanced! CAUSE i'm more smart. Lolz. Just kidding.

Anyway i think that's basic, i believe all books will have this.
  #7  
Old 11-Mar-2010, 04:12
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: First and last element in an array


I still have the lecture slides from the college class where I learned C++, and they cover static and dynamic arrays in the very same lecture. Are dynamic arrays really that much more advanced?

I have no interest in boosting my ego. I do, however, think it is important that the OP is not left with the impression that the sizeof operator is the answer for finding the size of any array. Forgive me for trying to make sure the OP had a complete answer to his/her question.
__________________
www.blake-foster.com
Last edited by Blake : 11-Mar-2010 at 05:04.
  #8  
Old 11-Mar-2010, 08:38
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: First and last element in an array


Quote:
...where I learned C++, and they cover static and dynamic arrays in the very same lecture.
Are dynamic arrays really that much more advanced?
I think yes dynamic arrays are that much more advanced for someone who is asking "how to call the first and last element".
Seems to me like they would need to understand the basics first.
Especially when you get into dimensional arrays.

Along with that this is the C forum and you used the "new" allocator from C++ and C# instead of the malloc() or calloc() functions we use in C.
That would just confuse me if I was just starting out. (which is easy to do).

Yes sizeof() is not the only way to determine size, ahbi82 's example used a "defines" identifier to hold the size.
But those are the only ways I know for an array in C. Is there another?
  #9  
Old 11-Mar-2010, 08:51
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: First and last element in an array


I guess we have different opinions about how arrays should be taught, so I think we can just agree to disagree and leave it at that.

I'm well aware of how malloc and calloc work, but I forgot that I was in the C forum when I posted that response. I wasn't going to say anything, because I though that would be confusing.

I didn't mean to imply that there was another way of determining the size of an array. I would actually argue that sizeof is the only way, and that ahbi82's example is keeping track of the size, not determining the size.
__________________
www.blake-foster.com
  #10  
Old 11-Mar-2010, 21:15
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 1,015
admin will become famous soon enough

Re: First and last element in an array


May I add something? The information on this page can potentially help many others in the future, not only Hollywood99. So anything you may add to the discussion is relevant to someone else.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
What is an array? Howard_L C Programming Language 3 05-Oct-2007 05:11
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 14:43
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:19.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.