Question

c++ Note: Do not use using namespace std; Use std:: Use comments for clear understanding TEST...

c++

Note:
Do not use using namespace std;

Use std::

Use comments for clear understanding

TEST THE PROGRAM.

1) Create a c++ program to run a simple experiment to see how reference parameters work.
2) You need to add a “&” symbol after the parameter type specification to set up a reference parameter:
int myfunc (int & x) {
x = 11;
return -11;
}

int testdata = 0;
int y;

y= myfunc (testdata);
3) Now, You can get a normal return value from our function. But a hidden side effect is possible as well. If myfunc modifies the parameter variable (x in this case), the caller’s testdata will be modified. It is important.

Things to do:
You are to build a project that has three files:
• main.cpp
• compare.cpp
• compare.h
The Compare Function
The “compare.cpp” file contains one function, also named “compare”. This function takes two integer reference parameters with names “param1” and “param2” and compares them. It returns a single character based on what it sees. Here is what you are to return:
• “=” if the two values are equal
• “<” if param1 is less than param2
• “>” if param1 is greater than param2
Just to prove that the “compare” function really does have access to your caller’s variables, have the function flip the sign of each parameter before you process the return statement you need in this function:
param1 = -param1;
param2 = -param2;
WarningThis will mess up your final output, but that is fine for what we are trying to see here!)
The Main Function
Set up the main program that asks the user for two integer numbers, which are stored in variables named “var1” and “var2”. The main program then calls the “compare” function described above, passing in the parameters “var1” and “var2”. Once you have the return character, you are to test that character and print out a more complete English statement about the result. For example, if you get back an “equal sign” character, you will print out this:
• 1234 is equal to 1234

Since you flipped the sign in the compare function, you will actually see negative values here, not the positive values the user entered).
Add output statement similar to the above for the other two possible return characters.

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

compare.h :

//header file for compare function file

#ifndef COMPARE_H
#define COMPARE_H
char compare(int &param1, int &param2);
#endif

compare.cpp :

#include "compare.h" //include the header file compare.h

char compare(int &param1,int &param2){ //definition of function using reference variable
    param1=-param1; //change the sign of variable param1
    param2=-param2; //change the sign of variable param1
  
    //return = or > or < depending on whether the variable are equal,1st is greater or the 1st id smaller
    if(param1==param2)
        return '=';
    else if (param1<param2)
        return'<';
    else
        return '>';
}

main.cpp :

#include <iostream>
#include "compare.h" //inclue the file


int main()
{
    int var1,var2;
    char sign;
    //take var1 value from user
    std::cout<<"Enter the first integer value: ";
    std::cin>>var1;
  
    //take var2 value from user
    std::cout<<"Enter the second integer value: ";
    std::cin>>var2;
  
    //call the compare function and store the returned value in sign variable
    sign=compare(var1,var2);
  
    //print statement depending on the sign value
    //The sign of variable var1 and var2 will be changed from the one entered by the user as they were modified in the compare function
    if(sign=='=')
        std::cout<<var1<<" is equal to "<<var2;
    else if(sign=='>')
        std::cout<<var1<<" is greater than "<<var2;
    else
        std::cout<<var1<<" is less than "<<var2;
    return 0;
}

Output:

Enter the first integer value: 34 er the second integer value: 12 34 is less than -12

You will observe that the sign of variable var1 and var2 are changed after they are paased to compare function as reference.

Add a comment
Know the answer?
Add Answer to:
c++ Note: Do not use using namespace std; Use std:: Use comments for clear understanding TEST...
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
  • C++. Use <iostream>, using namespace std; Please Allow a user to enter a set of one-word...

    C++. Use <iostream>, using namespace std; Please Allow a user to enter a set of one-word string and character pairs until they type "Done". For each pair, calculate how many of that character exists in that string and print out the resulting count. Write a user-defined function (not main) with two parameters. The first parameter is a one-word string, the second parameter is a character The function returns an integer specifying how many times that character is found in the...

  • C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std;...

    C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std; // Write your function here //////////////// STUDENT TESTING //////////////////// int run() { cout << "Student testing" << endl; strip(); return 0; } Here is the instruction: Write a filter function named strip that removes C++ comments from input, sending the uncommented portion of the program to output. Your program should work with both single line (//) comments, and multi-line (/* */) comments. + A...

  • #include<iostream> using namespace std; void twodigits(int original, int& first, int& second) {    first = original...

    #include<iostream> using namespace std; void twodigits(int original, int& first, int& second) {    first = original / 10;    second = original - first * 10; } int main(){ int original = 95; int first = 0; int second = 0; twodigits(original, first, second); cout << original << " => " << first << " and " << second << endl; return 0; } Write a function that takes two integers, numerator and denominator, as input parameters and output their...

  • Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int...

    Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }

  • #include #include #include using namespace std; int main() { // Declare variables here ifstream fin; string...

    #include #include #include using namespace std; int main() { // Declare variables here ifstream fin; string flowerName; string flowerGrow; // Open input file fin.open("flowers.dat"); fin >> flowerName; fin >> flowerGrow; while (!fin.eof()) { cout << flowerName << "grows in the" << flowerGrow << endl; // Write while loop that reads records from file. fin >> flowerName; fin >> flowerGrow; // Print flower } cout << flowerName << "grows in the" << flowerGrow<< endl; // Print flower name using the following...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return...

    C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return next character (in ASCII order) // which is a vowel, starting with parameter // If there are no more vowels, return null character ('\0') char nextVowel(char start) { // recursive call return nextVowel(start + 1); // base case if (start > 'z') return '\0'; // incorrect base case if ('a' == start || 'i' == start || 'o' == start || 'u' == start)...

  • #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string,...

    #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string, string &);       int main (){    string s;    cout << "Enter a string:" << endl;    get_user_string(&s);        string dash_version = convert_to_dash(&s)    if ( dash_version != 32){    &s.push_back('-');    } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

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