(CNN) -- U.S. President Barack Obama wrapped up his visit to South Africa on Sunday with a visit to the prison cell where anti-apartheid leader Nelson Mandela was held and a call to students to help build a new Africa.
Speaking at the University of Cape Town, Obama said a rising Africa offers new hope for a continent where more than 60% of the population is under 35. But he said Africa's economic growth and political progress in African states "rests on a fragile foundation," still vulnerable to corruption, repression and inequality.
"Just like previous generations, you've got choices to make. You get to decide where the future lies," he said.
"You've got time and numbers on your side, and you'll be making decisions long after politicians like me have left the scene," he added. "And I can promise you this: The world will be watching what decisions you make."
Obama told students that the American college campaigns against investment in apartheid-era South Africa in the 1980s inspired him to get involved in a public cause for the first time. Cape Town University was the site of a famous speech by Sen. Robert F. Kennedy at the height of apartheid in 1966, and Obama said the leadership of figures like Kennedy, the now-ailing Mandela and Indian independence leader Mahatma Gandhi -- who began his career as a lawyer in South Africa -- "stand as a challenge to your generation."
"They tell you that your voice matters," he said. "Your ideals, your willingness to act on those ideas, your choices can make a difference. And if there's any country in the world that shows the power of human beings to effect change, this is the one."
Obama spoke after touring Robben Island, where Mandela was held during most of his 27 years in prison for fighting South Africa's now-dismantled system of white minority rule and racial segregation. The president spent a few minutes in the cell where Mandela slept during that stretch, quietly contemplating the concrete walls and the mat that served as a bed.
Veteran anti-apartheid activist Ahmed Kathrada, who had been imprisoned with Mandela on the island, was the first family's guide during the visit.
Obama had visited the island prison once before, in 2006. Sunday's visit was the first for the family members who accompanied him -- first lady Michelle Obama; the couple's daughters, Sasha and Malia; Mrs. Obama's mother, Marian Robinson; and a cousin, Leslie Robinson.
"For me to be able to bring my daughters there and teach them the history of that place and this country ... that's a great privilege and a great honor," Obama told the students.
Obama also visited the anti-HIV program now led by Desmond Tutu, the former Anglican archbishop of Cape Town and another leading anti-apartheid campaigner. While Obama praised the center's battle against HIV, the virus that causes AIDS, Tutu urged Obama to keep up his efforts to restart Israeli-Palestinian peace talks and close the U.S. prison camp at Guantanamo Bay, Cuba.
"As you have been here before to Africa, you have heard us speak of something called 'ubunto,' " Tutu said. "Your success is our success. Your failure, whether you like it or not, is our failure. And so we want to show you that we pray for you to be a great success."
Mandela, 94, has been in a Pretoria hospital for more than three weeks and has been in critical condition since June 23. He was hospitalized for a lung infection that has plagued him since his days on Robben Island.
Obama met some of Mandela's relatives Saturday and spoke by telephone with his wife, who maintains a vigil by his bedside.
The president's decision not to visit the hospital was out of respect for the family's wishes, according to the White House.
"I expressed my hope that Madiba draws peace and comfort from the time that he is spending with loved ones and also expressed my heartfelt support for the entire family as they work through this difficult time," Obama said, referring to Mandela by his clan name.
A meeting between the U.S. president and Mandela would have had historic significance. Like Obama, Mandela broke through racial barriers to become the first black president of his country. The two met when Obama was a senator.
Mandela became an international figure for his fight against apartheid. He was elected to the nation's highest office in 1994, four years after his release from prison, and remains popular worldwide as an icon of peaceful reconciliation.
Obama's visit to Africa's biggest economy is part of a three-nation trip that started in Senegal and will end in Tanzania this week.
It aims to bolster U.S. investment opportunities, address development issues such as food security and health, and promote democracy. It comes as China aggressively engages the continent, pouring billions of dollars into it and replacing the United States as Africa's largest trading partner.
During the trip, Obama pledged $7 billion to help combat frequent power blackouts in sub-Saharan Africa. The "Power Africa" campaign aims to double access to electricity throughout the region, extending service to 20 million new businesses and homes, the White House said.
"Access to electricity is fundamental to opportunity in this age. It's the light that children study by, the energy that allows an idea to be transformed into a real business. It's the lifeline for families to meet their most basic needs, and it's the connection that's needed to plug Africa into the grid of the global economy," Obama said at Cape Town University.
His visit also included a town hall with young people in Soweto, a Johannesburg neighborhood at the heart of the anti-apartheid struggle. He also held bilateral talks with South African President Jacob Zuma in Pretoria, with trade high on the agenda.
CNN's Laura Smith-Spark contributed to this report