Modify the partial program below to display a number along w

Modify the partial program below to display a number along with its square and cube.

b)Implement a function to test if a C-string is all digits and, if it is, convert it to an integer. Pseudocode for this function is given below:

Sample run:

Solution

A)

#include <string> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> using namespace std; bool tryParse(char strNum[], int &num);
int main() {
int num;
char strNum[15];
bool valid = false;
while (!valid) {
cout << \"Enter an integer: \";
cin >> strNum;
valid = tryParse(strNum, num);
if (!valid)
cout << \"Invalid integer.\ \";
}
// Now that num has a value output a message to the user, // tell them what their number is and then output the number // squared and then cubed.

cout<<\"num is :\"<<num<<\"\ square :\"<<num*num<<\"\ cube :\"<<num*num*num;


cin.ignore();
cin.get();
return 0;
}

bool tryParse(char strnum[],int &num)
{

num=0;
int len=strnum.length();

for(int i=strnum.length()-1;i>=0;i--)
{
if(isdigit(strnum[i]))
num=num+atoi(strnum[i])*pow(10,len-i-1);
else return false;
}
return true;
}

B)

bool tryParse(char strnum[],int &num)
{
int len=strnum.length();
num=0;

bool allDigits=true;
for(int i=0;i<len;i++)
{
if(isdigit(strnum[i]))
num=num+atoi(strnum[i])*pow(10,len-i-1);
else

{

allDigits=false;

break;

}
}
return allDigits;
}

Modify the partial program below to display a number along with its square and cube. b)Implement a function to test if a C-string is all digits and, if it is, c
Modify the partial program below to display a number along with its square and cube. b)Implement a function to test if a C-string is all digits and, if it is, c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site