NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Computer Programming w/ Eng. Applications
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Basic Input/Output and Variables Ethan Cerami New York
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming, an introduction to Pascal
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CMSC 104, Section 301, Fall Lecture 17, 11/04/02 Homework 4a and 4b Topics Go over Homework 4a Problems , Problems Go over.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Example # 1 Draw a flowchart for calculating the area of a room:
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Computer Programming for Engineers
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture2.
User-Written Functions
Chapter 2 More on Math More on Input
Variables, Expressions, and IO
Week 4 – Repetition Structures / Loops
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
1) C program development 2) Selection structure
Introduction to C Topics Compilation Using the gcc Compiler
A function with one argument
Chapter 2 - Introduction to C Programming
Lecture3.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Python Creating a calculator.
Presentation transcript:

NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement; /*body of code */ } Building programs by divide and conquer Length of a function One task per function End statement with ; Function has a return type Must have a main routine Use void to indicate no parameters passed Comments marked with /* */

NA2204.1jcmt CSE 1320 Intermediate Programming C Ins and Outs (Puts) Input commands scanf - formatting for any type of data getchar - read a single char gets - get a line of text ‘Readin’ Output commands printf - formatting for any type of data putchar - print a single char puts - put a line of text w‘Ritin’

NA2204.1jcmt CSE 1320 Intermediate Programming C Arithmetic (the third ‘R’) All the usual suspects ( ) : parentheses *, /, % : multiply, divide, remainder +, - : add, subtract Operators have precedence - all symbols have it No built-in power operator - pow(x) function

NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Read three integers from the user at the keyboard, sum them, take their average, and then print out all of the original numbers, the sum and the average with labels to the screen. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff

NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Read numbers What command to use? Syntax? scanf(“%d %d %d”, num1, num2, num3); But does the computer know what num1 is? Is it declared? Does the user know what to enter? Give them a clue.

NA2204.1jcmt CSE 1320 Intermediate Programming Our story so far … int main (void) {/* declarations */ int num1, num2, num3; /*body of code */ printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); }

NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) Now what? –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Sum the numbers What command? Syntax? sum = num1 + num2 + num3; But what is sum? Drat, another declaration. Notice that = means ‘assignment’ not equality. = is right side assigned to left

NA2204.1jcmt CSE 1320 Intermediate Programming Our story continues … int main (void) {int num1, num2, num3, sum; /*body of code */ printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); sum = num1 + num2 + num3; }

NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Take the average Avg is sum over number of - ergo, must find the sum first. avg = sum / 3; Declare avg. What are these things we’ve been declaring? What is a variable and why do we need it?

NA2204.1jcmt CSE 1320 Intermediate Programming Almost there… int main (void) {int num1, num2, num3, sum, avg; printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); sum = num1 + num2 + num3; avg = sum / 3; /* take input average */ }

NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Print stuff Need to print out what the user gave, then what we calculated. printf(“The inputs were %d, %d, %d”, num1, num2, num3); printf(“The sum is %d and the average is %d”, sum, avg); What are we going to realize about the average value?

NA2204.1jcmt CSE 1320 Intermediate Programming And now… int main (void) {int num1, num2, num3, sum, avg; printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); sum = num1 + num2 + num3; avg = sum / 3; /* take input average */ printf(“The inputs were %d, %d, %d”, num1, num2, num3); printf(“The sum is %d and the average is %d”, sum, avg); }

NA2204.1jcmt CSE 1320 Intermediate Programming Have I written a program to.. Do the actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Structure the progam? Correct name and parameters? All variables declared? All actions performed? Yes. IS IT DONE?

NA2204.1jcmt CSE 1320 Intermediate Programming No. Is it all correct? What about the average value? What could be changed? Anything else amiss? After the average value is fixed - is it done then? Maybe. Isn’t this fun!?! :)