Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

C# Recursion Tips

Example: This code will count from 1 to 10 using recursion

using System;class Program{ static void RecuriveFunction(int x) { if (x > 10) //Condition to stop recursion return; //Exit out of function else { Console.WriteLine(x); RecuriveFunction(x + 1); //Call myself } } static void Main() { RecuriveFunction(1); Console.Read(); }}

Output:

12345678910

VARIANTS

TASK1.

Execute the problem given in the following task.

1) Write a function max3() that takes three int values as arguments and returns the value of the largest one. Add an overloaded function that does the same thing with three double values.

2) Write a driver program which uses the function to output an m x n block of asterisks. m and n are entered by the user.

3) Write a program that asks a user to type the value of N and computes N!. Use function prototype.

4) Write a C++ program employing a function int equals(int,int) which checks whether two integer numbers are equal. In this case, the return value should be 1 otherwise 0.

5) Write a program for a function that takes two parameters of the float type and returns true (1) if the first parameter is greater than the second one and otherwise returns false (0).

6) Write a program MaxMin.cpp that reads in integers (as many as the user enters) and prints out the maximum and minimum values read in.

7) Write a program employing GeometricMean function that reads in positive real numbers and prints out their geometric mean. Find the geometric mean of N positive numbers x1, x2,..., xN is (x1 * x2 *... * xN)1/N.

8) Write a C++ program using a function prototype. Input data should be typed in from the keyboard.

9) Write a program employing Pnorm function that takes a command-line argument p, reads in real numbers, and prints out their p-norm. The p-norm norm of a vector (x1,..., xN) is defined to be the pth root of (|x1|p + |x2|p +... + |xN|p).

10) Write a program employing the HarmonicMean function that reads in positive real numbers and prints out their harmonic mean. Find the harmonic mean of N positive numbers x1, x2,..., xN is (1/x1 + 1/x2 +... + 1/xN) / (1 / N).

11) Write a program that calculates the mileage you travel on your car. The program should ask a user for the number of miles traveled and the number of gallons consumed. Use the function Mileage() to calculate the mileage. Mileage() should take two arguments (the number of miles traveled and the number of gallons consumed). The function should return the mileage and the program should then display it on the screen.

12) Write a program that computes the cost of a taxi ride. A taxi charges a fixed fee of $2.50 plus an additional charge of $5.50 per mile. The program should ask a user to enter the length of the trip in miles and use the function Taxi_Charge() to calculate the fare. Taxi_Charge() should have one argument, the length of the trip, and return the total fare for the ride. The program should display the total fare.

13) Write a program that asks user to input a grade that he or she received at an exam. The grade is an integer from 0 to 100 inclusive. The program should convert the numeric grade into the equivalent letter grade. Do the conversion by using a function Letter_Grade() that converts a numeric grade in the range 0100 to the equivalent letter grade. The function should have one parameter, (an integer grade). The return value of the function should be A if the grade is 90 to 100; B if the grade is 80 to 89; C if the grade is 70 to 79; D if the grade is 65 to 69; and F if the grade is 64 or lower. After converting the grade, the program should display the numeric grade and the equivalent letter grade.

14) – 16) Write a C++ program employing AREA function which will ask a user to enter coordinates and define whether they belong to the shaded area.

     

17) You have a,b,c numbers. Write a program employing a TRIANGLE function which will define whether you can build a triangle with a,b,c sides.

18) The factorial n! of a positive integer n is defined as

n! = 1*2*3... * (n-1) * n,

where 0! = 1. Write a function to calculate the factorial of a number.

19) Write a function power (double base, int exp) to calculate integral powers of floating-point numbers. The power x0 is defined as 1.0 for a given number x. The power is defined as for a negative exponent n. The power with n > 0 will always yield 0.0.

20) Write a program that repeatedly asks a user to enter pairs of numbers until at least one of the pair is 0. For each pair, the program should use a function to calculate the harmonic mean of the numbers. The function should return the answer to main(), which should report the result. The harmonic mean of the numbers is the inverse of the average of the inverses and can be calculated as follows:

harmonic mean = 2.0. x. y / (x + y)

21) The trigonometry functions (sin(), cos(), and tan()) in the standard <math.h> library take arguments in radians. Write three equivalent functions, called sind(), cosd(), and tand(), which take arguments in degrees. All arguments and return values should be the double type.

22) Write a program that reads in a number (an integer) and a name (less than 15 characters) from the keyboard. Data is entered by one function, and output by another. Keep the data in the main program. The program should end when zero is entered.

23) Write a family of overloaded functions called equal(), which take two arguments of the same type, returning 1 if the arguments are equal, and 0 otherwise. Provide versions for char, int and double functions.

24) Write a program that calculates an expression , where , the maximum number between x and y. Solve this problem by using function prototype.

25) Write a C++ program by using function prototype to identify whether the entered number is odd or even. Provide versions checking also if the entered number is a total square.

26) The date consists of three numbers: y (year), m (month) and d (day). Write a program that prints out the date of the previous and the next days.

27) Write a program that displays the following on the screen:

************************** * * * I can write functions!* * * **************************

To accomplish the task the program should use three functions. The function Stars() should display a line of 26 asterisks. The function Bar() should display an asterisk, 24 blanks, and an asterisk. The function Message() should display the middle line. None of the functions should return a value.

28) Write a program that draws a rectangle of Xs. Ask a user to enter dimensions of a rectangle, then use the function Rectangle() to draw a rectangle of the requested size consisting of Xs. Rectangle() should have two integer arguments (the length and width of the rectangle). The function should not return a value.

29) A manufacturer needs a program to display a table of costs for certain items of her product. She wants to input the production costs for one unit, the smallest and the largest number of products produced, each in hundreds. For example, she might input 3.46 for the unit cost, and 300 and 700 as the range of numbers. The resulting table should look as shown below.

Product Cost Table:

----------------------------------------------------

Number Produced Cost

----------------------------------------------------

----------------------------------------------------

300 1038.00

400 1384.00

500 1730.00

600 2076.00

700 2422.00

Use a function Heading() to display the table headings. Heading() should receive no arguments and return no value. Use another function, Table(), to produce the table. Table() should have three arguments (the unit cost, the smallest number produced, and the largest number produced) and table() should not return a value.

30) Write a program that asks a user to enter two decimal numbers, calculates and displays the product and quotient of them. Use a function Product() to calculate the product. The function should have two arguments which are the two numbers that the user inputs. The function should return the product of the numbers. Use a function Quotient() that has two arguments, which are the numbers entered by the user. The function should return the quotient of the first number divided by the second. If the second number is zero (recall that division by zero is not allowed), display an error message and exit the program.

31) Write a program that calculates expression , where , the maximum number between x and y. Solve this problem using function prototype.

 

TASK2.

Note: All input values must be entered from the keyboard by the user and a sensible input prompt and informative output should be included.

1) Write a program which will ask a user to enter rectangle with m and n sides and compute how many squares with side m can be placed in it.

2) Write a program that changes an integer number of centimetres into its equivalent in kilometres, metres and centimetres. For example, 164375 centimetres is 1 kilometre, 643 metres and 75 centimetres. Include declarations of suitable variables.

3) Write a program to read in characters of different type (char, int, float, double, bool, string) and to print them out, each one in a separate line and enclosed in single quotation marks.

4) Write a program that converts a sum of money given as an integer number of pence into a floating point value representing the equivalent number of pounds. For example, 365 pence would be 3.65 pounds.

5) Write a program which prompts the user to enter two integer values, a float value and two values of bool type and then prints out all five values that are entered with a suitable message.

6) Write a program which asks user to enter the needed values and calculates the expression for the following mathematical formula:

Y= .

7) Write a program to evaluate the fuel consumption of a car. The mileage at the start and end of the journey should be read in, as well as the fuel level in the tank at the start and end of the journey. Calculate fuel used, miles traveled, and hence the overall fuel consumption in miles traveled per gallon of fuel.

8) Write a C++ program which asks a user to enter the needed values and calculates the expression for the following mathematical formula:

Y=

9) Write a program to convert currency from pounds sterling into deutsch marks. Read the quantity of money in pounds and pence, and output the resulting foreign currency in marks and pfennigs. (There are 100 pfennigs in a mark). Use a const to represent the conversion rate, which is 2.31DM to £1 at the time of writing.

10) Write a program which will compute the area of a square () and a triangle ().

11) A customer's gas bill is calculated by adding a standard charge of 9.02 pence per day to the charge for gas, calculated as 1.433 pence per cubic meter. The whole bill is then liable for VAT at 8%. Write a program that reads in the number of days, and the initial and final meter readings, and calculates and prints the bill. Use const for the various rates of charging.

12) Write a program which will ask a user to enter a double figure and which will output:

– number of ten's place;– amount of digits in it;– summation of its digits.

Example: input: 123, output: 1) 12; 2) 3; 3) 6

13) Write a program which will calculate Surface area (S= 4 Pi2Rr) and Volume (V= 2 Pi2Rr2) of the Circular (Ring Torus).

14) Write a program which reads values of two floats and outputs their sum, product and quotient.

15) Write a program which will ask a user to enter three figures and output:– amount of digits in it– summation of its digits

– product of its digits

Example: input:123, output: 1) 3; 2) 6; 3) 6;

16) Write a program which will ask a user to enter three figures and output a number in which the first figure will be put after the third one. Example: input: 123, output: 231.

17) Write a program with a function which will ask a user to enter four figures and output a number in which figures will be transposed. Example: input: 1234, output: 2143 or input: 7086, output: 768.

18) Write a program which will ask a user to enter a natural number (n>9) and output an amount of ten's places.

Example: input: 123456789, output: 9.

19) Write a program that computes the hypotenuse length of four right triangles based on the lengths of the two legs. (Use the task 2 from the laboratory work 1).

20) Read in three integers of the data type int from the keyboard. Then add three numbers and divide received summation by the fourth number that was entered by the user.

21) Write a program that is capable of displaying the distances from the sun to four planets closest to it in centimeters and inches. (Use the task 8 from the laboratory work 1).

22) Write a program that prints out all six permutations of the three lines typed into the screen. Declare a named constant for each line and write an output statement for each permutation. (Use the task 10 from the laboratory work 1).

23) Write a program that prints out a business card. A card should include your name, street address, phone number(s), and email address. Also make up a company name and put it on the card. Print the complited card to the screen.

24) Write a program that takes an integer as the number of minutes and outputs the total hours and minutes.

25) Write a program to calculate the volume of a sphere . Print the result to the screen in the following manner:

The volume of a sphere
r V
3.5 1.07 ? ? ?

26) Write a program that converts Celsius into Fahrenheit. The formula is . Print the result to the screen in the following manner:

Celsius to Fahrenheit
Celsius Fahrenheit
-17.8 -10 ? ? ?

27) Write a program to print out the perimeter of a rectangle given its height and width. The formula is perimeter = 2 · (width + height). Print the result to the screen in the following manner:

The perimeter of a rectangle
Width Height Perimeter
3.04 2.9 7.6 3.4 ? ? ?

 

28) Write a program which asks a user to enter the needed values and calculates the expression for the following mathematical formula:

Y=

29) Write a program that does the following: given as input three integers representing a date as day, month, year, print out the number day, month and year for the following day's date.

Typical input: 28 2 1992

Typical output: Date following 28:02:1992 is 29:02:1992.

30) Write a program creating a table of Olympic running distances in meters, kilometers, yards, and miles. Use the pattern exhibited in these distances to write your program: 1m = 0.001 km = 1.094 yd = 0.0006215 mi. All distances are real numbers. Print the result to the screen in the following manner:

Table of Olympic running distances
Meters Kilometers Yards Miles
  ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____

 


Дата добавления: 2015-11-14; просмотров: 307 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
C# Global variables| Single-Dimensional Arrays

mybiblioteka.su - 2015-2024 год. (0.014 сек.)