|
1 | 1 | # Introduction-to-Programming |
2 | 2 |
|
3 | | -Congratulations on making such a great decision to learn programming. In this repository we will focus on the concepts of programming more than solving problems. |
| 3 | +Congratulations on making such a great decision to learn programming! <br> |
| 4 | +In this repository we will focus on the concepts of programming more than solving problems. |
4 | 5 | <img src="https://st.depositphotos.com/2065405/4839/i/450/depositphotos_48392531-stock-photo-word-cloud-coding.jpg"> |
5 | 6 |
|
6 | 7 | ## Table of Contents |
@@ -206,7 +207,79 @@ High-level languages are classified as procedural and non-procedural and problem |
206 | 207 | 6. Put the program into operation. |
207 | 208 |
|
208 | 209 | # Chapter-05 |
209 | | -## Problem Solving |
| 210 | +## Problem Solving and Software Development Method |
| 211 | + |
| 212 | +We will focus on problem solving more than programming here. Problem solving is finding solutions for the descriptive problems. We will forllow some sequential way of solving problems using C. Later we will see the sotware development methods. |
| 213 | +### Problem Solving |
| 214 | + |
| 215 | +Problem solving is the process of transforming the description of a problem into a solution by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques and tools. |
| 216 | + |
| 217 | +Computers can be used to help us solve problems |
| 218 | + |
| 219 | +### Software Development Method(SDM) |
| 220 | + |
| 221 | +1. Specification of needs |
| 222 | +2. Problem analysis |
| 223 | +3. Design and algorithmic representation |
| 224 | +4. Implementation |
| 225 | +5. Testing and verification |
| 226 | +6. Documentation |
| 227 | + |
| 228 | +#### 1. Specification of Needs |
| 229 | +* To understand exactly: |
| 230 | +* what the problem is |
| 231 | +* what is needed to solve it |
| 232 | +* what the solution should provide |
| 233 | +* if there are constraints and special conditions. |
| 234 | + |
| 235 | +#### 2. Problem Analysis |
| 236 | +In the analysis phase, we should identify the following: |
| 237 | +* Inputs to the problem, their form and the input media to be used |
| 238 | +* Outputs expected from the problem, their form and the output media to be used |
| 239 | +* Special constraints or conditions (if any) |
| 240 | +* Formulas or equations to be used |
| 241 | + |
| 242 | +#### 3. Design and Algorithmic Representation |
| 243 | +* An algorithm is a sequence of a finite number of steps arranged in a specific logical order which, when executed, produces the solution for a problem. |
| 244 | +* An algorithm must satisfy these requirements: |
| 245 | + _It may have input(s) |
| 246 | + _It must have an output |
| 247 | + _It should not be ambiguous (different interpretations to it) |
| 248 | +* Every step in the algorithm must be clear and easily understandable |
| 249 | +* It must be general (it can be used for different input values) |
| 250 | +* It must be correct and solve the problem for which it is designed |
| 251 | +* It must execute and terminate in a finite amount of time |
| 252 | +* It should be efficient so that it can solve the intended problem using the minimum amount of computational resources |
| 253 | +* An algorithm can be represented using pseudocode or a flowchart. |
| 254 | + |
| 255 | +### Control Structure |
| 256 | +* In order to tackle a problem, we need a suitable Control Structure. |
| 257 | +* In 1966, two researchers, C. Bohn and G. Jacopini, demonstrated that any algorithm can be described using only 3 control structures: sequence, selection and repetition. |
| 258 | +#### 4. Implementation |
| 259 | +* The process of implementing an algorithm by writing a computer program using a programming language (for example, C) |
| 260 | +* The output of the program must be the solution to the intended problem |
| 261 | +* The program should not do anything that it is not supposed to do |
| 262 | +* Self-organization may occur in complex code. This is when a program handles unexpected input in a meaningful way without having explicitly being programmed to do so. |
| 263 | + |
| 264 | +#### 5. Testing and Verification |
| 265 | +* Program testing is the process of executing a program to demonstrate its correctness |
| 266 | +* Program verification is the process of ensuring that a program meets user-requirements |
| 267 | +* After the program is compiled, we must run the program and test and verify it with different inputs before the program can be released to the public or other users (or submitted for evaluation in this class) |
| 268 | + |
| 269 | +#### 6. Documentation |
| 270 | +* Contains details produced at all stages of the program development cycle. |
| 271 | +* Typically done in 2 ways: |
| 272 | +* Writing comments between and beside your actual lines of codes |
| 273 | +* Creating a separate document file to explain the program |
| 274 | +* Important not only for other people to use or modify your program, but also for you to understand your own program after a while (you will forget the details and logic of your own program before long) |
| 275 | + |
| 276 | +<b>Documentation</b> |
| 277 | +Documentation is important because: |
| 278 | + |
| 279 | +* You may return to your program in future to use parts of it again (e.g. certain functions) |
| 280 | +* Other programmers or end users might require some information about your program for reference or maintenance |
| 281 | +* You may someday have to modify your program, or may discover some errors or weaknesses in the program that you wish to correct or improve |
| 282 | +* Although documentation is listed as the last stage of software development method, it is actually an ongoing process which should be done from the very beginning of the software development process. |
210 | 283 |
|
211 | 284 | # Chapter-07 |
212 | 285 | ## Computer Software |
@@ -285,3 +358,227 @@ Developing algorithms using step-form: |
285 | 358 | 4. Operators- |
286 | 359 | a. Arithmetic Operators: ←; The expression X←6 means that a value 6 is assigned to the variable X. Examples: +, -, *, /, % |
287 | 360 | b. Relational Operators |
| 361 | + |
| 362 | +# Chapter-10 |
| 363 | +## Algorithms and Flowcharts |
| 364 | +# Chapter-11 |
| 365 | +## Pseudocodes |
| 366 | +* A pseudocode is a semiformal, (typically English) spoken-like language with limited vocabulary that can be used to design and describe algorithms. |
| 367 | +<b>Criteria of good pseudocode: </b> |
| 368 | + |
| 369 | +* Easy to understand, precise and clear. |
| 370 | +* Gives the correct solution in all cases. |
| 371 | +* Eventually ends. |
| 372 | + |
| 373 | +* A series of steps or statements that are executed in the order they are written in an algorithm. |
| 374 | +* The beginning and end of a block of statements can be optionally marked with the keywords begin and end. |
| 375 | +<b><i>Example 1:</i></b> |
| 376 | + |
| 377 | + 1. Begin |
| 378 | + 2. Read the birth date from the user. |
| 379 | + 3. Calculate the difference between the birth date and today’s date. |
| 380 | + 4. Print the user’s age. |
| 381 | + 5. End |
| 382 | + |
| 383 | +* Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed,evaluated as either true or false. |
| 384 | + |
| 385 | +* The keywords used are if and else. |
| 386 | +<b>Format:</b> |
| 387 | + |
| 388 | + if condition |
| 389 | + then-part |
| 390 | + else |
| 391 | + else-part |
| 392 | + end_if |
| 393 | + |
| 394 | +<b><i>Example 2:</i></b> |
| 395 | + |
| 396 | + if age is greater than 55 |
| 397 | + print “Pencen” |
| 398 | + else |
| 399 | + print “Kerja lagi” |
| 400 | + end_if |
| 401 | + |
| 402 | +* Sometimes in certain situations, we may omit the else-part. |
| 403 | + |
| 404 | + if number is odd number |
| 405 | + print “This is an odd number” |
| 406 | + end_if |
| 407 | + |
| 408 | +* Nested selection structure: basic selection structure that contains other if/else structure in its then-part or else-part. |
| 409 | + |
| 410 | + if number is equal to 1 |
| 411 | + print “One” |
| 412 | + else if number is equal to 2 |
| 413 | + print “Two” |
| 414 | + else if number is equal to 3 |
| 415 | + print “Three” |
| 416 | + else |
| 417 | + print “Other” |
| 418 | + end_if |
| 419 | + |
| 420 | +<b><i>Example 3:</i></b> |
| 421 | + |
| 422 | +<b>Pseudocode: The Repetition control structure.</b> |
| 423 | + |
| 424 | +* Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. |
| 425 | +* The keyword used is while. |
| 426 | +<b>Format:</b> |
| 427 | + while condition |
| 428 | + loop-body |
| 429 | + end_while |
| 430 | + |
| 431 | +<b><i>Example 5: Summing up 1 to 10</i></b> |
| 432 | + |
| 433 | + set cumulative sum to 0 |
| 434 | + set current number to 1 |
| 435 | + while current number is less than or equal to 10 |
| 436 | + add the current number to cumulative sum |
| 437 | + add 1 to current number |
| 438 | + end_while |
| 439 | + print the value of cumulative sum |
| 440 | + |
| 441 | +Pseudocode: The Repetition control structure |
| 442 | +• Subsequently, we can write the previous pseudocode |
| 443 | +(example 5) like this. |
| 444 | +• Example 6: Summing up 10 numbers |
| 445 | +cumulative sum = 0 |
| 446 | +current number = 1 |
| 447 | +while current number is less than or equal to 10 |
| 448 | +cumulative sum = cumulative sum + current number |
| 449 | +current number = current number + 1 |
| 450 | +end_while |
| 451 | +print the value of cumulative sum |
| 452 | +• Note that in this algorithm, we are using both the |
| 453 | +sequence and repetition control structure |
| 454 | +17 |
| 455 | +Pseudocode: The Repetition control structure |
| 456 | +• Example 7: |
| 457 | +Begin |
| 458 | +number of users giving their birth date = 0 |
| 459 | +while number of users giving their birth date < 10 |
| 460 | +begin |
| 461 | + Read birth date from the user. |
| 462 | + Calculate the difference between the birth |
| 463 | +date and today’s date. |
| 464 | + Print the user’s age. |
| 465 | + if the age is greater than 55 |
| 466 | +print “Pencen” |
| 467 | + else |
| 468 | +print “Kerja lagi” |
| 469 | + end_if |
| 470 | + number of users giving their birth date + 1 |
| 471 | +end |
| 472 | +end_while |
| 473 | +End |
| 474 | +18 |
| 475 | +Pseudocode: The Repetition control structure |
| 476 | +• Example 8: |
| 477 | +while user still wants to play |
| 478 | +begin |
| 479 | +Select either to play on a network or play against computer |
| 480 | + if play on network |
| 481 | +create connection to remote machine |
| 482 | +play game with connected computer |
| 483 | + else |
| 484 | + select mission |
| 485 | + play game locally |
| 486 | + end_if |
| 487 | +Ask user whether he/she still wants to play |
| 488 | +end |
| 489 | +end_while |
| 490 | +19 |
| 491 | +Pseudocode: The Repetition control structure |
| 492 | +• Example 9: |
| 493 | +while user still wants to play |
| 494 | +begin |
| 495 | +Select either to play on network or play against computer |
| 496 | +if play on network |
| 497 | +create connection to remote machine |
| 498 | +play game with connected computer |
| 499 | +Else |
| 500 | +select mission |
| 501 | +play game locally |
| 502 | +end_if |
| 503 | +Ask user whether he/she still wants to play |
| 504 | +end |
| 505 | +end_while |
| 506 | +• For readability, always use proper indentation!!! |
| 507 | +20 |
| 508 | +Flowcharts |
| 509 | +• A flowchart is a graph used to depict or show |
| 510 | +a step by step solution using symbols which |
| 511 | +represent tasks. |
| 512 | +• The symbols used consist of geometrical |
| 513 | +shapes that are connected by flow lines. |
| 514 | +• It is an alternative to using pseudocode; while |
| 515 | +a pseudocode description is verbal, a |
| 516 | +flowchart is graphical in nature. |
| 517 | +21 |
| 518 | +Flowchart Symbols |
| 519 | +Terminal symbol - indicates the beginning and |
| 520 | +end points of an algorithm. |
| 521 | +Process symbol - shows an instruction other than |
| 522 | +input, output or selection. |
| 523 | +Input-output symbol - shows an input or an output |
| 524 | +operation. |
| 525 | +Disk storage I/O symbol - indicates input from |
| 526 | +or output to disk storage. |
| 527 | +Printer output symbol - shows hardcopy printer |
| 528 | +output. |
| 529 | +22 |
| 530 | +Flowchart Symbols cont… |
| 531 | +Selection symbol - shows a selection process |
| 532 | +for two-way selection. |
| 533 | +Off-page connector - provides continuation |
| 534 | +of a logical path on another page. |
| 535 | +On-page connector - provides continuation |
| 536 | +of logical path at another point in the same |
| 537 | +page. |
| 538 | +Flow lines - indicate the logical sequence of |
| 539 | +execution steps in the algorithm. |
| 540 | +23 |
| 541 | +Flowchart – sequence control structure |
| 542 | +Statement 2 |
| 543 | + Statement 1 |
| 544 | + Statement 3 |
| 545 | +: |
| 546 | +24 |
| 547 | +Flowchart – selection control structure |
| 548 | +Condition |
| 549 | +elsestatement(s) |
| 550 | +thenstatement(s) |
| 551 | +No Yes |
| 552 | +25 |
| 553 | +Flowchart – repetition control structure |
| 554 | +Condition Loop |
| 555 | +Statement(s) |
| 556 | +yes |
| 557 | +no |
| 558 | +26 |
| 559 | +Flowchart – example 1 |
| 560 | +Begin |
| 561 | +Read birth date |
| 562 | +Calculate |
| 563 | +Age = current year – birth date |
| 564 | +Display age |
| 565 | +End |
| 566 | +27 |
| 567 | +Flowchart – example 2 |
| 568 | +Begin |
| 569 | +Read age |
| 570 | +End |
| 571 | +YES Age > 55? NO |
| 572 | +print “Pencen” print “Kerja lagi” |
| 573 | +28 |
| 574 | +Flowchart – example 5 |
| 575 | +Begin |
| 576 | +End |
| 577 | +current_number <= 10? |
| 578 | +NO |
| 579 | +YES |
| 580 | +sum = 0 |
| 581 | +current_number = 1 |
| 582 | +sum = sum + current_number |
| 583 | +current_number = current_number + 1 |
| 584 | +print sum |
0 commit comments