Question

6. (Short answer) The C++ code below is very close to working, but, as written, wont compile. When trying to compile, the co

0 0
Add a comment Improve this question Transcribed image text
Answer #1

hello,

let us answer each of the question together.

a) the code to fix all errors will be " using namespace std; " written just after the line " #include<iostream> ".

Note:- the code (correct code) is at last of the solution where changes had been made correctly)

c++ defines all the standard function like "cout" , "cin" ,"endl" in standard library namespace , in fact "std" is the short form of " Standard ", which is a library in c++.

b) The program has Two function and they are:-

  1. The " main() " function. It is the function from where the execution of the code begins. The c++ compiler executes only main function of the code which calls all the others function attached with the main function. the program just takes user input on two "integer" type declared variables i.e., "num1" and "num2", and it send these two variable along with the third variable "max" as a parameter to the function "FindMax()".
  2. The second function is "FindMax()" , which takes 3 parameter , two of them being the value of two input variable entered by user in main function the the third is simply the address of the third variable of main function i.e., variable "max". The function checks the greater between two numbers and stores the greater number in the address of variable "max" obtained from the main function as the parameter for the FindMax function.

The code as a whole finds the greatest number of two (the two numbers are user input value).

c) The function is "FindMax()" , which takes 3 parameter , two of them being the value of two input variable entered by user in main function the the third is simply the address of the third variable of main function i.e., variable "max". The function computes the greatest value of two by simply comparing them in if-else statement. the function stores the greatest value at the memory address stored in reference variable max inside the FindMax function iteself.

The 2 of 3 parameters are value parameters but the thirs parameter i.e., "int& max" is reference parameter.

d) Yes, the Find max function will work correctly if a and b are equal , because if they are equal , let they be a=5 and b=5 i.e., both the values are equal, then if we compare the values , the greatest value is 5 which is same i.e., either a or b . So the function is Correct.

just look at the output also:-

Enter the first integer Ennter the second integer The max is 5

The correct and Complete program is here:-

code starts here:-

#include<iostream>
using namespace std;
void FindMax(int a,int b,int& max)
{
   if(a>b){
   max=a;
   }
   else
   {
   max=b;
   }
}
int main()
{
int num1=0;
int num2=0;
int max;
cout<<"Enter the first integer"<<endl;
cin>>num1;
cout<<"Ennter the second integer"<<endl;
cin>>num2;
FindMax(num1 ,num2 ,max);
cout<<"The max is "<<max<<endl;
return 0;

}

code ends here:-

Looking for your appreciation in the form of Thumbs Up.

i hope i was able to solve your problem to a greater extent, please feel free to comment your queries, Please consider my efforts and upvote my solution.

Thanku:)

Add a comment
Know the answer?
Add Answer to:
6. (Short answer) The C++ code below is very close to working, but, as written, won't...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • So far your TestEqual functions have tested values but haven't made any changes... Q: If you...

    So far your TestEqual functions have tested values but haven't made any changes... Q: If you changed one of the variable values in your function, would it change the value of the variable in main? Why? Q: What are the two options for passing parameters into a function? (by _________ and by __________) What's the difference? Write a new function, MakeEqual, that takes two integers and if they are not equal (tested using your TestEqual function), displays a message and...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

    I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b + c/d = (ad + bc)/bd a/b – c/d =...

  • Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code....

    Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT