Question

In: Computer Science

IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background...

IN C# WITH SCREENSHOTS OF THE CODE

RECURSION
Objectives
• Learn the basics of recursion.
Background
There are many problems that loops simplify, such as displaying every pixel to a screen or
receiving repetitive input. However, some situations that can be simplified with looping are not
easily solvable using loops. This includes problems that require back tracking and being able to
use information from previous iterations, which would normally be lost when using an iterative
loop. In those cases, it is much easier to use recursion to logically solve the problem and it may
even reduce the amount of code that needs to be written.


Submission Guidelines:
You will turn in a Program code – 2 program files (one for each lab problem)

Tasks
This lab has two parts:

1. Write a recursive method CalculateExponent( ) that takes in two integer parameters, x
and y, and implements xy (x raised to the power y), where x and y are integers and y > 0.
Write a driver program that calls this method from the Main program.

2. Write a recursive method IntegerMultipy ( ) that takes in two integer parameters i, j, and
implements i * j (integer multiplication), where i > 0. Define the multiplication process in
terms of integer addition. For example, 4 * 7 is equal to 7 added to itself 4 times. Write a
driver program that calls this method from the Main program.

Sample Output:
LAB 1:
Please enter a value for x
4
Please enter a value for y
2
Page 2 of 2
16
// Your program should check if y is a positive, non-zero integer.
LAB 2:
Please enter a value for i
4
Please enter a value for j
7
28

Grading:
● 100 %: Attempted lab and submitted fully functioning lab exercises with complete
headers and clear I/O statements and
● 95 %: Attempted lab and submitted fully functioning lab exercises but incomplete
headers and/or unclear I/O statements before due date.
● 90%: All but one item are correct
● 80%: At least two more items are correct
● 70%: Program compiles and methods are correct
● 0%: Did not attempt lab or did not submit it before the due date

Solutions

Expert Solution

Lab1 program:

using System;
                                        
public class Program
{
        //recursive function to calculate Exponent values
        private static int CalculateExponent( int x, int y){
                if (y>0){
                        return x* CalculateExponent(x, y-1);
                }else
                        return 1;
        }
        //Driver code
        public static void Main()
        {
                Console.WriteLine("Please enter value of x:");
                int x,y;
                string input = Console.ReadLine();
                Int32.TryParse(input, out x);           
                Console.WriteLine("Please enter value of y:");
                input = Console.ReadLine();
                Int32.TryParse(input, out y);
                Console.Write("x^y=");
                Console.WriteLine(CalculateExponent(x,y));
        }
}

Lab 1 program output:

Lab 2 program:

using System;
                                        
public class Program
{
        //recursive function to calculate multiplication
        private static int IntegerMultiply(int i, int j){
                if(i>0 && j>0){
                        return i + IntegerMultiply(i,j-1);
                }
                else
                        return 0;
        }
        //Driver code
        public static void Main()
        {
                Console.WriteLine("Please enter value of i:");
                int i,j;
                string input = Console.ReadLine();
                Int32.TryParse(input, out i);           
                Console.WriteLine("Please enter value of j:");
                input = Console.ReadLine();
                Int32.TryParse(input, out j);
                Console.Write("i*j=");
                Console.WriteLine(IntegerMultiply(i,j));
        }
}

Lab 2 program output:


Related Solutions

PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: Write a driver program that calls this method from the Main program. • • Write a driver program that calls this method from the Main program. Note: Your program name should match the name of your java /...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
how to send background process to foreground process using c code?
how to send background process to foreground process using c code?
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to...
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to use sequential statements to build a basic program to implement the algorithm, using correct programming format Learn to use the scanf function to read and store values entered on the keyboard by the user, and the printf function used to display output results on the computer monitor Learn to convert math formulas into correct arithmetic expressions using variables, constants, and library math functions Learn...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives • Be able to write methods • Be able to call methods Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
Coding Project 2: UDP Pinger In this lab, you will learn the basics of socket programming...
Coding Project 2: UDP Pinger In this lab, you will learn the basics of socket programming for UDP in Python. You will learn how to send and receive datagram packets using UDP sockets and also, how to set a proper socket timeout. Throughout the lab, you will gain familiarity with a Ping application and its usefulness in computing statistics such as packet loss rate. You will first study a simple Internet ping server written in the Python, and implement a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT