Question

Rewrite the following C++ code fragments. Your modified code should obey the following rules: • No global variables • No pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

#include <iostream>
using namespace std;

// Recursive function to return fibonacci series
int fibonacci(int number)
{
// Checks if the parameter number is less than or equals to 1
// return the number
if (number <= 1)
return number;

else
// Otherwise recursively call the function with parameter number minus one
// added with parameter number minus two
return fibonacci(number - 1) + fibonacci(number - 2);
}// End of function

// main function definition
int main ()
{
// To store the number entered by the user
int number, no = 0;
// Accepts the number
cout<<"\n Enter number of terms: ";
cin>>number;

for (int c = 1; c <= number; c++)
// Calls the function to generate fibonacci number
// Returns the returned value
cout<<fibonacci(no++)<<" ";
return 0;
}// End of main function

Sample Output:

Enter number of terms: 7
0 1 1 2 3 5 8

2)

#include <iostream>
using namespace std;

// Recursive function to display maximum and minimum element from array
void minmaxFromArray(long _array[], short arrayLength, int index, long _min, long _max)
{
// Checks if parameter index is less than array length
if(index < arrayLength)
{
// Checks if parameter maximum number is less then the array current index position value
if(_max < _array[index])
// Assign array current index position value to parameter variable max
_max = _array[index];

// Checks if parameter maximum number is greater then the array current index position value
if(_min > _array[index])
// Assign array current index position value to parameter variable min
_min = _array[index];

// Recursively calls the function with current index plus one
// and updated max and min
minmaxFromArray(_array, arrayLength, index + 1, _min, _max);
}// End of if condition

// Otherwise end of the array
else
{
// Displays maximum and minimum
cout<<"\n Maximum = "<<_max<<" ";
cout<<"\n Minimum = "<<_min<<" ";
}// End of else
}// End of function

// main function definition
int main()
{
// Creates an array
long _array[] = {11, 49, 41, 62, -20, 90, 23};
// Calculates length
int len = sizeof(_array)/sizeof(_array[0]);
// Calls the function to display maximum and minimum from array
minmaxFromArray(_array, len, 0, _array[0], _array[0]);
return 0;
}// End of main function

Sample Output:

Maximum = 90
Minimum = -20

Add a comment
Know the answer?
Add Answer to:
Rewrite the following C++ code fragments. Your modified code should obey the following rules: • No...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • This java code won't run and I can't figure out the problem with it. Please help...

    This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci {    // Returns n-th Fibonacci number    static BigInteger fib(int n)    {        BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE;           for (int j=2 ; j<=n ; j++)        {            Fibo[j] = Fibo[j-1].add(Fibo[j-2]);        }    return (Fibo[n]); }...

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • Rewrite the C++ code below so the the following conditions are met: You may create any...

    Rewrite the C++ code below so the the following conditions are met: You may create any number of classes. You may edit the main() function. Remove the variable string type completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Get rid of every if and if else statement completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Hint: Use polymorphism. #include <iostream> #include <vector> #include <algorithm>...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

  • Write a MIPS assembly code that corresponds to the following C code. Note: use the stack...

    Write a MIPS assembly code that corresponds to the following C code. Note: use the stack to store all register values that you use in the procedures. int aver(int * array, int N){ int i, sum = 0; for ( i=0;i i<N; i++) sum += array[i]; return sum/N;} int Max( int * array, int N){ int i, Maximum = array[i]; for ( i = 1; i< N; i++) if ( array[i] > Maximum) Maximum = array[i]; return Maximum; } int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • Consider the following functions: int min(int x, int y) { return x < y ? x...

    Consider the following functions: int min(int x, int y) { return x < y ? x : y; } int max(int x, int y) { return x < y ? y : x; } void incr(int *xp, int v) { *xp + = v; } int square(int x) { return x*x; } Assume x equals 10 and y equals 100. The following code fragment calls these functions int low = min (x,y); int high = max (x,y); for (i low;...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT