C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Computer Programming w/ Eng. Applications
Introduction to C Programming
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Basic Input/Output and Variables Ethan Cerami New York
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Scanf Reads in information from the keyboard Examples –scanf(“%d”, &number); –scanf(“%d%d”, &value1, &value2); WARNINGS! –Don’t forget the & (address of)
How to start Visual Studio 2008 or 2010 (command-line program)
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 C Programming Week 2 Variables, flow control and the Debugger.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
What Actions Do We Have Part 1
Formatted Input/Output
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
Formatted Input/Output
INPUT & OUTPUT scanf & printf.
Programming Funamental slides
1) C program development 2) Selection structure
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
Data Types and Maths Programming Guides.
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
Presentation transcript:

C OMPUTER P ROGRAMMING 1 Assignment

A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as assignment Assignment allows us to give a value to the variable directly in a program We may give the variable a constant value or use the value of other variables. For example, suppose we have a variable called feet which we wish to give the value 12. In C we write: feet = 12; This can be read as “feet is assigned the value 12” or “feet becomes 12”. We could use any value instead of 12

A SSIGNMENT Other examples of assigning values to variables might be: feet = 130; ins = 10; metres = 4; We must have declared the variable above before we can use them e.g. int feet, ins, metres; or float feet, ins, metres; Note we can define several variables in a single statement.

A SSIGNMENT : E XAMPLE Consider a program to convert feet to inches. A simple (and fairly useless) C program to do this might be: /* convert.c: converts feet to inches Author: Joe Carthy Date: 01/01/94 */ main() { int feet, inches; feet = 10; inches = feet * 12 ; printf(“The number of inches is %d \n“, inches ) ; } Executing this program produces as output: The number of inches is 120

A SSIGNMENT : E XAMPLE Pictorially, after execution of the second assignment statement, the variables in memory may be visualised as. Here we use the value of one variable ( feet ) to compute the value of another variable ( inches ). Memory 120inches 10feet

A SSIGNMENT Other examples of such an assignment are: pints = gallons * 8; cms = (km * ) + (m * 1000); where the values of variables on the right hand side are used to compute the values assigned to the variables on the left hand side of the assignment.

A SSIGNMENT : U SER I NPUT The program to convert feet to inches as presented above is very limited in that it always produces the same answer. It always converts 12 feet to inches. A better version would ask the user to enter the number of feet to be converted and display the appropriate result: /* convert2.c: convert feet to inches, Version 2 Author: Joe Carthy Date: 01/01/94 */ main() { int feet, inches ; printf(“Enter quantity of feet: “); scanf(“%d“, &feet ) ; inches = feet * 12 ; printf(“The number of inches is %d“, inches ) ; }

A SSIGNMENT : U SER I NPUT Executing this program produces as output: Enter quantity of feet: 4 The number of inches is 48 The scanf(“%d“, &feet) statement reads a whole number from the keyboard and stores it in the variable feet. The & character is vital and scanf() will not function properly without it. Yes it has a messy look to it, but unfortunately in the C programming language this is one of the common methods for reading such quantities. The %d tells scanf() to read a whole number; %f can be used to read a real number; %c to read a character and %s to read a string (containing no spaces e.g. a single word).

A SSIGNMENT : E XAMPLE 2 As another example of the use of I/O and variables consider a simple calculator program which prompts for two numbers, adds them and displays the sum: /* calc.c: calculator program Author: Joe Carthy Date: 01/01/94 */ main() { int num1, num2, sum; printf(“Enter first number: “); scanf(“%d”, &num1 ); printf(“Enter second number: “); scanf(“%d“, &num2 ); sum = num1 + num2 ; printf(“The sum of %d and %d is %d “, num1, num2, sum); }

A SSIGNMENT : E XAMPLE 2 Executing this program produces as output: Enter first number: 14 Enter second number: 10 The sum of 14 and 10 is 24 NOTE : in this program, we illustrate that a single printf() can display the value of a number of variables, in this case the values of three variables are displayed.

A SSIGNMENT : P ORTFOLIO E XERCISES The following programs should prompt the user to enter the required data and display an appropriate message that explains the output. You should use the float type where appropriate. It is used for real numbers – numbers with a decimal point. Write a program (WAP) to read the base and height of a triangle and display the area WAP to read the radius of a circle and display the area – take PI as WAP to read in an amount of Euros and convert it to Sterling – assume 1E = 0.85Stg For each program, draw memory maps showing the variables and the values they have at the end of executing the program.