1 / 127

Administrative details

Administrative details. Project #1 due Tuesday Lab #6 on Tuesday Midterm exam next week Content for exam: General computer knowledge Unix commands C language Chapters 1-5 Afzal Chapters 1-4 Deitel Class slides Lab work Homework. Topic 1: Introduction to Computers and Programming.

Download Presentation

Administrative details

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. Administrative details • Project #1 due Tuesday • Lab #6 on Tuesday • Midterm exam next week • Content for exam: • General computer knowledge • Unix commands • C language • Chapters 1-5 Afzal • Chapters 1-4 Deitel • Class slides • Lab work • Homework CISC 105 – Topic 1

  2. Topic 1: Introduction to Computers and Programming “A journey of a thousand miles must begin with a single step.” - Lao Tzu

  3. Binary Math A binary digit or bit for short is the smallest unit of computer information. Just as our familiar decimal number system has 10 digits,(0,1,2,3,4,5,6,7,8,9) the binary number system has only 2 digits (0,1). This is the perfect number system for computers since we can store a binary digit by making an electrical or magnetic field either on or off, positive or negative, 1 or 0. In the decimal system we can count 10 (we start counting with 0) items with one decimal place, 100 with two decimal places, 1000 with three decimal places and so on. CISC 105 – Topic 1

  4. Binary Math The binary number system works the same way except since we only have 0s and1s our base is 2. So we can count 2 permutations of 1 bit: 0 1 4 permutations of 2 bits: 00 01 10 11 8 permutations of 3 bits: 000 001 010 011 100 101 110 111 16 permutations of 4 bits: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 ...and so on. CISC 105 – Topic 1

  5. Binary Math So in an eight bit byte, the numbers are represented this way: Bit: - - - - - - - - Value: 128 64 32 16 8 4 2 1 Example: 1 0 1 1 0 1 1 0 Values: 128 + 0 + 32 +16 + 0 + 4 + 2 + 0 = 182 The minimum number is 0 (all 0s) and the maximum number is 255 (all 1s), so you can represent 256 numbers (0-255) with one byte. CISC 105 – Topic 1

  6. Hardware Components • Hardware consists of many types of components: • Primary Storage • Secondary Storage • The Central Processing Unit (CPU) • Input Devices • Output Devices CISC 105 – Topic 1

  7. Primary Storage andSecondary Storage • There are two primary types of storage in a computer: • Primary Storage – often referred to as main memory, or just memory. This storage is very quickly accessed by the various hardware components. • Secondary Storage – includes such devices as a hard disk, floppy disk, CD-ROM, DVD-ROM, etc… CISC 105 – Topic 1

  8. Computer Software • All major computers run an operating system. • An operating system is a special type of program which controls the interaction between all hardware components and the user. CISC 105 – Topic 1

  9. Operating Systems Tasks • Responsibilities of the OS include: • Communicating with the user(s) • Managing resources including memory and processor time among programs • Collecting data from input devices • Providing data to output devices • Accessing data from secondary storage • Writing data to secondary storage CISC 105 – Topic 1

  10. Computer Programming - Machine Language • Computer programs were originally written in machine language. Machine language is a sequence of binary numbers, each number being an instruction. • Each instruction is computer-understandable. 00000000 00010101 00010110 CISC 105 – Topic 1

  11. Computer Programming - Assembly Language • To make machine language more abstract, assembly language was introduced. • Assembly language provides a one-to-one mapping between a machine instruction and a simple human-readable instruction. • Assembly language is converted to computer-understandable machine language by an assembler. CLR A ADD A ADD B 00000000 00010101 00010110 CISC 105 – Topic 1

  12. Computer Programming – High-Level Languages • In order to get around these obstacles, high-level languages were introduced. • High-level languages provide a one-to-many mapping between one high-level statement and multiple assembly language instructions. • High-level language is converted to assembly instructions by a compiler, which creates object files. Modern compilers perform the assembler function as well. CISC 105 – Topic 1

  13. Modern Software Development • Programmers often use library functions, which are pre-written functions provided as part of the compiler/development toolset. • A library is an object file. • After the source code is compiled into machine code, a linker resolves cross-references among these object files, including libraries. The object files are linked into an executable file. CISC 105 – Topic 1

  14. Modern Software Development Compiler Source Code File Object File Other Object Files (perhaps libraries) Linker Executable File Loader CISC 105 – Topic 1

  15. The Software Development Method • Specify the problem. • Analyze the problem. • Design an algorithm to solve the problem. • Implement the algorithm. • Test and verify the program. • Maintain and update the program. CISC 105 – Topic 1

  16. Review • Name the principle components of a computer. • What are three responsibilities of the operating system? • What advantages does assembly language have over machine language? • What advantages do high-level languages have over assembly language? CISC 105 – Topic 1

  17. Review • What computer program translates a source code file into an object file? • What program combines object files and produces an executable file? • What part of the operating system is responsible for the loading and scheduling of programs? • What are the steps in the software development method? CISC 105 – Topic 1

  18. Topic 2 – Introduction to the C Programming Language

  19. Preprocessor Directives • A preprocessor directive is a command given to the C preprocessor, which is a part of the compilation process that modifies a C source code file before it is compiled. • Preprocessor directives always begin with a “#” character. • In the example program, there are two preprocessor directives used, #include and #define. CISC 105 – Topic 1

  20. #include • The #include directive tells the C preprocessor to give a program access to a library. By putting a #include in a program, the preprocessor loads a header file, which tells the compiler what functions and other information is provided in the library. • In this program, #include <stdio.h> indicates that this program uses the stdio library which contains functions such as printf(). Page 542 of Deitel lists standard libraries. CISC 105 – Topic 1

  21. #define • The #define directive specifies a constant macro. • This tells the preprocessor that every time it encounters the first text, it should replace it with the second text. • In this program, #define KMS_PER_MILE 1.609 tells the preprocessor to replace KMS_PER_MILE with 1.609 every place KMS_PER_MILE appears in the program. CISC 105 – Topic 1

  22. Preprocessor Directives /* Converts distances from miles to kilometers */ #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double miles, /* distance in miles */ kms; /* equiv. distance in kms */ /* Get the distance in miles */ printf(“Enter the distance in miles>”); scanf (“%lf”, &miles); /* Convert the distance to kilometers */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers */ printf (“That equals %f kilometers.\n”,kms); return(0); } CISC 105 – Topic 1

  23. #include directive – Tells the preprocessor to include the stdio.h header file. This file describes the functions and other information included in the stdio library. Preprocessor Directives /* Converts distances from miles to kilometers */ #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double miles, /* distance in miles */ kms; /* equiv. distance in kms */ /* Get the distance in miles */ printf(“Enter the distance in miles>”); scanf (“%lf”, &miles); /* Convert the distance to kilometers */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers */ printf (“That equals %f kilometers.\n”,kms); return(0); } CISC 105 – Topic 1

  24. #include directive – Tells the preprocessor to include the stdio.h header file. This file describes the functions and other information included in the stdio library. #define directive – Tells the preprocessor to replace every occurrence of KMS_PER_MILE in the program with 1.609. Preprocessor Directives /* Converts distances from miles to kilometers */ #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double miles, /* distance in miles */ kms; /* equiv. distance in kms */ /* Get the distance in miles */ printf(“Enter the distance in miles>”); scanf (“%lf”, &miles); /* Convert the distance to kilometers */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers */ printf (“That equals %f kilometers.\n”,kms); return(0); } CISC 105 – Topic 1

  25. What is a valid identifier? • Identifiers can only be composed of letters, digits, and underscores. • Identifiers cannot begin with a digit. • Reserved words cannot be identifiers. • Identifiers can be as long as you want. • Upper and lower case letters are different (i.e. kms and Kms are not considered to be the same identifier). CISC 105 – Topic 1

  26. Review • Which of the following are valid identifiers? For each that is not valid, why is it not? This_is_a_long_identifier_but_is_it_valid? 8timesTheRadius miles Phil’s variable kilometers_per_hour x “radius” CISC 105 – Topic 1

  27. Variables • A variable is a memory cell that is used to hold data acted upon by the program. • A variable declaration tells the C compiler the name and type of a variable used in a program. • A variable declaration consists of a data type and an identifier which is the name of that variable. • Every variable that will be used in a program must be declared. CISC 105 – Topic 1

  28. Variable Declarations • The first line declares a variable named mile of the double data type. • The second line declares a variable named counter of the int data type. double mile; int counter; CISC 105 – Topic 1

  29. Data Types • There are a large number of data types. These are some of the most popular ones. • void – this keyword means “no data type”. • int – An integer (a whole number). This data type can represent an integer in a specific range, at least –32767 through 32767. CISC 105 – Topic 1

  30. Data Types • char – A character. One letter, digit, or symbol. This is enclosed in single quotes. • float – A real number (an integer part and a decimal part). • double – A real number. Note that this data type is a memory cell double the size of a float data type. This allows a bigger number to be represented, or a specific number to be represented more precisely. This is referred to as a double-precision floating point number. CISC 105 – Topic 1

  31. Review of Variables • Write a #define preprocessor declaration for a constant macro of STUDENTS_PER_SECTION to 22 and variable declarations of num_students as an integer, GPA and class_GPA as double-precision floating point numbers, and letter_grade as a character data type. #define STUDENTS_PER_SECTION 22 int num_students; double GPA, class_GPA; char letter_grade; CISC 105 – Topic 1

  32. Assignment Statements • An assignment statement is one type of executable statement. • An assignment statement uses the “=“ operator, and follows the form: variable = expression; • This statement first evaluates the expression on the right and stores the result in the variable on the left. CISC 105 – Topic 1

  33. Assignment Statements • Here are some examples of assignment statements: • x = 12; • negative_x = -x; • x = y + 12 + z * 5; • result = y; • Note that any variables in the right-side expression are not modified by an assignment statement. CISC 105 – Topic 1

  34. Function Calls • A function is a piece of code which performs a specific task. • Functions can be created by programmers or supplied as part of the C compiler toolset. • A function is called, which causes it to execute. • A function call is composed of the function name, an open paren, a set of function arguments separated by commas, and a close paren. CISC 105 – Topic 1

  35. function name function arguments open paren close paren Function Calls • A function call looks like this: function_name(argument1, argument2, argument3); CISC 105 – Topic 1

  36. format string print list function arguments The printf Function • The C function for displaying output on the screen to the user is printf. • printf is called in the following manner: printf(“The final GPA of student number %d is %f.\n”,student_num, GPA); CISC 105 – Topic 1

  37. The Format Stringand Print List • The format string is the text that is to be displayed on the screen. • The “%” characters are called placeholders. They indicate the display position for variables whose values are to be displayed. • The variable names to be displayed are specified in the print list and appear in the same order as their placeholders. CISC 105 – Topic 1

  38. This pair specifies the location in the print string to display the student_num value. This pair specifies the location in the print string to display the GPA value. Placeholders printf(“The final GPA of student number %d is %f.\n”,student_num, GPA); CISC 105 – Topic 1

  39. Placeholders • All placeholders begin with a “%”. • The text after the “%” symbol indicates how to format the output, i.e. what kind of variable it is. • %d – decimal number (int) • %f – floating-point number (float or double) • %c – character (char) CISC 105 – Topic 1

  40. Escape Sequences • All escape-sequences begin with a backslash, “\”. • A letter after the “\” character denotes an escape sequence, which has special meaning. • The “\n” sequence indicates a new-line character, which cause any following text to appear on the next line on the display. • In order to display a “\”, the format string must contain a “\\”. CISC 105 – Topic 1

  41. Specifies to display student_num, which is an integer. Specifies to display GPA, which is an floating-point number. Placeholders and theNewline Escape Sequence printf(“The final GPA of student number %d is %f.\n”,student_num, GPA); CISC 105 – Topic 1

  42. Specifies to display student_num, which is an integer. Specifies to display GPA, which is an floating-point number. Placeholders and theNewline Escape Sequence printf(“The final GPA of student number %d is %f.\n”,student_num, GPA); Therefore, if the student_num variable was set to 10 and the GPA variable was set to 3.03 this printf function call would cause: The final GPA of student number 10 is 3.03. to be displayed on the screen and any subsequent output to begin on the next line. CISC 105 – Topic 1

  43. format string input list function arguments The scanf Function • The C function for reading input from the user is scanf. scanf(“%d %f”,&student_num, &GPA); CISC 105 – Topic 1

  44. The Format String • The format string is the set of placeholders which specify what type of data is being input. • The same placeholders are used as for printf, except for when inputting a floating-point number. A float type still uses the “%f”, however the double type uses the “%lf” placeholder. CISC 105 – Topic 1

  45. The Input List • The variables to store the inputted data are specified in the input list. They must be in the same order as their corresponding placeholders. • Notice that each variable name is preceded by a “&”. • The “&” is an operator which means “the address of”. • Therefore, “&student_num” tells the scanf function to store what it reads from the user at the memory address of student_num. CISC 105 – Topic 1

  46. This pair specifies to read in an integer and store it at the memory address of student_num, thus setting the student_num variable to the inputted value. Placeholders and the Input List scanf(“%d %f”,&student_num, &GPA); CISC 105 – Topic 1

  47. This pair specifies to read in a single-precision floating point number and store it at the memory address of GPA, thus setting the GPA variable to the inputted value. Placeholders and the Input List scanf(“%d %f”,&student_num, &GPA); CISC 105 – Topic 1

  48. Placeholders and the Input List scanf(“%d %f”,&student_num, &GPA); Therefore, if the input into this scanf function call was: 9 3.560 <ENTER> the student_num variable would be set to 9 and the GPA variable would be set to 3.560 CISC 105 – Topic 1

  49. Review of printf and scanf • What is the displayed output when the following code fragment is run and the inputs are 8 and 12? int x, y; printf(“My name is”); printf(“ Phil Viscito”); printf(“\nEnter two integers> ”); scanf(“%d%d”,&x, &y); x = x + 2; y = x + y; printf(“Thanks! The answer is %d.\nBye now!”,y); My name is Phil Viscito. Enter two integers> 8 12 Thanks! The answer is 22. Bye now! CISC 105 – Topic 1

  50. Customizing Integer Output • The “%d” placeholder, used to display an integer variable, can be altered to format how the number is displayed. • Instead of “%d”, use a “%Xd” where the X is an integer that is the field width, the number of digits to display. • For example, “%4d” displays four digits of the result. The negative sign (for negative integers) is also considered a digit here. CISC 105 – Topic 1

More Related