Write a recursive function named largestDigit that accepts an integer parame and returns the largest digit value that appears in that integer. Your function should work for both positive and negative numbers. If a number contains only a single di hat digit's value is by definition the largest.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
Write a recursive function named largestDigit that accepts an integer parameter
and returns the largest digit value that appears in that integer. Your function should
work for both positive and negative numbers. If a number contains only a single digit,
that digit's value is by definition the largest.
You must not use any loops; you also cannot use the string type or any library
functions. Your function must be recursive.
Complete the following file:
largestDigit.cpp
2 // You may use this helper function
3 int max(int a, int b) { return a < b ? b : a; }
4
5 int largestDigit(int n)
if((n<0) && (n/10
if(n<0) n =
0)) return n;
8
-n;
static int maxi
9
n%10;
0) return maxi;
maxi = max(maxi, n%10);
return largestDigit(n/10);
=
10
if((n/10)
11
12
13
14
15
16 }
Submit
Calling with Arguments
Name
Arguments Actual Expected
pass largestDigit 135725
7
7
pass largestDigit 111211
2
2
fail
largestDigit 100000
1
pass largestDigit 4530021
5
pass largestDigit 7639852
fail
largestDigit 76352
6
pass largestDigit 666555444
6
6
pass largestDigit 12424224
4
pass largestDigit 1242526
6
pass largestDigit o
670O
Transcribed Image Text:Write a recursive function named largestDigit that accepts an integer parameter and returns the largest digit value that appears in that integer. Your function should work for both positive and negative numbers. If a number contains only a single digit, that digit's value is by definition the largest. You must not use any loops; you also cannot use the string type or any library functions. Your function must be recursive. Complete the following file: largestDigit.cpp 2 // You may use this helper function 3 int max(int a, int b) { return a < b ? b : a; } 4 5 int largestDigit(int n) if((n<0) && (n/10 if(n<0) n = 0)) return n; 8 -n; static int maxi 9 n%10; 0) return maxi; maxi = max(maxi, n%10); return largestDigit(n/10); = 10 if((n/10) 11 12 13 14 15 16 } Submit Calling with Arguments Name Arguments Actual Expected pass largestDigit 135725 7 7 pass largestDigit 111211 2 2 fail largestDigit 100000 1 pass largestDigit 4530021 5 pass largestDigit 7639852 fail largestDigit 76352 6 pass largestDigit 666555444 6 6 pass largestDigit 12424224 4 pass largestDigit 1242526 6 pass largestDigit o 670O
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr