Jump to content
xisto Community
Sign in to follow this  
euclotan

Help On C++ Functions

Recommended Posts

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

Share this post


Link to post
Share on other sites
#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']++;	}}

Share this post


Link to post
Share on other sites

Thank you for your reply sir..but your code is too advance for me. i am stuck with the C++ functions. and I cant proceed to higher c++ if i'm confuse with functions... i just know the stdio.h, conio.h, stdlib.h, math.h.BTW: thank you for the reply sir..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.