0

just writing and testing the code using arrays,

int main()
{   int i,n;


    int arr[14];

    printf("\nEnter the array ",n);
    scanf("%d",&n);
    int lastindex =  n-1;
    int *l;
    l = &lastindex;
    for(i = 0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    for(i = 0;i<n;i++)
    printf("\n%d\n",arr[i]);
    printf("%d",*l);
    //arr[i] = lastindex + 2;
    //printf("\n%d",arr[i]);
    if(arr[lastindex[arr]]  < lastindex)
    {
        printf("\n %d",arr[lastindex[arr]]);
        printf("\nright");
    }
    else
    {   printf("\n%d",lastindex);
        printf("wrong");

    }
}

//output
Enter the array 5
5
4
3
2
1

5

4

3

2

1
4

4 wrong
arr[lastindex[arr]]
lastindex[arr] will be 4
arr[4]= 1
1 < 4 is true so it has to print right,but its printing wrong
whats the wrong in the code

3
Contributors
13
Replies
14
Views
4 Years
Discussion Span
Last Post by Ancient Dragon
0

lastindex[arr] is the same thing as arr[lastindex], which is 1, not 4. So the comparsion can be rewritten like this:
if(arr[arr[lastindex]] < 4)

Now, start from the innermost expression arr[lastindex] which is 1, then evaluate arr[1] which is 4.

Edited by Ancient Dragon

0

BTW, don't use index[arr] because that form is rarely, if ever, used in real life. If you intend to index an array, then do so like this: arr[index]

0

you mean you want to display the data in reverse order, such as 1 2 3 4 5 instead of 5 4 3 2 1? You already have a pointer named l, just use it

int* l = &arr[lastindex];
while( l > arr )
{
   printf("%d\n", *l);
   --l;
 }

or you could do this:

while( lastindex >= 0)
{
   printf("%d\n", arr[lastindex]);
   --lastindex;
}

Edited by Ancient Dragon

0

i need to return the lastindex of the array and then element in the index,how to do that

0

no,not in reverse order,say n is 5 and the last array index is 4,and then have to print the element in the last index

-1

how to write in code ,to return the last index and the element in the lastindex

0

Dude, go read a book on C. You're asking for something so trivial that I can't imagine anything other than total ignorance as the cause. This forum is not for holding your hand and teaching you from scratch.

0

how to write in code ,to return the last index and the element in the lastindex

lastindex is not an array, so your question makes no sense.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.