Question

In: Computer Science

Take a piece of paper and fold it in half. Unfold it and there is one fold, which I'll signify with a "v". If you fold the paper twice

Use C# 

RECURSION - PAPERFOLDS

Concept Summary:

1. Recursion

For this assignment1 you will design a recursive method and the main program that calls the recursive method.

Description

Take a piece of paper and fold it in half. Unfold it and there is one fold, which I'll signify with a "v". If you fold the paper twice (always keeping the folds in the same direction and the creases parallel), it will have three folds when you unfold it, and they will be in a "^ v v" pattern. Fold a piece of paper three times, and you will see "^ ^ v v ^ v v" as the sequence of folds.
Folding a piece of already folded paper sounds a lot like recursion, and indeed it is. In case you can't see the algorithm, here it is:

  • If you fold a paper once, the fold pattern is "v".

  • For additional folds, it is the pattern you have only reversed and flipped over, then a

    "v" fold, and then the pattern you already have tacked on the end.

    Submission Guidelines:

    Turn in 1 program file that calls the recursive method from the Main program.

    Assignment:

  • Write a static method, paperFold, which is a recursive routine that returns a string representing the fold pattern for a paper folded n times. The driver program that calls the paperFold () method

  • Write one or more helper methods to generate a string that takes a fold sequence and returns it reversed and flipped over such that a "v ^ ^ ^" becomes a "v v v ^".
    Hint: You could write one to reverse the string and one to flip the string.

As in many recursive solutions, expect the fold method to be extremely simple (and of course recursive).

Skeleton Code

C# Version:

public static void Main (string[] args) { for(int i=1;i<5;i++)
{

string fold_string=paperfold(i);

Console.WriteLine("For "+i+" folds we get: "+fold_string+"\n"); }

}

Sample Output:

For 1 folds we get: v
For 2 folds we get: ^vv
For 3 folds we get: ^^vv^vv
For 4 folds we get: ^^v^^vvv^^vv^vv

Solutions

Expert Solution

Solution :

Here's the code in C # :-

using System;
class Fold
{
public static void Main() //Main function
{
Console.WriteLine("Enter n");
int n = Convert.ToInt32(Console.ReadLine());
for(int i =1;i<n;i++)
Console.WriteLine("For " +i+ " folds, we get : "+paperFold(i));
}

public static string reverse(string s) // Helper function to reverse string
{
int n = s.Length;
string ret = "";
for (int i = 1; i <= n;i++)
{
ret = ret + s[n-i];
}
return ret;
}

public static string flip(string s) // Helper function to flip string
{
string ret ="";
for (int i = 0; i<s.Length;i++)
{
if (s[i].Equals('v'))
ret = ret + '^';
else
ret = ret + 'v';
}
return ret;
}

public static string paperFold(int i) //Constructing paperfold recursively
{
if (i==1)
return "v";
string ret = flip(reverse(paperFold(i-1))) + "v" + paperFold(i-1);
return ret;
}
}
Since the question mentions a purely recursive approach, I haven't used memoization at all. A way to make this program run faster would be to store the output of paperFold(i) in an array so that we do not have to reconstruct it when it is called once again.

 


Related Solutions

A typical thickness for a sheet of paper is 0.004 inches. If you fold a sheet...
A typical thickness for a sheet of paper is 0.004 inches. If you fold a sheet of paper once, the thickness of the folded paper will double to a value of 0.008 inches. A second fold will result in a folded thickness of 0.016 inches. Create a spreadsheet that shows the number of folds from 0 to 50 and the resulting thickness of each fold. Calculate the resulting thickness in units of inches, feet, and miles.
One car has twice the mass of a second car, but only half as much kinetic...
One car has twice the mass of a second car, but only half as much kinetic energy. When both cars increase their speed by 7 m/s, they then have the same energy. What were the original speeds of the two cars?
One car has twice the mass of a second car, but only half as much kinetic...
One car has twice the mass of a second car, but only half as much kinetic energy. When both cars increase their speed by 9.0 m/s , they then have the same kinetic energy. a)What were the original speeds of the two cars?
Test 1: Blind spot determination Take a piece of paper or index card and make an...
Test 1: Blind spot determination Take a piece of paper or index card and make an “X” and a large dot about 3 inches apart. Hold it at arm’s length from your face (the X should be on the left, the ● on the right) Close your left eye. Focus your right eye on the “X” Slowly move the card toward your eye until the ● disappears, at this point stop moving the card and hold steady. Measure the distance...
Choose one of these factors to explore. Your research paper should answer the two-fold question: Decreased...
Choose one of these factors to explore. Your research paper should answer the two-fold question: Decreased use of tobacco and drugs Regular physical activity Balanced nutritional intake Positive mental health Safe sexual activity Provision of preventative services Access to culturally appropriate health services Community participation in the planning and delivery of health services Participation in civic activities and social engagement Strong social networks Feeling of trust The feeling of power and control over life decisions Supportive family structure Positive self-esteem...
If you take a boat from earth and put it on a planet with twice earth’s...
If you take a boat from earth and put it on a planet with twice earth’s gravity, what would happen if you tried to float it in water? Hint: Think about the buoyancy force formula. (5 points) 2. You drop a cube of mass 1 kg into a cup of water. It sinks with an acceleration of 2 m/s^2. a. What is the buoyancy force of the water? (4 points) b. What is the density of the water if the...
For each of the following ions, draw diagrams (on a piece of paper), like the one...
For each of the following ions, draw diagrams (on a piece of paper), like the one to the right below, to show orbital occupancies for both weak and strong octahedral fields. Indicate (on the table below) the total number of unpaired electrons in each case. Ion # Unpaired Weak field # Unpaired Strong field    (a) Mo3+ (b) Ni4+ (c) Cd2+ (d) Cr4+ (e) Co2+
write a short one-half to one page response paper describing what you learned about computational science...
write a short one-half to one page response paper describing what you learned about computational science and anything interesting about it
please don't skip any step/part or give me half answer I'll rate You are working for...
please don't skip any step/part or give me half answer I'll rate You are working for The Wellington Company on temporary assignment while one of the accountants is on family leave. You have been asked to review the company’s investment journal entries and provide necessary information to the accountant preparing the financial statements. PAGE 8 JOURNAL ACCOUNTING EQUATION DATE DESCRIPTION POST. REF. DEBIT CREDIT ASSETS LIABILITIES EQUITY 1 Jan. 17 Investments-Red Rock Co. Stock 38,500.00 ? 2 Cash 38,500.00 ?...
Below you are given the description of two quadrilaterals. On a piece of paper (labelled and...
Below you are given the description of two quadrilaterals. On a piece of paper (labelled and in order), use the appropriate tools (straightedge, compass, etc.) to construct the described quadrilaterals and the perpendicular bisectors of each of their sides. After each construction, state why there is no circle that will circumscribe the quadrilateral. 1. a parallelogram that is not a rectangle 2. a non-isoceles trapezoid. There is no diagram.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT