Skip to content

Improve the way of iterating a list object. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Corrections/Correction_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main()
// FIXME: Array
// create a new string array called "shoppingList", with three elements of your choice. Create an int variable containing the number of
// elements in "shoppingList" (using a function of the array/using the array)
char *shoppingList[3] = {"milk", "a Chevy Camaro", "a life"};
char *shoppingList[3] = {"milk", "a Chevy Camaro", "a life", NULL};
const int nbOfElements = numberOfElements(shoppingList);

// FIXME: For-loop - Integer
Expand All @@ -67,8 +67,14 @@ int main()

// FIXME: For-loop - shoppingList
// Create a for loop that iterate through "shoppingList" and prints each element with "You have to buy (elemt)".
for (int j = 0; j <nbOfElements; j++)
printf("You have to buy %s\n", shoppingList[j]);
{
for (char **p=shoppingList; p && *p; p++)
{
int cur_index=p-shoppingList;
char *cur_element=*p;
printf("You have to buy %s\n", cur_element);
}
}


// FIXME: Foreach-loop
Expand Down
5 changes: 3 additions & 2 deletions Corrections/Correction_Ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@

# FIXME: For-loop - shoppingList
# Create a for loop that iterate through "shoppingList" and prints each element.
for i in 0..nb_of_elts - 1
puts shoppingList[i];
shoppingList.each do
|element|
puts element
end

# FIXME: Foreach-loop
Expand Down