Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the full C++ Code for a function that prompts the user to enter a characte

ID: 3603136 • Letter: W

Question

Write the full C++ Code for a function that prompts the user to enter a character, then stores the user's response in a char variable. The function should return the character entered by the user. (The function will not have any actual arguments passed to it.) Name the function getChar().

Write the full C++ code for a function that receives four integers. The function should calculate the average of the four integers, then return the result (which may contain a decimal place). Name the function calcAverage().

Write the full C++ code for a function that receives an integer passed to it. The function, named halveNumber)(), should divide the integer by 2, then return the result (which may contain a decimal place).

Explanation / Answer

Problem 1

Code

#include <iostream>

using namespace std;

char getChar(){

char input;

cout << "Enter a character:";

cin >> input;

return input;

}

int main() {

char ch = getChar();

cout << "getChar() returned " << ch;

}

Output

Enter a character:m

getChar() returned m

Enter a character:mn

getChar() returned m (Just the first character is considered)

Problem 2

Code

#include <iostream>

using namespace std;

float calcAverage(){

int int1,int2,int3,int4;

  

cout << "Enter four integers:";

cin >> int1;

cin >> int2;

cin >> int3;

cin >> int4;

  

return ((int1+int2+int3+int4)/4.0); // Used 4.0 for getting the decimal place, if any, in the average

}

int main() {

float avg = calcAverage();

cout << "calcAverage() returned " << avg;

}

Output

Enter four integers: 10 11 12 13

calcAverage() returned 11.5

Problem 3

Code

#include <iostream>

using namespace std;

float halveNumber(){

int int1;

  

cout << "Enter an integer:";

cin >> int1;

  

return (int1/2.0); // Used 2.0 for getting the decimal place, if any, in the halved number

}

int main() {

float half = halveNumber();

cout << "halveNumber() returned " << half;

}

Output

Enter an integer:11

halveNumber() returned 5.5

Note: Please let me know in case of any doubt, through the comments section.