There are more efficient ways to this, but these are simple examples of function
ID: 3554726 • Letter: T
Question
There are more efficient ways to this, but these are simple examples of functions.
Filename: ca6-2a.cpp
This is an program that calls a function that doubles a number and then adds 1 and returns the value back to main.
// Name: John Smith
// This program uses a function
#include <iostream>
using namespace std;
int db1(int );
int main()
{ int x, y;
cin>>x;
y = db1(x);
cout<< x<<" Doubled plus 1 is "<<y<<endl;
return 0;
}
int db1(int a)
{ int result;
result = a * 2;
result = result +1;
return( result );
}
Write a program that calls a function that triples a number and then adds 10 and returns the value back to main.
Filename: ca6-2b.cpp
This is a program that does the same thing as the last program, but it passes the value back as a reference paramater instead of returning the value
// Name: John Smith// This program uses a function
#include <iostream>
using namespace std;
void db1(int , int &);
int main()
{ int x, y;
cin>>x;
db1(x, y);
cout<< x<<" Doubled plus 1 is "<<y<<endl;
return 0;
}
void db1(int a, int &result)
{
result = a * 2;
result = result +1;
return ;
}
Write a program that calls a function that triples a number and then adds 10 and returns
the value back to main in the paramater list as a reference parameter.
Filename: ca6-2c.cpp
Write a program that reads in 3 numbers calls a function that doubles each of the 3
numbers.
This program does it for 2 numbers.
// Name: John Smith
// This program uses a function
#include <iostream>
using namespace std;
void db(int &, int &);
int main()
{ int x, y;
cin>>x>>y;
cout<<"The orginal numbers "<<x<<" "<<y<<endl;
db(x, y);
cout<<" Both numbers Doubled are "<<x<<" and "<<y<<endl;
return 0;
}
void db(int &a, int &b)
{
a = a * 2;
b = b * 2;
return ;
}
Explanation / Answer
/***part A***/
// Name: John Smith
// This program uses a function
#include <iostream>
using namespace std;
int db1(int );
int main()
{ int x, y;
cin>>x;
y = db1(x);
cout<< x<<" Trippled plus 10 is "<<y<<endl;
return 0;
}
int db1(int a)
{ int result;
result = a * 3;
result = result +10;
return( result );
}
/***partB**/
// Name: John Smith// This program uses a function
#include <iostream>
using namespace std;
void db1(int , int &);
int main()
{ int x, y;
cin>>x;
db1(x, y);
cout<< x<<" Trippled plus 10 is "<<y<<endl;
return 0;
}
void db1(int a, int &result)
{
result = a * 3;
result = result +10;
return ;
}
/**Part C*********/
// Name: John Smith
// This program uses a function
#include <iostream>
using namespace std;
void db(int &, int &,int &);
int main()
{ int x, y,z;
cin>>x>>y>>z;
cout<<"The orginal numbers "<<x<<" "<<y<<" "<<z<<endl;
db(x, y,z);
cout<<" All the numbers Doubled are "<<x<<" "<<y<<" "<<z<<endl;
return 0;
}
void db(int &a, int &b,int &c)
{
a = a * 2;
b = b * 2;
c=c*2;
return ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.