So basically the user inputs how many countries they visitd in a variable, then I take that var and use it as the size of the array. After doing it, I use a for loop to list out all the countries to visit. But I want to make my code a little smarter and put 'and' at the end of the sentence for the final country they visited so it looks smart. For example if it was 3 countries You visited Japan, Korea, *and* Canada
How do I access the last element in an array?
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int number_of_p = get_int("How many countries did you visit?\n");
string countries[number_of_p];
for (int x = 0; x < number_of_p; x++)
{
countries[x] = get_string("What countries did you visit?\n");
}
printf("You visited %i countries, including: ", number_of_p);
for (int x = 0; x < number_of_p; x++)
{
printf("%s, ", countries[x]);
/* looks for last element in arrays, inserts 'and'
to make it look grammatically correct. */
if (countries[x] == number_of_p - 1 )
{
printf(" and ");
}
}
printf(".\n");
}This is my code. Please fix it and teach me how to access the last element in array and how to set up for the loop.
Videos
Last element is in position
Copysizeof(array)/sizeof(array[0]) - 1
There is no defined term as empty array
Your array will always hold some value even if you dont initialize it explicitly
You need to define in your application how will you term it as empty may be by considering if its(element of array) value is 0 or some other value
items.end(), and other end-of-container iterators (e.g. items.rend()) point to one past the end of the list. Try items.back()->getID() + 1;.
std::list::end returns an iterator to the element following the last element of the container, This element acts as a placeholder; attempting to access it results in undefined behavior.
You could use std::list::back or reverse iterator std::list::rbegin
try:
items.back()->getID() +1;
(*items.rbegin())->getID() +1;
Try this :)
MusicRec *getLastItem(MusicRec *theList)
{
MursicRec *currentElement;
currentElement = theList;
if (currentElement == NULL) // return NULL is list is empty cf (Captain girafe && learningC)
return (NULL);
while(currentElement->next != NULL) // check if next element is null then currentElement = next else return currentElement
currentElement = currentElement->next;
return (currentElement);
}
you should return theList.
when loop ends currentElement is NULL. so theList will be pointing at the last node.
And make sure that theList received by the function is not NULL.
To get the last item of a collection use LastOrDefault() and Last() extension methods
var lastItem = integerList.LastOrDefault();
OR
var lastItem = integerList.Last();
Remeber to add using System.Linq;, or this method won't be available.
If you just want to access the last item in the list you can do
if (integerList.Count > 0)
{
// pre C#8.0 : var item = integerList[integerList.Count - 1];
// C#8.0 :
var item = integerList[^1];
}
to get the total number of items in the list you can use the Count property
var itemCount = integerList.Count;
I think you can do:
std::list<int> l = {1, 2, 3}; // (types shouldn't matter), you can do *std::prev(l.end(), 2);
i was trying to get the new second element of the list, using std::next.
You are not getting the new second element of the list, what you are trying to get is the next new address of the list, since what you are passing is the pointer, the address of the list, not the iterator:
list *getNextXValue(list *Head, int x)
{
return std::next(Head, x);
}
Try this instead:
#include <iostream>
#include <list>
#define LOG(x) std::cout << x << std::endl;
typedef std::list<int> list;
std::list<int>::iterator getNextXValue(std::list<int>::iterator Head, int x) {
return std::next(Head, x);
}
/**
* @brief Find last element of a linked list of ints
*
* @return int Program ended correctly
*/
int main() {
list listOne;
list *aux = NULL;
// Inserts all the elements in the list
for (int i = 0; i < 100; i++) {
listOne.insert(listOne.end(), i);
}
listOne.reverse();
auto next = getNextXValue(listOne.begin(), 1);
std::cout << *next << std::endl;
return 0;
}
Hello,
How to retrieve the last element of a list in c#.
I have tryed liste.FindLast(), it's asking a predicate
I think I can use liste(liste[liste.Count()] but it's too long and don't work 😣