0

How to find the last position of an element in that array?

Char ar[10]={'1','2','3',};

8th Feb 2022, 2:04 PM
Aswin
Aswin - avatar
7 odpowiedzi
+ 2
//You can put values like this: char a[] = { '1', '2', '3', '4', '5' }; or char a[10] = { '1', '2', '3', '4', '5' }; int len= sizeof(a) / sizeof(a[0]); //both arrays works, but first one no need loop //len value is 5 //just need cout<<a[len-1]; //but second way need this loop why because the value for len=10 for 2nd array so need loop So in that case, use @JaScript code to avoid printing empty character and only print last assigned value and break loop. for(int i=len-1; i>=0;i--) if (a[i]) { cout<<a[i]; break; }
8th Feb 2022, 3:35 PM
Jayakrishna 🇮🇳
+ 1
Find array length by int len = sizeof(ar) / sizeof(ar[0]) ; and ar[len-1] is the last value in array... but if you declare length as 10 , then ar[9] is the last value.. if declare length as 10 then, len= 10 only with the above method. use char ar[]={'1', '2',..... };
8th Feb 2022, 2:27 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 thank u very much
8th Feb 2022, 4:23 PM
Aswin
Aswin - avatar
+ 1
size of the array / size of its type (you can get the second by passing one of its element — the safest being the first (index 0) element) to the sizeof function. If you're building a class with a array as one of its members, it may make sense to add an extra int size member to your class with a getter (just don't forget to update the int whenever it makes sense)
8th Feb 2022, 8:31 PM
Hiba al-Sayf
Hiba al-Sayf - avatar
0
Please 🙏 Help me
8th Feb 2022, 2:10 PM
Aswin
Aswin - avatar
0
Jayakrishna🇮🇳 but I want to input the values in that array and find the last position of that element
8th Feb 2022, 2:51 PM
Aswin
Aswin - avatar
0
You're welcome...
8th Feb 2022, 4:39 PM
Jayakrishna 🇮🇳