1 / 11

Computer Programming 1

Computer Programming 1. Assignment. Assignment. 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

Download Presentation

Computer Programming 1

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. Computer Programming 1 Assignment

  2. Assignment • 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 becomes12”. • We could use any value instead of 12

  3. Assignment • 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. intfeet, ins, metres; or float feet, ins, metres; • Note we can define several variables in a single statement.

  4. Assignment: Example • 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() { intfeet, 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

  5. Assignment: Example • 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 120 inches 10 feet

  6. Assignment • Other examples of such an assignment are: pints = gallons * 8; cms= (km * 100000) + (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.

  7. Assignment: User Input • 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() { intfeet, inches ; printf(“Enter quantity of feet: “); scanf(“%d“, &feet ) ; inches = feet * 12 ; printf(“The number of inches is %d“, inches ) ; }

  8. Assignment: User Input • 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).

  9. Assignment: Example 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() { intnum1, 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); }

  10. Assignment: Example 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.

  11. Assignment: Portfolio Exercises • 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 3.1417 • 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.

More Related