How to find the last value in an array

This is a discussion on How to find the last value in an array within the C Programming forums, part of the General Programming Boards category; How to find the last value in an array Hi experts, I am new to C, i need to insert ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    How to find the last value in an array

    How to find the last value in an array
    Hi experts,
    I am new to C, i need to insert an element in the middle of an array,for that i thought a logic that after finding the last value in an array i can shift the value and after that i can insert the new value for this i have to find the last value in an arry,how can i find the last value in an array.

    example

    int arr[30]



    in this i have to insert the element after the 4 location,for that i thought to find the last value in an array

    ex the last value in the arr[15].


    thanks and regards,
    kanimozhi.m

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Wouldn't the last value in the array arr[30] just be arr[29]? Otherwise, you need to pick a way to mark an element as invalid and just iterate backwards through the array until you find an element that is valid.
    If you understand what you're doing, you're not learning anything.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by mkanimozhi View Post
    ex the last value in the arr[15].


    thanks and regards,
    kanimozhi.m
    I think you've answered your own question. How do you know that the last value is in arr[15]? I mean if you're inputting the array yourself, you would have made a point till which you're inputting, that point will be your last index.
    Code:
    int arr[30],i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    Ofcourse actually the last element can be stored till arr[29], but it could be less than it also.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find the middle element of a bulk array?
    By void_mehboob in forum C Programming
    Replies: 4
    Last Post: 04-19-2009, 11:37 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Easiest way to find the max value stored in an array
    By criticalerror in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2004, 02:35 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21