Question

• bool test(int a, int& b) { bool res = false; if(a > b) { b...

bool test(int a, int& b)
{
bool res = false;
if(a > b)
{
b = a%2;
res = true;
}
return res;
}

int main()
{
int x = 45, y = 43;
test(x, y);
cout << x << " " << y << endl;
return 0;
}


Question: The output of the above code is: ________________



•Write a function that takes an integer as its sole parameter and returns -1, 0 or 1 depending upon whether the integer is odd, zero or even respectively.
















•Write a function addFractions that takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. Note that this function should have a total of six parameters. Hint: a/b + c/d = (ad+bc)/bd.


















•Write out the contents of the array a after the following code executes:

int a[4];

a[0] = 4;

for(i = 1; i < 4; i++)
{
a[i] = 2*i+2;
if(i > 1)
a[i] = 2*a[i] – a[i-1];
}








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

1>

To the 'test' function we are passing one argument(x) as call by value and next argument(y) as call by reference. Which mean whatever changes done to y in 'test' will reflect in main function also; but for x it will not. Now in main function we pass 'test(45,43)'. Now a=45 and b =43. if(a>b) becomes true and b=a%2 is executed.45%2 is 1. Now a=45 and b=1 so finally when we print values of x and y, it will print 45 1.

2>

Write a function that takes an integer as its sole parameter and returns -1, 0 or 1 depending upon whether the integer is odd, zero or even respectively.

if(n%2==1)//if remider is 1 after dividing by 2 then number is odd. We use modulo operator
return -1;
else if(n==0)//We use simple equality operator for checking if the number is 0
return 0;
else//if the number is not odd or 0 then number is even
return 1;

3>

Write a function addFractions that takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. Note that this function should have a total of six parameters. Hint: a/b + c/d = (ad+bc)/bd.

#include<iostream>
using namespace std;
void test(int a,int b, int c,int d,int *x,int *y)//a,b,c,d are fractionals and x and y are the addresses of
{                                               // numerator and denominators are stored.
*x=a*d+b*c;
*y=b*d;
}

int main()
{
int *numerator,*denominator,nume,denom;
numerator=&nume;//The variable for storing numerator. the address will be passed to function
denominator=&denom;//The variable for storing denominator. the address will be passed to function
test(1,2,3,4,numerator,denominator);
cout<<nume<<endl<<denom;
}

4>

Write out the contents of the array a after the following code executes:

The output will be 4 4 8 8.

a[0]=4

The for loop starts

i=1

a[1]=2*1+2=4

if condition fails

i=2

a[2]=2*2+2=6

if condition true

a[2]=2*a[2] - a[1]=8

i=3

a[3]=2*3+2=8

if condtion true

a[3]=2*a[3] - a[2]=8

finally we have a[0]=4, a[1]=4, a[2]=8,a[3]=8

Add a comment
Know the answer?
Add Answer to:
• bool test(int a, int& b) { bool res = false; if(a > b) { b...
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
  • Here is a function that takes, and returns pointers: 2 int* select(bool which, int* a, int*...

    Here is a function that takes, and returns pointers: 2 int* select(bool which, int* a, int* b) { if(which == true) return a; else return b; } Using this function, suppose we have variables x, y: int x = ..., y = ...; int* p = select(x < y, &x, &y); if(p == &x) cout << "Yes"; else cout << "No"; What will this print if x = 5, y = 7? What will this print if x = 23, ...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • Haskell code: checkIfEven :: Int -> Bool x <- readLn let checkIfEven x = (even ((x*3)+1))...

    Haskell code: checkIfEven :: Int -> Bool x <- readLn let checkIfEven x = (even ((x*3)+1)) print checkIfEven    Getting error :  Variable not in scope: checkIfEven :: Int -> Bool, how to fix it? note: function goal is take an int and return a bool. takes the integer multiplies it by 3 and adds 1. if the the outcome is even return true, otherwise false.

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • QUESTION 1 What is the output of the following code snippet? int main() { bool attendance...

    QUESTION 1 What is the output of the following code snippet? int main() { bool attendance = false; string str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } cout << str << endl; return 0; } False True Unknown Maybe QUESTION 2 What is the output of the following code snippet? #include <iostream> #include <string> using...

  • #include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main()...

    #include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main() { int size = 10; int * A = newZeroArray(size); for(int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; }Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

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