first one, we were supposed to a add on a few things to an example in the book. The example was to find a a palindrome(dont know if that is spelled right). One addition that my instructor put on us is to not accept user input. soooooo...here is the code....
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool isNumPalindrome(int num);
int main(int argc, char *argv[])
{
cout << "answers in binary" << endl;
cout << "the answer is: " << isNumPalindrome(646) << endl;
system("PAUSE");
return 0;
}
bool isNumPalindrome(int num)
{
int pwr = 0;
if (num < 10)
return true;
else
{
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num >= 10)
{
int tenTopwr = static_cast<int>(pow(10.0, pwr));
if ((num / tenTopwr) != (num % 10))
return false;
else
{
num = num % tenTopwr;
num = num / 10;
pwr = pwr - 2;
}
}
return true;
}
}
some things I feel I could have dont is put in a switch system so that it output "the number is not a palindronme" or "it is a palindrom" instead of binary
something like
switch (something here)
case 0:
cout << "num is not a palin" << endl;
case 1:
cout << "num is a palin" << endl;
the above isnt perfect; just psudocode.
we were to make two other functions, one that output a double when it was passed an int variable
and the other was to simply install a prototype
first
#include <cstdlib>
#include <iostream>
using namespace std;
double check (int a, double b, double c);
int main(int argc, char *argv[])
{
check(1, 2.2, 2.3);
system("PAUSE");
return EXIT_SUCCESS;
}
double check(int a, double b, double c)
{
cout << "please enter a number" << endl;
cin >> a;
cout << "please enter a decimal number" << endl;
cin >> b;
cout << "please enter another decimal number" << endl;
cin >> c;
}
first
#include <cstdlib>
#include <iostream>
using namespace std;
double check (int a, double b, double c);
int main(int argc, char *argv[])
{
check(1, 2.2, 2.3);
system("PAUSE");
return EXIT_SUCCESS;
}
double check(int a, double b, double c)
{
cout << "please enter a number" << endl;
cin >> a;
cout << "please enter a decimal number" << endl;
cin >> b;
cout << "please enter another decimal number" << endl;
cin >> c;
}
quick note, the above didnt have to output anything
second
double findabd ( int a, double b)
{
return (double)
}
not much to say on that one
No comments:
Post a Comment