Using Microsoft Visual Studio C++ from control structure through object 8th Prog
ID: 3712377 • Letter: U
Question
Using Microsoft Visual Studio
C++ from control structure through object 8th
Programming Assignment?:
a) Create a program with the main function and one additional function.
The additional function asks for input from the user and returns the output back to the main function for display.
b) Take your previous program and include overload functions. Display the results of those functions.
c) Continue with the previous program. Include a loop in the main function that call another function. The other function should utilize a "Static" variable that keep a running total.
Explanation / Answer
a)
int output(int k){
int i;
cout<<"Enter your input:";
cin>>i;
return k*i;
}
int main() {
int a=10;
cout<<output(a)<<" ::is the output returned form the integer function"<<endl;
cout<<endl;
}
sample output:
b)
#include <iostream>
using namespace std;
// function overloding is achieved for this output function here
// with integer type
int output(int k){
int i;
cout<<"Enter your input:";
cin>>i;
return k*i;
}
// and here float type
float output(float k){
float i;
cout<<"Enter your input(may be this time a float):";
cin>>i;
return k*i;
}
// calling both the function with different input parameters
//Here a is an interger variable
// b is a float variable
//complier calls the correspinding function
int main() {
int a=10;
float b=10.5;
cout<<output(a)<<" ::is the output returned form the integer function"<<endl;
cout<<endl;
cout<<output(b)<<" ::is the output returned form the float function"<<endl;
}
C)
#include <iostream>
using namespace std;
// function overloding is achieved for this output function here
// with integer type
int output(int k){
int i;
cout<<"Enter your input:";
cin>>i;
return k*i;
}
// and here float type
float output(float k){
float i;
cout<<"Enter your input(may be this time a float):";
cin>>i;
return k*i;
}
// a function with static variable defined inside it and it will increment the variable value each time the function is called
void otherfunction(){
static int loop_count=0;
cout<<loop_count<<" ";
loop_count++;
}
// calling both the function with different input parameters
//Here a is an interger variable
// b is a float variable
//complier calls the correspinding function
int main() {
int a=10;
float b=10.5;
cout<<output(a)<<" ::is the output returned form the integer function"<<endl;
cout<<endl;
cout<<output(b)<<" ::is the output returned form the float function"<<endl;
cout<<endl;
//c)calling the other function inside a loop
cout<<"loop started"<<endl;
for(int i=0;i<9;i++){
otherfunction();
}
cout<<"loop callling ends"<<endl;
}
A static variable life is till the program ends. And it will be initialized only once in the lifetime. That is the reason the variable loop_count will not go back to zero for each function call. If update done by previous function call will be saved in the variable.
Please comment below for any other clarification on the answer provided.
int output(int k){
int i;
cout<<"Enter your input:";
cin>>i;
return k*i;
}
int main() {
int a=10;
cout<<output(a)<<" ::is the output returned form the integer function"<<endl;
cout<<endl;
}
sample output:
gcc version 4.6.3 Enter your input: 5 50 ::is the output returned form the function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.