Your professor is asking you to use
command-line arguments. That's what the argc and argv arguments to main are for:
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
int main( int argc, char** argv )
{
std::cout << "The executable name is: " << argv[0] << "\n";
for (int i = 1; i < argc; i++)
std::cout << "Argument " << i << " is: " << argv[i] << "\n";
}
|
C:\> a 1 2 3 A B C Hello world!
The executable name is: a
Argument 1 is: 1
Argument 2 is: 2
Argument 3 is: 3
Argument 4 is: A
Argument 5 is: B
Argument 6 is: C
Argument 7 is: Hello
Argument 8 is: world! |
Your goal is to take
one command line argument (which is always a
const char*
), turn it into an integer (I suggest using
std::stoi()
) to convert it to an integer (which you might as well name
n
).
Then find the largest power of 2 less than or equal to
n
.
Use
std::cout
to print that value, formatted the same way as the text example given to you by your professor.
Then use
std::cerr
to print the number of “basic operations” you had to perform to find that value. You should have received prior instruction as to what that means (multiplication,division, etc; comparisons, etc; function calls?).
Hope this helps.