Can you please add notes to code so I can learn what I am looking at. Thank you.
ID: 3703386 • Letter: C
Question
Can you please add notes to code so I can learn what I am looking at. Thank you.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int menu();
int doubleIt(int);
int getPower(int,int);
int reverse(int);
int sum(int);
int twoDigit(int);
int threeDigit(int);
int main()
{int n;
srand(time(0));
n=rand()%90+10;
int result;
do
{
cout<<"The number is: "<<n<<endl<<endl;
switch(menu())
{case 1: result=doubleIt(n);
cout<<"Ans: "<<result<<endl;
break;
case 2: result=reverse(n);
cout<<"Ans: "<<result<<endl;
break;
case 3: result=getPower(n, 3);
cout<<"Ans: "<<result<<endl;
break;
case 4: result=sum(n);
cout<<"Ans: "<<result<<endl;
break;
case 5: result=twoDigit(n);
cout<<"Ans: "<<result<<endl;
break;
case 6: result=threeDigit(n);
cout<<"Ans: "<<result<<endl;
break;
case 7: return 0;
}
}while(true);
}
int doubleIt(int n)
{return n*2;
}
int reverse(int n)
{int newnum=0;
while(n>0)
{newnum=newnum*10+n%10;
n=n/10;
}
return newnum;
}
int sum(int n)
{int sum=0;
while(n>0)
{sum=sum+n%10;
n=n/10;
}
return sum;
}
int twoDigit(int n)
{int d1,d2;
if(n<100)
{d1=n%10;
d2=n/10;
return getPower(d1,d2);
}
return n;
}
int threeDigit(int n)
{int d1,n2;
if(n>99)
{d1=n%10;
if(d1<=4)
{n2=n/10;
return getPower(n2,d1);
}
}
return n;
}
int getPower(int x,int y)
{int ans,i;
if(y==0)
return 1;
else
{ans=x;
for(i=2;i<=y;i++)
ans=ans*x;
return ans;
}
}
int menu()
{int choice=8;
while(choice<1||choice>7)
{cout<<"1. double the number ";
cout<<"2. reverse the digit of the number ";
cout<<"3. raise the number to the power of 3 ";
cout<<"4. sum thee digits of the number. ";
cout<<"5. if the number is a two digit number then raise the first digit to the power of the second digit ";
cout<<"6. If the number is a three digit number ";
cout<<" and the last digit is less than or equal to 4 then raise the first two digits to the power of the last digit. ";
cout<<"7. exit ";
cin>>choice;
}
return choice;
}
Explanation / Answer
here i have commented the code..what ever i can..
===============================================================
//required header files
#include <iostream>
#include <ctime>
#include <cstdlib>
//namespace
using namespace std;
//function prototypes
int menu();
int doubleIt(int);
int getPower(int,int);
int reverse(int);
int sum(int);
int twoDigit(int);
int threeDigit(int);
//start of the main function
int main()
{
int n;
//seed a value zero so that in every execution we get different random numbers
srand(time(0));
//generate a random number between 10 to 90
n=rand()%90+10;
//variables
int result;
//start of do while loop to continuosly ask the user to input choice until he entered choice 7
do
{
cout<<"The number is: "<<n<<endl<<endl;
//switch to menu
switch(menu())
{
//switch to each case as value return by menu function
case 1: result=doubleIt(n);
cout<<"Ans: "<<result<<endl;
break;
case 2: result=reverse(n);
cout<<"Ans: "<<result<<endl;
break;
case 3: result=getPower(n, 3);
cout<<"Ans: "<<result<<endl;
break;
case 4: result=sum(n);
cout<<"Ans: "<<result<<endl;
break;
case 5: result=twoDigit(n);
cout<<"Ans: "<<result<<endl;
break;
case 6: result=threeDigit(n);
cout<<"Ans: "<<result<<endl;
break;
case 7: return 0;
}
}while(true); //while true
}
//defintion to double the input number
int doubleIt(int n)
{
return n*2;
}
//defintion to reverse the input number
int reverse(int n)
{
int newnum=0;
while(n>0)
{
newnum=newnum*10+n%10;
n=n/10;
}
return newnum;
}
//defintion to sum the digit of the numbers
int sum(int n)
{
int sum=0;
while(n>0)
{
sum=sum+n%10;
n=n/10;
}
return sum;
}
//defintion of the function of the number is a two digit
int twoDigit(int n)
{
int d1,d2;
if(n<100)
{d1=n%10;
d2=n/10;
return getPower(d1,d2);
}
return n;
}
//defintion of the threeDigit function
int threeDigit(int n)
{
int d1,n2;
if(n>99)
{d1=n%10;
if(d1<=4)
{n2=n/10;
return getPower(n2,d1);
}
}
return n;
}
//defintion for the getPower function
int getPower(int x,int y)
{
int ans,i;
//if power is 0 return 1
if(y==0)
return 1;
//else calculate the power by multiplying the x y times
else
{ans=x;
for(i=2;i<=y;i++)
ans=ans*x;
return ans;
}
}
//defintion of the menu function
int menu()
{
int choice=8;
//while user input is not between 1 and 7 display menu
while(choice<1||choice>7)
{
//display the menu
cout<<"1. double the number ";
cout<<"2. reverse the digit of the number ";
cout<<"3. raise the number to the power of 3 ";
cout<<"4. sum thee digits of the number. ";
cout<<"5. if the number is a two digit number then raise the first digit to the power of the second digit ";
cout<<"6. If the number is a three digit number ";
cout<<" and the last digit is less than or equal to 4 then raise the first two digits to the power of the last digit. ";
cout<<"7. exit ";
cin>>choice;
}
//return the choice to calling function
return choice;
}
==============================================================
KIndly Check and Verify Thanks..!!!