1 / 18

Intro to Art of Programming

B Smith: Slightly long. Score 3. B Smith: 8/24/05: Steps are not small enough. Introduction of loop code, etc., is confusing. Must find a better point of entry. Intro to Art of Programming. Math 130 Lecture # 01. Overview. Programs and their connection to algorithms Compilation

dragon
Download Presentation

Intro to Art of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B Smith: Slightly long. Score 3. B Smith: 8/24/05: Steps are not small enough. Introduction of loop code, etc., is confusing. Must find a better point of entry. Intro to Art of Programming Math 130Lecture # 01

  2. Overview • Programs and their connection to algorithms • Compilation • Pseudocode: The alternative to thinking in C • Modularity • The components of a program • main() • functions/subroutines/procedures • A simple program • Hello World

  3. Programming - B. Gates • Interviewer: "Is studying computer science the best way to prepare to be a programmer?" • Bill Gates: "No, the best way to prepare is to write programs, and to study great programs that other people have written. In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."

  4. What is a Program? • A computer program is... • A list of instructions • A sequence of operations • Commands to a computer • Something that accepts input, and manipulates it to get an output. • The output to this one is… #include <stdio.h> void findMax(int [5]); int main() { int nums[5] = {2, 18, 1, 27, 16}; findMax(nums); return 0; } void findMax(int vals[5]) { int i, max = vals[0]; for (i = 1; i <= 4; ++i) if (max < vals[i]) max = vals[i]; printf("The max value is %d", max); getchar(); return; }

  5. Programming Language • The allowed group of instructions • Some instructions are built from other instructions • findMax() • Others are requested from an established group, or library of instructions (library functions) • printf() • getchar() #include <stdio.h> void findMax(int [5]); int main() { int nums[5] = {2, 18, 1, 27, 16}; findMax(nums); return 0; } void findMax(int vals[5]) { int i, max = vals[0]; for (i = 1; i <= 4; ++i) if (max < vals[i]) max = vals[i]; printf("The max value is %d", max); getchar(); return; }

  6. #include <stdio.h> void findMax(int [5]); int main() { int nums[5] = {2,18,1,27}; findMax(nums); return 0; } 111111000111 111111011010 111101011110 110101000110 100101011010 101111000110 111001011011 110001010000 100001011010 101001011011 110001000111 001001011011 111001011010 111001011010 111111011010 111001011010 source code machine code Basics of Program Development • The process starts with a text file (C language) and ends with a .exe (executable file, “Machine Language”) • machine language is understood by the computer • Six phases before a program is executed: • edit • pre-process • compile • link • load • execute

  7. Compiling Programs • Edit a file and save with the proper extension • .c for C language, .cpp for C++, .java for Java • this file must be saved as a “text” file, not a word document • Give the command to compile the program • the preprocessor does additional prep work on source • the source code gets translated to machine code • Your code is linked with other resources/library functions • The linked code is loaded into computer memory and executed

  8. Pseudocode • Simple language to help a programmer “think through” a program before writing in a programming language like C • Consists mainly of action statements • Use standard English, Spanish, Czech, etc. for expressing the flow of operations and computations • Not executed on computers • Using pseudocode is a must for many programmers • Many people think better with pen and paper vs. at the computer

  9. B Smith: More useful to think of this as numbers coming out of a black box. We only see numbers one ata time. Pseudocode Example • Describe an algorithm to find the maximum number in the list of numbers {2, 18, 1, 27, 16} • select the first number and name it “max” • point to “next number” • reassign “max” with next on list if it’s greater than current max • if not at end of list, go to step 2 void findMax(int vals[5]) {. . . max = vals[0]; for (i = 1; i <= 4; ++i) if (max < vals[i]) max = vals[i]; printf("The max value is %d", max); }

  10. Modularity • Def of a module: • A “bite-sized” chunk of code can be called a module. Typically each module has a distinct purpose. • Makes troubleshooting (debugging) programs much easier • Modules in C are generally implemented with functions #include <stdio.h> void findMax(int [5]); int main() { int nums[5] = {2, 18, 1, 27, 16}; findMax(nums); return 0; } void findMax(int vals[5]) { int i, max = vals[0]; for (i = 1; i <= 4; ++i) if (max < vals[i]) max = vals[i]; printf("The max value is %d", max); getchar(); return; }

  11. Basic Structure of a C Program output “Hello World!” Example: Hello World Algorithm: C Program: #include <stdio.h> int main() { printf(“HelloWorld!”); return 0; }

  12. The main() function • Consider main( ) to be a driver function. • The chief of the submodules • All programs require a main function • Example: • Helps you think about top-down programming int main() { GETgrossPay( ); DetTaxes( ); CalNetPay( ); displyAns( ); return 0; }

  13. The printf() function • printf() is a prewritten function in C • It writes formatted output to the standard output • typically the screen/terminal #include <stdio.h> int main() { printf("Hello there world!"); return 0; }

  14. Comments • A comment is anything you want the compiler to ignore in the process of reading and creating your program • /* is the beginning of a comment • */ is the end of a comment • They do not have to be on the same line • Each beginning must be matched with an end • Use comments liberally! • They will tell you (or others) what you thought you were doing; i.e., the intent of the code • Very, very important for documenting your thoughts!

  15. Comments (cont) • Comments do not “nest” /*Comments start with a “/*” and end with a “*/” but they don’t nest! */

  16. Preprocessor Commands • #include <stdio.h> is a preprocessor command • Preprocessor commands always start with # • They specify an action to be taken prior to the compiler translating the source code into machine code • The statement here does literally what it says: include the file “stdio.h” prior to compiling. • stdio.h is defined as a “header file” • We need stdio.h to be able to use printf ( ) /* File: Lab01.c Description: Display hello! Programmer: I. Student Date: 1/24/05 */ #include <stdio.h> int main() { printf("Hello!"); return 0; }

  17. The first program • Enter, compile, run and submit this program: • under Dev-C: Alt-Insert-Comment/header /* File: Lab01.c Description: Displays Hello there world! Programmer: I. Student Date: 1/20/06 */ #include <stdio.h> #include <stdlib.h> int main() { printf("Hello there world!"); system(“pause”); return 0; }

  18. Review • Programs and their connection to algorithms • Compilation • Pseudocode: The alternative to thinking in C • Modularity • The components of a program • main() • functions/subroutines/procedures • A simple program • Hello World

More Related