#include <iostream> using namespace std; int sum(int a, int b=10, int c=20); int main(){ /* In this case a value is passed as * 1 and b and c values are taken from * default arguments. */ cout<<sum(1)<<endl; /* In this case a value is passed as * 1 and b value as 2, value of c values is * taken from default arguments. */ cout<<sum(1, 2)<<endl; /* In this case all the three values are * passed during function call, hence no * default arguments have been used. */ cout<<sum(1, 2, 3)<<endl; return 0; } int sum(int a, int b, int c){ int z; z = a+b+c; return z; }
Tuesday, 14 May 2019
Default Arguments in C++ Functions
sum of two values by using function
#include <iostream> using namespace std; /* This function adds two integer values * and returns the result */int sum(int num1, int num2){ int num3 = num1+num2; return num3; } int main(){ //Calling the function cout<<sum(1,99); return 0; }
EXAMPE 2:
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2) {
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Saturday, 11 May 2019
Recursivity function factorial of a number (n!)
Recursivity
Recursivity is the property that functions have to be called by themselves. It is useful for some tasks, such as sorting elements, or calculating the factorial of numbers. For example, in order to obtain the factorial of a number (n!) the mathematical formula would be:n! = n * (n-1) * (n-2) * (n-3) ... * 1 More concretely,
5! (factorial of 5) would be:5! = 5 * 4 * 3 * 2 * 1 = 120 And a recursive function to calculate this in C++ could be:
|
| 9! = 362880 |
Notice how in function factorial we included a call to itself, but only if the argument passed was greater than 1, since, otherwise, the function would perform an infinite recursive loop, in which once it arrived to 0, it would continue multiplying by all the negative numbers (probably provoking a stack overflow at some point during runtime).
Friday, 10 May 2019
test mach
#include <iostream>
using namespace std;
const int Inng = 2;
const int Player = 11;
int main()
{int inngtotal=0;
int score[Inng][Player];
cout << "Enter all score for a Player of first Inng and then second Inng. \n";
// Inserting the values into the score array
for (int i = 0; i < Inng; ++i)
{
for(int j = 0; j < Player; ++j)
{
cout << "Inng " << i + 1 << ", Player " << j + 1 << " : ";
cin >> score[i][j];
}
}
cout << "\n\nDisplaying Values:\n";
// Accessing the values from the score array
for (int i = 0; i < Inng; ++i)
{
inngtotal=0;
for(int j = 0; j < Player; ++j)
{
cout << "Inng " << i + 1 << ", Player " << j + 1 << " = " << score[i][j] << endl;
inngtotal=inngtotal+score[i][j];
}
cout << "\ninng total ="<< inngtotal;
}
return 0;
}
using namespace std;
const int Inng = 2;
const int Player = 11;
int main()
{int inngtotal=0;
int score[Inng][Player];
cout << "Enter all score for a Player of first Inng and then second Inng. \n";
// Inserting the values into the score array
for (int i = 0; i < Inng; ++i)
{
for(int j = 0; j < Player; ++j)
{
cout << "Inng " << i + 1 << ", Player " << j + 1 << " : ";
cin >> score[i][j];
}
}
cout << "\n\nDisplaying Values:\n";
// Accessing the values from the score array
for (int i = 0; i < Inng; ++i)
{
inngtotal=0;
for(int j = 0; j < Player; ++j)
{
cout << "Inng " << i + 1 << ", Player " << j + 1 << " = " << score[i][j] << endl;
inngtotal=inngtotal+score[i][j];
}
cout << "\ninng total ="<< inngtotal;
}
return 0;
}
function passing value
#include <iostream>
using namespace std;
int kkk(int a,int b);
int main()
{
int i,j;
i=kkk(99,80);
cout <<"\nvalue of i .................>"<<i;
cout << "\n value of iiiiiiiiiiiiii "<< i;
return 0;
}
int kkk(int a,int b)
{cout <<"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\n"<<a<<" value of b "<<b;
return a+b;
}
using namespace std;
int kkk(int a,int b);
int main()
{
int i,j;
i=kkk(99,80);
cout <<"\nvalue of i .................>"<<i;
cout << "\n value of iiiiiiiiiiiiii "<< i;
return 0;
}
int kkk(int a,int b)
{cout <<"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\n"<<a<<" value of b "<<b;
return a+b;
}
Thursday, 9 May 2019
while table program
#include <iostream>
using namespace std;
int main()
{
int i=1;
int t=7;
while(i<10)
{
cout<<t<<"*"<<i<<"="<<t*i<<endl;
i++;
}
return 0;
}
using namespace std;
int main()
{
int i=1;
int t=7;
while(i<10)
{
cout<<t<<"*"<<i<<"="<<t*i<<endl;
i++;
}
return 0;
}
while
Friday, 3 May 2019
temperature for a week of first city
#include <iostream>
using namespace std;
const int CITY = 2;
const int WEEK = 7;
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then second city. \n";
// Inserting the values into the temperature array
for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
cin >> temperature[i][j];
}
}
cout << "\n\nDisplaying Values:\n";
// Accessing the values from the temperature array
for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl;
}
}
return 0;
}
Subscribe to:
Posts (Atom)
Directory implementation
The selection of directory-allocation and directory-management algorithms significantly affects the efficiency, performance, and reliabil...
-
Recursivity Recursivity is the property that functions have to be called by themselves. It is useful for some tasks, such as sorting elem...
-
structure is a collection of variables of different data types under a single name. It is similar to a class in that, both holds a coll...