+ 4

What is up with these practice exercises? In practice exercise 6.2, my out put and the expected output are exactly the same.

David and Alex each have aquariums. There are 8 Rainbowfishes in David’s aquarium, and 11 Angelfishes in Alex’s aquarium. Help them exchange their fishes between them. Complete the code to swap the values of variables between aquariumDavid and aquariumAlex. You will need a third free aquarium to temporarily hold the fishes from one of the aquariums in order to swap them. #include <iostream> using namespace std; int main() { int aquariumDavid = 8; int aquariumAlex = 11; int aquarium3 = 0; // your code goes here aquarium3 = aquariumDavid ; aquariumDavid = aquariumAlex ; aquariumAlex = aquarium3 ; { cout << "David's aquarium: " << aquariumDavid << endl; } { cout << "Alex's aquarium: " << aquariumAlex; } return 0; }

13th Jan 2021, 7:34 PM
Michael Maxwell
Michael Maxwell - avatar
8 Antworten
+ 5
Sorry I’m late, but this worked for me #include <iostream> using namespace std; int main() { int aquariumDavid = 8; int aquariumAlex = 11; int aquariumSpare = 0; aquariumSpare = aquariumDavid; aquariumDavid = aquariumAlex; aquariumAlex = aquariumSpare; // your code goes here cout << "David's aquarium: " << aquariumDavid << endl; cout << "Alex's aquarium: " << aquariumAlex; return 0; }
28th Apr 2021, 6:01 PM
Dylan Allison
Dylan Allison - avatar
+ 4
I'm having trouble finding that practice question. That's from the c++ course, right? If you want to share a screenshot of what you're seeing, you could use: https://pasteboard.co/ Until I see it for myself, you could write your code a little shorter like this: #include <iostream> using namespace std; int main() { int aquariumDavid = 8; int aquariumAlex = 11; int aquarium3 = 0; aquarium3 = aquariumDavid ; aquariumDavid = aquariumAlex ; aquariumAlex = aquarium3 ; cout << "David's aquarium: " << aquariumDavid << endl; cout << "Alex's aquarium: " << aquariumAlex; return 0; } The above won't fix your problem but it is just shorter and cleaner. Most problems have multiple test cases so I expect not reading in the values for aquariumDavid or aquariumAlex could be part of the problem. Another potential problem could be the output not being EXACTLY the same. For example, you say aquarium when it should be Aquarium or you didn't add enough spaces before a colon.
12th Apr 2021, 9:22 AM
Josh Greig
Josh Greig - avatar
+ 1
The fastest way i found to solve this was just using the swap() function. So only 1 line of code is needed: // your code goes here swap (aquariumDavid, aquariumAlex);
3rd Jul 2022, 10:50 AM
Netsy
Netsy - avatar
0
disculpen pero no lo e podido lograr
7th Jul 2021, 6:39 AM
J.m Z.m
J.m Z.m - avatar
0
Hello, I just wanted to check what is left in swap aquarium after the exchange, the result was 8 not zero. So these codes works but I think we are not doing correctly. Any ideas?
7th Sep 2021, 8:37 PM
Filiz
0
I think there is a certain way the problem is to be approached but I literally just swapped the numbers in the integers. Cheesy? Thinking outside the box? It worked. #include <iostream> using namespace std; int main() { int aquariumDavid = 11; int aquariumAlex = 8; cout << "David's aquarium: " << aquariumDavid << endl; cout << "Alex's aquarium: " << aquariumAlex; return 0; }
17th Jan 2022, 8:12 AM
Zachariah Willett
0
What I did at first was subtract 3 from David’s and added 3 to Alex’s. The output was the same as the expected, but it bothered me that the hint was to create another variable as a temp storage.
24th Jan 2022, 8:19 PM
Philip Angelo Florita
0
#include <iostream> using namespace std; int main() { int aquariumDavid = 8; int aquariumAlex = 11; int aquariumSpare = 0; aquariumSpare = aquariumDavid; aquariumDavid = aquariumAlex; aquariumAlex = aquariumSpare; // your code goes here cout << "David's aquarium: " << aquariumDavid << endl; cout << "Alex's aquarium: " << aquariumAlex; return 0; } Good Luck
25th Jan 2022, 10:49 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar