Beruflich Dokumente
Kultur Dokumente
#include <iostream>
using namespace std;
/* Function arguments are of different data type */
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
}
return 0;
return sum;
In the above program, we have created two functions "add" for two different data types you can
create more than two functions with same name according to requirement but making sure that
compiler will be able to determine which one to call. For example you can create add function
for integers, doubles and other data types in above program. In these functions you can see the
code of functions is same except data type, C++ provides a solution to this problem we can
create a single function for different data types which reduces code size which is via templates.
Output of program:
C programming
C programming
C++ programming
C++programming code
#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
}
return 0;
}
call();
one();
two();
three();
four();
void five();
void six();
void call();
int main()
{
//gotoxy(30,15);
cout<<"\n\n\n\n\t\tAuthor: Arpit Agrawal\n\t\tEmail:
arpitagrawal294@gmail.com\n\t\tDescription: Dice Roll Algorithm.\n\t\tProject Name: eRoll.\n\t\t" ;
cout<<"\n\n\t\tLoading. . . . . . . ";
Sleep(3000);
cout<<"\n\n\t\tPress r to roll or q to quit the game "<<endl;
char ch;
ch = getch();
xm:
if (ch=='r'){
system("cls");
call(); }
else
exit (0);
cout<<endl<<endl<<"Press r to roll again q to quit!";
ch = getch();
goto xm;
getch();
}
void call()
{
srand (time(NULL));
int n;
n= rand();
n = 1 + n % 6;
switch (n)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
default:
cout<<"NONUM";
}
void one()
{
cout << " -----" << endl;
cout << "|
|" << endl;
cout << "| O |" << endl;
cout << "|
|" << endl;
cout << " -----" << endl;
}
void two()
{
cout << " -----" << endl;
cout << "| O|" << endl;
cout << "|
|" << endl;
cout << "|O |" << endl;
cout << " -----" << endl;
}
void three()
{
cout << " -----" << endl;
cout << "| O|" << endl;
cout << "| O |" << endl;
cout << "|O |" << endl;
cout << " -----" << endl;
}
void four()
{
cout << " -----" << endl;
cout << "|O O|" << endl;
cout << "|
|" << endl;
cout << "|O O|" << endl;
cout << " -----" << endl;
}
void five()
{
cout << " -----" << endl;
cout << "|O O|" << endl;
cout << "| O |" << endl;
cout << "|O O|" << endl;
cout << " -----" << endl;
}
void six()
{
cout << " -----" << endl;
cout << "|O O|" << endl;
cout << "|O O|" << endl;
cout << "|O O|" << endl;
RubberDuck
Arpit Agrawal
21.5k236120
43115
add a
comme
nt
4 Answers
activeoldestvotes
up
vote13do
wn vote
accepted
This is because rand() returns a number from [0,RAND_MAX) or [0,32767) which is not
exactly divisible by 6. So you get:
1:
2:
3:
4:
5:
6:
1/5462
1/5461
1/5461
1/5461
1/5461
1/5461
Probably not an issue for a simple app but worth noting. The proper way to do this is:
int dieRoll() // 1-6 evenly distributed.
{
static int const max = RAND_MAX/6*6;
int r = rand();
while(r >= max) { r = rand();}
}
return r%6+1;
ch = getch();
goto xm;
std::endl is used to flush the output. If you just want a new line use "\n".
8down
vote
Function-naming
Your function names are absolutely useless. They should all be in verb form as they
perform an action, making the function's purpose clear. You have already done this
with call(), though the name itself is quite unhelpful since functions are called.
Based on what they're doing, call() can be renamed to something like runGame(), and
the rest of them can be renamed to something like displayDiceX() (replace X with the
respective number).
main() function
There is no separation between different lines of code (based on purpose), nor is there
any indentation in the conditional statements. Not only that, but you use goto, which has
a great potential of causing "spaghetti code" (not a good thing). Try to keep the control
flow as easy to follow as possible.
I can hardly follow the logic at a glance, but the one thing that makes sense are the
outputs, and even some of those are weird. It's not technically "loading" if it's sleeping
for a set period of time, although I can understand the aesthetic effect you're trying to
achieve. Also, do youreally need to output your email address? What does that have to
do with anything in this program?
Function-indentation
I also have no idea why you've indented these entire functions towards the right. You do
already have main() aligned correctly, so why not the rest? At a glance, I thought all of
this was part of main(), making me think that you were missing the function definitions.
You also haven't indented the code inside these "number" functions, though you have
done it correctly elsewhere. Try to keep things consistent.
Overall
Overall, this looks like C code and not at all C++. It's okay to keep it simple for learning
purposes, but this could use a lot of work to make it look like an ideal game
implementation.
If you want to take this a step further and really make it look more like C++, consider
defining your own classes, such as a Game and a Die class. The Game class will define
the game rules, while the Die class will represent one die and will allow you to create
dice (Die objects). The Game class will handle most of the work, and main() will just
need to have a Game object that can call a public interface function for running the
game. Nothing else would need to be done in main().
shareimprove this answer
Jamal
26.6k996197
up
vote5dow
n vote
add a comment
Your main() is pretty messy and very hard to read... I won't comment on the while loop
since someone else already did (and I agree with him), but stylistically to make your
code more readable, you want to have proper indentation:
int main()
{
//gotoxy(30,15);
cout<<"\n\n\n\n\t\tAuthor: Arpit Agrawal\n\t\tEmail:
arpitagrawal294@gmail.com\n\t\tDescription: Dice Roll Algorithm.\n\t\tProject
Name: e-Roll.\n\t\t" ;
cout<<"\n\n\t\tLoading. . . . . . . ";
Sleep(3000);
cout<<"\n\n\t\tPress r to roll or q to quit the game "<<endl;
char ch;
ch = getch();
xm:
if (ch=='r') {
system("cls");
call();
}
else
exit (0);
I'm not sure what your objectives are, but this is what your code is doing. The
last getch() will never get executed ever because of the goto xm statement and it really
is a mess. The 3 lines only get executed if ch=='r' so it would be more clear to have it in
that block.
Anyways, the best way to do what you want to do there is a loop - it is the clearest even
though it effectively does the same thing as your goto. With a goto, you have to analyze
the code to determine that you want looping behavior. When someone glances at a while,
they automatically know it will loop based on the condition inside the while.
shareimprove this answer
Jamal
PressingOnAlways
26.6k996197
34925
add a comment
up vote4down vote