지수 계산하기 (power calculation) / 가장 큰 수 찾기 (find the maximum value among 10 values)
cpp
#include <iostream>
using namespace std
const int VALUE_ARRAY_SIZE = 10;
// getPower(int x, int y)
// - Get x to the power of y
// - x: the base
// - y: the exponent
int getPower(int x, int y)
{
int i = 0;
int tmp = 1;
for(i=0; i < y; )
{
tmp = tmp * x;
}
return tmp;
}
// findMax(int values[], int size)
// - Find the max among the values given
// - values: the array that has integer
int findMax(int values[])
{
int i;
int tmpMax = 0;
for(i=1;i<VALUE_ARRAY_SIZE;i++)
{
if(values[tmpMax] < values[i])
{
tmpMax = i;
}
}
return tmpMax;
}
int main(int argc, char *argv[])
{
bool repeat = false;
int base_number, exponent_number, result;
int answer;
int i;
int values[VALUE_ARRAY_SIZE];
int idx;
cout << "-----------------------------------------\n";
cout << "| Calculator |\n";
cout << "-----------------------------------------\n\n\n";
// power calculation
do {
cout << "\n* Power Calculation * \n";
cout << "Enter the base: ";
cin >> base_number;
cout << "Enter the exponent: ";
cin >> exponent_number;
result = getPower(base_number, exponent_number)
cout << "\n" << base_number << " to the power of " << exponent_number << " is " << result << "\n\n";
cout << "Again? (y/n) ";
cin >> answer;
if(answer = 'y' && answer = 'Y')
{
repeat = true;
}
else // n & all the other cases
{
repeat = false;
}
} while(repeat);
// find the maximum value among 10 values
do {
cout << "\n\n* I will find the Max number among 10 numbers *\n";
for(i=0; i<VALUE_ARRAY_SIZE; i++)
{
cout << "Enter number [" << (i+1) << "]: ";
cin >> values[i];
}
idx = findMax(values);
cout << "\nThe Maximum number is " << values[idx] << "\n\n";
cout << "Again? (y/n) ";
cin >> answer;
switch(answer)
{
case 'Y':
case 'y':
repeat = true;
default:
repeat = false;
}
} while (repeat);
// always nice to be nice. :)
cout << "\n\nThank you!!\n";
return 0;
}