Question

In: Computer Science

C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a...

C++

Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a boolean value to find if a given account number appears in an array of Account objects.

Solutions

Expert Solution

Explanation:

Here is the function is_present which takes the array of objects named arr, size of the array and also the account number to be searched inside the array.

using a for loop, the account number is searched, it returns true if it is found.

Else false if it is not found till the last element.

Function required:

bool is_present(Account arr[], int size, string acc_num_to_find)
{
for (int i = 0; i < size; i++) {
  
if(arr[i].getAcctNum()== acc_num_to_find)
return true;
}
  
return false;
}

Sample code to check the function:

#include <iostream>

using namespace std;

class Account
{
private:
  
string accNum;
string name;
double balance;
double interestRate;

public:
  
Account(string a, string n, double b, double i)
{
accNum = a;
name = a;
balance = b;
interestRate = i;
}

string getAcctNum()
{
return accNum;
}
  
  
};

bool is_present(Account arr[], int size, string acc_num_to_find)
{
for (int i = 0; i < size; i++) {
  
if(arr[i].getAcctNum()== acc_num_to_find)
return true;
}
  
return false;
}


int main()
{
Account ob("123", "John", 0, 0);
Account ob2("234", "Smith", 1, 1);
  
Account arr[] = {ob, ob2};
  
  
cout<<is_present(arr, 2, "234");

return 0;
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write a c++ function named multi_table to print out multiplication table for a given number of...
Write a c++ function named multi_table to print out multiplication table for a given number of rows and columns. Your program should print a header row and a header column to represent each row and column number. For example your function call multi_table(4, 4) would print below to the command line: 1 | 1 2 3 4 _________ 2 | 2 4 6 8 3 | 3 6 9 12 4 | 4 8 12 16 Test your function in...
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
In C++, write a function that returns the average of all the values stored in an...
In C++, write a function that returns the average of all the values stored in an integer array (called arr) of size n. The return type of the function should be double. Test this function in the main program.
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
Write a function named timesOfLetter that reads an array and returns the number of times of...
Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT