Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method called numRemainderMatches which retruns an int, takes an array o

ID: 659656 • Letter: W

Question

Write a method called numRemainderMatches which retruns an int, takes an array of integers as its first parameter and a single positive digit (int) as its second parameter.  It looks at the last digit of each number in the array and compares it to the digit passed in as the second parameter.  It returns an int corresponding to the number of matches it found doing this comparison.  So, for example, if we passed in the array:  [29, 934, 3, 64, 12, 92, 7] and the digit: 4, it would compare the last digit of 29 (9) to 4, and those don't match, then the last digit of 934 (4) to 4, and those do match, then the last digit of 3 (3) to 4, and those don't match, then the last didfit of 64 (4) to 4, and those do match, then the last digit of 12 (2) to 4, and those don't match, then the last digit of 92 (2) to 4, and those don't match, and finally the last digit of 7 (7) to 4, and those don't match, so we found 2 matches to 4 (934 and 64), so we would return 2 if those were the array and digit passed in.  You code needs to work for any array of ints and any single positive digit int passed in.  The way to get the last digit of a number is by taking the remainder after dividing each element of the array by 10.  Use the % operator to get this remainder.

Explanation / Answer

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;

int numRemainderMatches(int arr[],int n, int d)
{
   int i = 0;
   int cnt = 0;
   while(i != n) {
       int p = arr[i]%10;
       if(p == d) {
           cnt++;
       }
       i++;
   }
   return cnt;
}
int main()
{
   int d;
   int n;
   cout << "Enter the size of array ";
   cin >> n;
   int arr[n];
   for(int i = 0;i < n;i++) {
       cin >> arr[i];
   }
   cout << "Enter the maching number ";
   cin >> d;
   cout << numRemainderMatches(arr,n,d) << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote