I am practicing about c++ using functions. but im stuck on the 1st question in the book about zeros.
can any1 help me so that i can study your codes. im a beginner in c++
tenx
the input must be
· No. 5 Zero! Are you there?
Enter 3 numbers: 10 20 10
No. of Zero/s: 3
Other numbers: 2
MENU: (User Defined Functions)
[A]dd
[M]ultiply
A[v]erage
E[x]it
Enter choice: A
The sum is 40
Please repeat the process? Y for yes, N for no
| |
|
Welcome to KnowledgeSutra - Dear Guest | |
Help On C++ Functions
Started by euclotan, Feb 24 2010 01:06 PM
2 replies to this topic
#2
Posted 26 February 2010 - 11:24 AM
#include <iostream>
#include <sstream>
using namespace std;
void tallyNums( int [] );
int tally[] = {0,0,0,0,0,0,0,0,0,0};
int main()
{
while ( true )
{
int num[3] = {0};
cout<<"Enter 3 Numbers: ";
cin>>num[0]>>num[1]>>num[2];
tallyNums( num );
cout<<"No. of Zero/s: "<<tally[0]<<endl;
int otherNums = 0;
for( size_t i = 1; i < 10; i++ )
{
otherNums += ( ( tally[i] >= 1 ) ? 1:0 );
}
cout<<"Other numbers: "<<otherNums<<endl;
cout<<"Menu \n [A]dd \n [M]ultiply \n A[v]erage \n E[x]it\n Enter choice:";
string choice;
double out = 0;
string op = "";
cin>>choice;
if( choice == "A" || choice == "a" )
{
out = num[0] + num[1] + num[2];
op = "sum";
}
else if( choice == "M" || choice == "m" )
{
out = num[0] * num[1] * num[2];
op = "product";
}
else if( choice == "V" || choice == "v" )
{
out = ( num[0] + num[1] + num[2] ) / 3.0;
op = "average";
}
else if( choice == "X" || choice == "x" )
{
break;
}
else
{
cout<<"error"<<endl;
}
cout<<"The "<<op<<" is "<<out<<endl;
}
return 0;
}
void tallyNums( int nums[] )
{
ostringstream oss;
oss<<nums[0]<<nums[1]<<nums[2];
string x = oss.str();
for( size_t i = 0; i < x.size(); i++ )
{
tally[x[i] - '0']++;
}
}
Reply to this topic

1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users













