Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 14.2, Problem 12STE
What is the output of the following program?
#include <iostream> using namespace std; int mystery(int n); //Precondition n > = 1. int main() { cout << mystery(3); return 0; } int mystery(int n) { if (n < = 1) return 1; else return ( mystery(n − 1) + n ); } |
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
John Here
Need correct java code for the following question
I have a running code of a dining philosophers problem but it has a logic problem where it loops right after it asks how many of the philosophers are hungry thus unable to calculate how exactly the philosophers are going to eat
below is the code:
#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <math.h>#include <string.h>
void one(void);void two(void);void exit();int tph, philname[20], howhung, hu[20], cho;//{}[]
int main(void){ int i, status[10],philname[10],howhung; printf("\n\nDINING PHILOSOPHER PROBLEM"); printf("\nEnter the total no. of philosophers: "); scanf("%d",&i); for(i=0;i<tph;i++) { philname[i] = (i+1); status[i]=1; } printf("How many are hungry : "); scanf("%d", &howhung); if(howhung==tph) { printf("\nAll are hungry..\nDead lock stage will occur"); printf("\nExiting.."); } else {…
1.The following is the C code that you need to implement for this lab:
uint8_ t f(uint8_tn)
return(n<2)?(n):(f(n-1)+f(n-2));
The main function can be assumed as follows:
int main()
uint8_ t x;
x=f(???);
return 0;
Obviously, " ? ? ? " is representing a value used to test the algorithm!
Is "f"recursive?
a.No answer text provided.
b.Yes
c.no
d.depends on whether it is in for main
2.First, make the code in the previous question an actual C program so that it can be compiled and it runs. Play with it so that you feel comfortable with the logic of the code.
Then implement the code in TTPASM. Note that you need to preserve the actual C code structure, this means you cannot it into a non-recursive subroutine. Furthermoreall conventions discussed in class regarding subroutines must be followed. The idea is that I should be able to substitute f with my own code, and main should work. Or, I can substitute main with my own, and f should work.
Attach the source code of your assembly…
Chapter 14 Solutions
Problem Solving with C++ (10th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Will all of a classs actions always be directly mentioned in the problem domain description?
Starting Out with C++ from Control Structures to Objects (9th Edition)
Why is it useful for a programmer to have some background in language design, even though he or she may never a...
Concepts Of Programming Languages
Describe a method that can be used to gather a piece of data such as the users age.
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Define each of the following terms: supertype subtype specialization entity cluster completeness constraint enh...
Modern Database Management
Use the following tables for your answers to questions 3.7 through 3.51 : PET_OWNER (OwnerID, OwnerLasst Name, ...
Database Concepts (8th Edition)
Assume the following variable definitions: int a = 5, b = 12; double x = 3.4, z = 9.1; What are the values of t...
Starting Out with C++: Early Objects (9th Edition)
Knowledge Booster
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
- Write a program called Super_Digit.java with a recursive function called superDigit. The function must return the calculated super digit as an integer. If 'x' has only 1 digit, then its super digit is 'x' Otherwise, the super digit of 'x' is equal to the super digit of the sum of the digits of 'x' Hint: use a combination of n%10 and n/10 The output should look like what is given below. Please give an editable code.arrow_forwardConsider the following function: int secret(int m, int n) { int temp = n; for (int i = 1; i < abs(m); i++) temp = temp + n; if (m < 0) temp = -temp; return temp; } What is the output of the following C++ statements? i.cout << secret(5, 4) << endl; ii.cout << secret(-3, 20) << endl;arrow_forwardConsider the following recursive function:void recFun(int u){if (u == 0)cout << "Zero! ";else{cout << "Negative ";recFun(u + 1);}}What is the output, if any, of the following statements? a. recFun(8); b. recFun(0); c. recFun(-2);arrow_forward
- int const MULTIPLIER = 5; is a valid way to declare a constant integer variable. Group of answer choices True False ------- Given the following code segment, what is output to the screen? int num1 = 6; int num2 = 4 * num1++; cout << "num1=" << num1 << " num2=" << num2; Group of answer choices num1=7 num2=24 num1=6 num2=24 num1=6 num2=28 Nothing, because the code does not compile.arrow_forwardIn Java: Develop a void function that takes two integers and prints whether the first number is divisible by the second. For example, with arguments 51 and 17, the function should print “51 is divisible by 17”. With arguments 17 and 8, the function should print “17 is not divisible by 8”. Call the function twice with different arguments to show each possible result. What happens when the second argument is greater than the first?arrow_forwardConsider the following recursive function:void recEx11(int x){if (x > 0)if (x % 2 == 0){recEx11((x - 3) / 2);cout << x << " ";}else{recEx11((x - 4) / 2);cout << x << " ";}}What is the output of the following statements? a. recEx11(28); b. recEx11(148); c. recEx11(98); d. recEx11(-30)arrow_forward
- Here thr____Calculator.java, to accept an expression as astring in which the operands and operator are separated by zero or more spaces.For example, 3+4 and 3 + 4 are acceptable expressions. Here is a sample run:++arrow_forwardIN Java: Complete main() to read dates from input, one date per line. Each date's format must be as follows: March 1, 1990. Any date not following that format is incorrect and should be ignored. Use the substring() method to parse the string and extract the date. The input ends with -1 on a line alone. Output each correct date as: 3-1-1990. Ex: If the input is: March 1, 1990 April 2 1995 7/15/20 December 13, 2003 -1 then the output is: 3-1-1990 12-13-2003 Use the provided getMonthAsInt() method to convert a month string to an integer. If the month string is valid, an integer in the range 1 to 12 inclusive is returned, otherwise 0 is returned. Ex: getMonthAsInt("February") returns 2 and getMonthAsInt("7/15/20") returns 0. provided code: import java.util.Scanner; public class DateParser { public static int getMonthAsInt(String monthString) { int monthInt; // Java switch/case statement switch…arrow_forwardIn JAVA A function foo takes three integers as input arguments, i.e., foo(int a, int b, int c). The three inputs represent the three sides of a triangle in centimeter. The function is expected to return the type of a triangle: “equilateral”, “isosceles”, or “scalene”. Assume the domains of the three variables are 1 ≤ a ≤ 100, 50 ≤ b ≤ 150 and 100 ≤ c ≤ 200, respectively. A test case is in a tuple format <a, b, c, expected_output> with test inputs and the expected output. S1 is a set of test cases for the “Boundary Value Analysis” approach. S1 = S2 is a set of test cases for the “Robustness testing” approach. S2 – S1 = S3 is a set of tests cases for the “Robust Worst-Case testing” approach. Are there any types of triangles that S3 cannot reveal? If yes, what are they? If no, why?arrow_forward
- Identify all private, shared variables in the following code snippet. What is the output of the program (e.g. the value of IS)? int A[100]; int count; void work(int index[]) { float B[10]; …… } void main(){ int IS = 10; int i=0; #pragma omp parallel private(i) { int index[10]; work(index); #pragma omp for reduction(+:IS) for(i=0;i<100;i++) { IS = IS + i; } } printf(“%i\n”, IS); }arrow_forwardWhat is the output of the following code segments (when run independently)? int [ ] myAry = {10,20,30,40,50}; for (int x=0; x<=myAry.length-1; x++ ) System.out.print(" " + myAry[x] ); note : a java codearrow_forwardConsider the following C function or Java method (except for final printstatement the code is the same):void fun (int x) {int a, b, c, d;d = 1;for (a = 1 ; a <= x ; a ++) {b = a;c = 1;while (b > 0) {c &= b % 2;b /= 2;}d += c;}printf ("%d\n", d);}void fun (int x) {int a, b, c, d;d = 1;for (a = 1 ; a <= x ; a ++) {b = a;c = 1;while (b > 0) {c &= b % 2;b /= 2;}d += c;}System.out.println (d);} (a) What output will be printed for input value x = 8 ?(b) How many dierent input values x cause the function to print theresult 8 ?(c) Write a function with the same behavior but only one loop (of anytype) and no library call except for the print statement.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Program to find HCF & LCM of two numbers in C | #6 Coding Bytes; Author: FACE Prep;https://www.youtube.com/watch?v=mZA3cdalYN4;License: Standard YouTube License, CC-BY