Перейти к основному содержанию
EDU-MMCS
Вы используете гостевой доступ (Вход)

cs101 ОП / Basics of programming ENG

  1. В начало
  2. Курсы
  3. Осенний семестр
  4. Фундаментальная информатика и ИТ
  5. cs101 ENG
  6. 1 topic. Basic constructions and loops
  7. Lesson #13. Procedures

Lesson #13. Procedures

Требуемые условия завершения
Открыто: Wednesday, 4 December 2019, 00:00
Срок сдачи: Tuesday, 31 December 2019, 00:00

Lections

  • Procedure is a piece of a code that performs a specific task.
  • Procedure consists of header (name and parameters) and body.
  • Procedure must be defined only once and can be run many times in the main program.
  • Procedure must be defined before the begin keyword of the main program.
  • To run a code of the procedure one has just to call the procedure in the main program.


Task. Let's write a procedure to calculate an average of two integer values (a & b) and to print the result:

Procedure definition before the program:


To run this procedure we have to call it and to pass two values in the main program:


****************************************************************************************

Example:

Task: Output a sequence of numbers from 1 to N (1 2 3 4 .... N). Use the procedure.

Solution:

  procedure PrintN(n: integer);
  begin
  for var i:=1 to n do
      Print(i)
  end;
  begin
  println('please, input N');
  var n:=readinteger;
  PrintN(N)
  end.

Result:

please, input N 
5
1 2 3 4 5 
******************************************************************************

✎

1. {0.3 points}[L13-task-01.pas] Define the procedure:  IncN(N), which calculates N + 1 and prints the result (N is input parameter).

2. {0.3 points}[L13-task-02.pas] Define the procedure:  PlusN(N), which calculates N + N and prints the result (N is input parameter).

3. {0.4 points}[L13-task-03.pas] Define the procedure:  APlusB(A,B), which calculates A + B and prints the result (A and B are input parameters).

4.{0.5 points}[L13-task-04.pas] Define the procedure: PrintSeq(A,B) (A<B), which prints the sequence of numbers from A to B (A and B are input parameters).

please, enter two numbers:
2  8
2 3 4 5 6 7 8

5. {0.6 points}[L13-task-05.pas] Define the procedure:  PrintSeq(A,B,C) (A<B), which prints the sequence of numbers from A to B with a step C (A, B and C are input parameters).

please, enter two numbers and a step:
2  17  3
2 5 8 11 14 17

6. {0.6 points}[L13-task-06.pas] Define the procedure:  PrintNumber(N,B) , which prints some entered number ( B Ntimes ( B and N are input parameters).

please, enter two numbers:
5  3  
result: 3  3  3  3  3

*****************************************************************************************

Input and Otput parameters

Task. Let's write a procedure to calculate an average of two integer values (a & b); the procedure has to pass the result to the main program:



To run the procedure you have to call it in the main program: 


**************************************************************************************

Formal & Actual parameters

Formal parameters - are the variables declared in a procedure definition.

Actual parameters - are the values passed to the procedure while calling it in the main program.


************************************************************

One more exaple:


***************************************************************************************************
 

✎

7. {0.5 points}[L13-task-07.pas] Define the procedure:  PlusN(N, result), which calculates N + N and passes the result in the result parameter (N is input parameter, result is output parameter).

8. {0.5 points}[L13-task-08.pas] Define the procedure:  APlusB(A, B, result), which calculates A + B and passes the result to the program in the parameter result (A and B are input parameters, result is output parameter).

9. {0.5 points}[L13-task-09.pas] Define the procedure:  Power2N(N, power), which squares the number N (N*N) and passes the result in the parameter power (N is input parameter, power is an output parameter).

10. {0.5 points}[L13-task-10.pas] Define the procedure:  FindMin(a,b, min), which finds the minimum between two numbers a and b and passes the result in the parameter min (a b are input parameters, min is an output parameter).

Please, input a and b:
2  15
>>> minimum is 2

11. {1.5 points}[L13-task-11.pas] Define the procedure:  Mean(X, Y, AMean, GMean), which calculates:

  • the average of two real numbers X & Y:


  • and the proportional average (geometric mean) of two real numbers X & Y:


X и Y are input parameters; while AMean and GMean are output parameters of real type.

In the main program:  For given  A, B, C, D find the average and the proportional average (geometric mean) of three pairs (A, B), (A, C), (A, D). Use a procedure.

Please, input A, B, C, D:
2  4  3  6
>>> A, B:  AMean = 3    GMean = 2.828
>>> A, C:  AMean = 2.5    GMean = 2.449
>>> A, D:  AMean = 4    GMean = 3.464

12. {1.5 points}[L13-task-12.pas] Define the procedure:  AddLeftDigit(D, K, res), which appends digit K (0<=K<=9) to the positive integer D (0<=D<=9) (D , K are input parameters, res is output parameter). Pass the result to the main program in the res parameter:

Please, input  D  (0<=D<=9) and K (0<=K<=9):
2  4  
>>> 24

13. {0.7 points}[L13-task-13.pas] Define the procedure:  FindSum(N, sum), which finds the addition (sum) of N numbers. Result is passed to the main program in the parameter sum (N is an input parameter, sum is an output parameter).

Please, input how many numbers will be (N):
5
input numbers:
7 3 2 8 12
>>> sum is 32

14. {0.7 points}[L13-task-14.pas] Define the procedure:  FindAverage(N, aver), which finds the average among N numbers. Result is passed to the main program in the parameter aver.

Please, input how many numbers will be(N):
5
input numbers:
7 3 2 8 15
>>> average is 7

**********************************************************************************

Extra tasks:

1. {1.5 points}[L13-task-01-extra.pas] Define the procedure:  AddLeftDigit(D, K, res), which appends digit K (0<=K<=9) to the positive integer D (D, K are input parameters, res is output parameter). Pass the result to the main program in the res parameter:

Please, input  D and K (0<=K<=9):
20  4  
>>> 204 

342 7
>>> 3427

2. {1.5 points}[L13-task-02-extra.pas] Define the procedure:  AddLeftDigits(D, K1,K2, res1, res2), which first appends digit K1 (0<=K1<=9) to the positive integer D and passes the result in res1, and afterwards appends digit K2 to it and passes the result in res2:

Please, input  D,  K1 and K2 (0<=K<=9):
20   4   5  
>>> 204   2045 

342  7  1
>>> 3427   34271

3. {1.5 points}[L13-task-03-extra.pas] Define the procedure:  FibN(N, result), which passes to the main program the N-th number of Fibonacci sequence. Fibonacci sequence: 1,1,2,3,5,8,13,21,34,55,… (F1=1, F2=1, Fk = Fk−2 + Fk−1, k = 3,4,…).

6  
>>> 8

9
>>> 34

Short procedure definition

If the body of a procedure consists only of one statement we can use short procedure definition:


◄ Lesson #12. While & Repeat Loops: digits of a number
Lesson #14. Functions ►
Пропустить Навигация
Навигация
  • В начало

    • Страницы сайта

      • Мои курсы

      • Теги

    • Мои курсы

    • Курсы

      • Осенний семестр

        • Прикладная математика и информатика

        • Фундаментальная информатика и ИТ

          • Compiler Development

          • КПР

          • АЗПК

          • Frontend

          • ТеорЯП

          • Ruby Eng

          • EngCA&OS

          • CS201e

          • Компиляторы - лекции

          • CS202

          • CS211 C++ ENG

          • cs101 ENG

            • Общее

            • 1 topic. Basic constructions and loops

              • ЗаданиеLesson #1. Introduction to PascalABC.NET

              • ЗаданиеLesson #2. Integers, div and mod operators

              • ЗаданиеLesson #3. Boolean Expressions And Conditions

              • ЗаданиеLesson #4. Condition operator & branches. IF s...

              • ЗаданиеLesson #5. IF statement Part II. Max and Min

              • ЗаданиеHometask (until 6/10)

              • ЗаданиеFormatted output using WritelnFormat

              • ЗаданиеLesson #6. Chained IF statements and Case statemen...

              • ЗаданиеLesson #7. Loops

              • ЗаданиеLesson #8. For Loop

              • ЗаданиеLesson #9. Sum, Accumulators, Product and Counter....

              • ЗаданиеLesson #10. Minimum or Maximum value of N numbers

              • ЗаданиеLesson #11. While & Repeat Loops

              • ЗаданиеLesson #12. While & Repeat Loops: digits of a ...

              • ЗаданиеLesson #13. Procedures

              • ЗаданиеLesson #14. Functions

              • ЗаданиеTest Loops (control test)

            • Тема 2

            • Тема 3

            • Тема 4

            • Тема 5

            • Тема 6

            • Тема 7

            • Тема 8

            • Тема 9

            • Тема 10

        • Математика, механика

        • Педагогическое образование

        • Магистратура

          • Разработка мобильных приложений и компьютерных игр

        • Аспирантура

        • Вечернее отделение

        • Другое

      • Весенний семестр

        • Прикладная математика и информатика

        • Фундаментальная информатика и ИТ

        • Математика, механика

        • Педагогическое образование

        • Магистратура

          • Разработка мобильных приложений и компьютерных игр

        • Аспирантура

        • Вечернее отделение

        • Другое

      • Воскресная компьютерная школа

        • Пользователь компьютера плюс

        • Пользователь прикладных программ

        • Программирование I ступень

        • Программирование II ступень

        • Программирование III ступень

        • Архив

      • Воскресная математическая школа

        • Олимпиадная математическая школа

        • Открытое тестирование - 2023 г.

        • Открытое тестирование - 2022 г.

        • Повышение квалификации

        • Архив

        • Доступная математика

        • ВМШ - 22

      • Государственная итоговая аттестация

      • Дополнительное образование

      • Олимпиады

      • Видеолекции

      • Разное

      • Архив курсов

Вы используете гостевой доступ (Вход)
cs101 ENG
Сводка хранения данных
Скачать мобильное приложение Яндекс.Метрика