Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Help On C++ Functions


2 replies to this topic

#1 euclotan

    Newbie

  • Kontributors
  • Pip
  • 2 posts

Posted 24 February 2010 - 01:06 PM

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

#2 iikwalsemsiskweyrd

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 10 posts

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']++;
	}
}


#3 euclotan

    Newbie

  • Kontributors
  • Pip
  • 2 posts

Posted 01 March 2010 - 05:18 AM

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..




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users