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
(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...
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Implement a function that returns the maximum number in a given unsorted linked list. For example,...
Implement a function that returns the maximum number in a given unsorted linked list. For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example. 1. Implement max.c with the completed printMax() function. 2. Provide an explanation for your solution #include <stdio.h> typedef struct node { int value; struct node *next; } node; int printMax(node *first) { // Your implementation return 0; } int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT