C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable...
-
Upload
deborah-griffith -
Category
Documents
-
view
212 -
download
0
Transcript of C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable...
COMPUTER PROGRAMMING 1Assignment
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 becomes 12”. We could use any value instead of 12
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.
int feet, ins, metres;
orfloat feet, ins, metres;
Note we can define several variables in a single statement.
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()
{
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
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
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.
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()
{
int feet, inches ;
printf(“Enter quantity of feet: “);
scanf(“%d“, &feet ) ;
inches = feet * 12 ;
printf(“The number of inches is %d“, inches ) ;
}
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).
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()
{
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);
}
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.
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.