× Didn't find what you were looking for? Ask a question
Top Posters
Since Sunday
5
a
5
k
5
c
5
B
5
l
5
C
4
s
4
a
4
t
4
i
4
r
4
New Topic  
Nirali Thakkar Nirali Thakkar
wrote...
Posts: 2
Rep: 0 0
3 years ago
One hundred students are assigned lockers 1 through 100
The student assigned to locker number 1 opens all 100 lockers
The student assigned to locker number 2 then closes all lockers whose numbers are multiples of 2
The student assigned to locker number 3 toggles the status of all lockers whose numbers are multiples of 3

Locker number 3, which is open gets closed,
Locker number 6, which is closed, gets opened
And so on for as many lockers need to be toggled
The student assigned to locker number 4 toggles the status of all lockers whose numbers are multiples of 4
And so on and on for all 100 students and all 100 lockers

Which lockers will be left open after everyone completes their turns?
Convert this C++ program into x86 assembly language and find out!

#include <iostream>
using namespace std;

// In this case we will be using a global array
// This is not generally the case for any other variables
// 101 element array, all elements initialized to 0
// NOTE:  For simplicity, element 0 is unused
int Lockers[101]{ 0 };

// Boolean NOT operation just like the exam question
int NotOperation(int x)
{
    return (x == 0) ? 1 : 0;
}

// Student x toggles all lockers { x, 2x, 3x, 4x, ... }
// up to and including locker 100
void StudentTogglingLockers(int x)
{
    for (int i = x; i <= 100; i += x)
    {
        Lockers = NotOperation(Lockers);
    }
}

// 100 students walk through and toggle lockers
void ToggleLockers()
{
    for (int i = 1; i <= 100; ++i)
    {
        StudentTogglingLockers(i);
    }
}

void PrintOpenLockers()
{
    for (int i = 1; i <= 100; ++i)
    {
        if (Lockers == 1)
        {
            cout << i;
            cout << ' ';
        }
    }
    cout << endl;
}

int main()
{
    ToggleLockers();
    PrintOpenLockers();
    system("PAUSE");
}
Source  Assembly Language for Intel-Based Computers Fifth Edition by KIP R. Irvine
Read 46 times
2 Replies

Related Topics

Replies
Nirali T. Author
wrote...
3 years ago
Need that C++ code in Assembly Language.

Let me know if someone can help

Thank you
wrote...
Educator
3 years ago
What's funny is that I can probably answer this on paper, but I don't know how I'd translate that into C++... do you need assistance with that?
New Topic      
Explore
Post your homework questions and get free online help from our incredible volunteers
  1228 People Browsing
Related Images
  
 674
  
 199
  
 383
Your Opinion
What's your favorite coffee beverage?
Votes: 274

Previous poll results: How often do you eat-out per week?