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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education