400 likes | 592 Views
Introduction to C Programming. Unit 1. What is a Program?. Unit 1: Programs. Recall: A Computer System is …. Hardware Central processing unit (CPU) Main memory (RAM, ROM) Secondary memory (Hard disk, CD, DVD, flash drive, etc) Input devices Output devices Network interface Power supply
E N D
Introduction toC Programming Unit 1
What is a Program? Unit 1: Programs
Recall: A Computer System is … • Hardware • Central processing unit (CPU) • Main memory (RAM, ROM) • Secondary memory (Hard disk, CD, DVD, flash drive, etc) • Input devices • Output devices • Network interface • Power supply • Software • Firmware • Operating system • Application programs
What is a Software “Program”? • A set of instructions • Performs a specific task • Required to make the hardware “do something” • The hardware “runs” or “executes” it • Must be in main memory (RAM or ROM) • Is written in machine language (specific to a CPU) • Consists of binary ‘1’s and ‘0’s
How a Program gets into Memory • It is always stored in memory (ROM) • Firmware such as a PC BIOS • Embedded systems contain ROM or Flash • From secondary storage when needed • Read from the Hard Disk into main memory • Read directly from a CD or DVD into main memory • Via the network • Automatic updates from the program vendor • Web pages and web applications with active content • Mobile applications • Cloud computing
Programs are Very Diverse … • Mobile applications – Info services and transactions • Web applications – Web pages, shopping, Virtual Library • Business applications – Word processing, email, accounting • Leisure applications – Games, e-book readers • Educational applications – Language learning, encyclopedia • Batch data processing – Payrolls, bank statements • Server software – Provide information services via web • Operating systems – Control hardware, software, security • Control systems – Automation, energy management • Embedded software – Routers, printers, appliances, automobiles, cameras, watches, cell phones, lighting control
…But Programs Have Much in Common • Programs process information • Information is stored in the form of binary data • Different types of information use different data types • Programs acquire input data • Programs produce output data • Programs are implementations of algorithms • Algorithms are precise solutions to problems • Programs are written in programming languages • May be compiled (translated) to machine language • May be compiled to intermediate form and then interpreted
This Class Introduces Programming • Programming is the discipline and art of creating software • Focus is on fundamental concepts and building skills • Examples, labs, and projects will use the C language • Prerequisite class – NT1110 Computer Structure & Logic • Future classes: • ELCT: ET2640 Microprocessors and Microcontrollers • ELCT: ET2750 Programmable Logic Controllers • EECT: ET4560 C++ Programming • EECT: ET4640 Embedded Systems
Programs Relevant to Electronics Unit 1: Programs
Program-based Solutions • Programs used to solve electrical engineering problems • Solutions of equations needed for design and analysis • Example: Iterative calculations of roots of complex polynomials • Example: www.wolframalpha.com – Knowledge computation engine • Try: “0.45 ohms per foot for 100 meters” • Simulation software to try circuit design before building • Multisim • Chip design software • Cost is hundreds of thousands of dollars per workstation • One design engineer can access immense libraries of knowledge • Simulations can save millions in time and manufacturing costs
What is a Control System? • An electronic control system is a device that: • Regulates a process (such as a manufacturing system) • Components: • Sensors – Convert physical variables to electronic signals • Controllers – Based on sensor data, send signals to actuators • Actuators – Use electronic signals to affect physical world • Example: A commercial baking oven • Example: An automobile engine • The controller can be … • Analog electronics • Hard-wired digital electronics • A programmable computer-based controller
Benefits of Programmable Control • Flexibility – can be reprogrammed • Upgradable • Can have optional features • Defects can be easily repaired • Lower engineering costs • Standard hardware designs available • Software designs can be reused • Self-testing and error reporting • Programmable logic controllers – PLCs • Off-the-shelf hardware • Simplified programming
Embedded Systems • Found everywhere as part of electronic devices • Examples: Watches, calculators, appliances, irrigation and lighting systems, copiers, traffic lights, MP3 players • Pre-programmed computer • Dedicated to a few functions inside a device • Often must be real-time (respond “immediately” to events) • Non-stop computing (continuously functioning) • Traditionally viewed as non-reprogrammable • Reprogrammability is becoming commonplace Therefore, programming is essential to electronics students!
How are Programs Created? Unit 1: Software Development Process
Creating a Program • Use problem solving skills • Create an algorithm • Implement the algorithm in a programming language • Understand the programming model • Use specialized tools • Use software engineering methodology
Use Problem-Solving Process • Heuristics (strategies) for successful problem solving • Five-step process (from Strategies for Creative Problem Solving, Fogler et al) • Define the problem • Generate possible solutions • Evaluate and decide on a solution • Implement the solution • Evaluate the solution
Develop Problem-Solving Skills • A good programmer is an effective problem solver • Has a belief that problems can be solved through analysis • Develops a high level of problem-solving skills: • Active – Makes sketches, draws figures, asks questions • Methodical – Keeps track of progress in solving the problem • Detail-oriented • Takes great care to understand facts and relationships • Checks and re-checks for accuracy
Create an Algorithm • Develop an algorithm before writing a program • Use precise statements • Based in mathematics and logic • Clear and concrete language • Define a step-by-step process • Goal is solution to a problem
Use a Programming Language • A computer’s CPU uses machine language • Consists of binary values representing instructions • Not practical for human programmers • Assembly language • Low-level language closely tied to machine language • Once widely used, now only specialized use • High-level languages • Hundreds to thousands of languages in use • Much easier to use than assembly and machine language • Compiler provides automatic translation to machine language
Based on a Programming Model • The operating system (OS) presents the programming model • An OS is used on all but the smallest computers • Controls the hardware • Standardizes access to files, sharedresources • May be console user interface or graphical (GUI) user interface • Console-based user interface • Called “command-line” access • Based on text serial input and output • Graphical user interface (GUI) • Based on point-and-click or touch screen and windowed interface • Event-driven, object-oriented model • Embedded systems may not have a user interface at all
The Compiler Approach • A compiler is a translation program • Input is written using a programming language • Output is assembly language for one specific CPU instruction set • Assembler may be included or separate • Completes the translation into machine language • Linker • Combines translated program pieces with library pieces • Creates a complete, executable module • Operating system and loader • Loads module into main memory and starts execution • Process must be done again for each type of “host” system
The Interpreter Approach • Compiler creates an intermediate representation • Program is neither in high-level language, nor machine language • Instead, it is “virtual instructions” in an intermediate language • The virtual instructions are the same for all CPUs • Interpreter follows the virtual instructions • A different interpreter is needed for each CPU type • Benefits • A single compilation can theoretically be used on all computers • Presents a common programming model on multiple CPUs • Drawback • Interpreters are slower than compiled (“native”) programs
The “Just-In-Time” (JIT) Approach • Compiler • Creates an Intermediate Language (IL) representation • Runtime system • Does not interpret • Contains a “compiler” which completes compilation • Result is machine language produced just before execution • Best of both worlds • Code can be “Compiled once, Run anywhere” (e.g. Java, .NET) • Single programming model regardless of computer host • Program execution speed is comparable to native code
Specialized Programming Tools • Editor • Compiler • Assembler • Debugger • Support files • Libraries • Integrated development environment • Complete package of tools • Uses a “project-based” approach
Software Engineering • Waterfall model, iterative model, agile development • Processes to move from idea to product • Lifecycle management • Beginning-to-end product management • Includes program maintenance over time • Support and structure for working in teams • Coding standards • Change control • Project management • Human factors • “User friendly” design, ease of use
What is an Algorithm? Unit 1: Algorithms
Software Development Method • Specify the problem requirements • Analyze the problem • Design the algorithm to solve the problem • Implement the algorithm as a program • Test and verify the completed program • Maintain and update the program
Analyzing the Problem • Requires identifying the … • Inputs – the data available to work with • Outputs – the desired results • Including the format of the output • Additional requirements • Constraints
Producing the Algorithm • List of steps from beginning to end • Begin at step 1, have a specific step to End • Don’t try to get every detail at first • Use top-down design: • Break the problem into subproblems • Solve each subproblem individually • Similar to outlining a written paper • Use pseudo-code combined with flowcharting • Desk-check the algorithm
Convert the Algorithm to a Program • Implement the flowchart and pseudo-code as a program • Verify that the program compiles • Test the program • Create several test cases and verify the results • Test boundary conditions • Enter erroneous data intentionally • If the program behaves unexpectedly, back up • Double-check the algorithm, modify if needed • Double-check the program, modify if needed • Use debugging tools and techniques
Identify Variables • Information in an algorithm is put into variables • A variable is the programming term describing the storage of a single datum or a collection of data. • In a program, the compiler will put the variables into memory • Each data item used in the algorithm should be given a name • Each name should be distinct and used for only one variable • Data types for variables include: • Integers: 0, 12, -53 • Decimal numbers: 22.76, 193.1 • Decimals in scientific notation 3.0x108 • Character strings (in double-quotes) “Programming is fun!”
Simple Example: miles to kilometers • Problem statement“Convert a distance in miles to kilometers” • Identify the inputsdist_in_milesA decimal number representing a distance in miles • Identify the outputsdist_in_kmsA decimal number representing the distance in kilometers
List the Steps to Solve the Problem • Break the problem into steps: • Input the dist_in_miles • Convert dist_in_miles to kilometers • Output the dist_in_kms • As you can see, step 2 is not precise, because it does not specify how to do the conversion. This is not yet a complete algorithm.
What is the Formula for Step 2? • One mile is equal to approximately equal to 1.609 km • A number of miles can be converted to kilometers by multiplying by 1.609 • The formula is: • dist_in_km = dist_in_mi × 1.609
Algorithm to Convert mi to km • Input the dist_in_miles • Convert dist_in_miles to kilometers • Multiply dist_in_miles by 1.609 • Put the result in dist_in_kms • Output the dist_in_kms • End This algorithm accomplishes the task, but it is not user-friendly. We need to add some human factors to the algorithm.
An Improved Algorithm • Output “Convert miles to kilometers” • Output “Please enter the distance in miles” • Input dist_in_miles • Convert dist_in_miles to kilometers • Multiply dist_in_miles by 1.609 • Put the result in dist_in_kms • Output “The distance in kilometers is ” • Output dist_in_kms • End As you can see, creating a good algorithm requires substantial effort and attention to details
An Algorithm to List #s < 10 • Output “List of Numbers less than 10” • Put 1 in number • Output number • Add 1 to number • If number is less than 10, go back to step 3 • End Notice this algorithm has no inputs, only one variable, “number”
An Algorithm to List Odd #s < 10 • Output “List of Odd Numbers less than 10” • Put 1 in number • If number is even, skip to step 5 • Output number • Add 1 to number • If number is less than 10, go back to step 3 • End Is this an algorithm? Is step 3 sufficiently precise?
An Algorithm to List Odd #s < 10 • Output “List of Odd Numbers less than 10” • Put 1 in number • If number divided by 2 has remainder 0, go to step 5 • Output number • Add 1 to number • If number is less than 10, go back to step 3 • End Now we have precise instructions which can be followed!