Download
constants n.
Skip this Video
Loading SlideShow in 5 Seconds..
Constants PowerPoint Presentation

Constants

181 Vues Download Presentation
Télécharger la présentation

Constants

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Constants

  2. Constants • Constants are used to associate meaningful names with constants. • Very useful whenever you have any value that is repeated in program. • Allow quick and easy changing of a value that is used throughout code simply by changing the declaration. • Also, greatly improve program’s readability • Constants may belong to any of the data type. • Constants are like normal variables but only difference is that their values cannotbe modified in the program once they are defined fixed values.

  3. Declaring constants • We can define constants in a C program in the following ways. • By “#define” preprocessor directive • By “const” keyword

  4. #define directive • The #define directive is a preprocessor command. • It's followed by the name of the symbol /constant’s name being defined, e.g. TRUE. • Convention of using ALL CAPS for constants lets you easily identify constants versus variables in your code. • The symbol must be all one word.

  5. #define directive • Following the symbol is a space and then the value that the symbol represents.  • There is no equal sign Or a semi-colon • The preprocessor replaces the symbol with the value it represents.

  6. Declaring constants • Examples of constant declaration: • #define TRUE 1 • #define FALSE 0 • #define VOTING_AGE 18 • constfloat PI = 3.1415; //the value of π • #define PI 3.1415 Notice semicolon

  7. Constants - example #define SALES_TAX 0.15 // 15 % rate of sales tax • This defines a constant SALES_TAX to have the value 15 percent, • Easy to change if government changed sales tax rate later • This of course only works if the constant identifier  SALES_TAX  has been used throughout the program and its numeric equivalent has never been used.

  8. Practice • Write a program that calculates the area and circumference of a circle of a (user specified) diameter. • Program repeats execution until a user is done with it. • circumference = πx r • area = π x (r)2

  9. Arrays

  10. Array • To store average temperature of a given month, you can use a variable of type float. E.g. float monthly_temperature = 45.7; • How would you store average temperature of every month in a given year? Declare 12 float variables? • An array is a collection of same type of elements which are grouped with a common name. • Array is a collection of memory locations, each of which can store the same data type and which can be referenced / accessed through the same variable name

  11. Array – homogeneous elements • All elements of any given array must be of the same type • i.e. we CANNOThave an array of 10 numbers, of which 5 are ints and 5 are floats.

  12. Array • Array can be visualized as a row in a table, whose each successive block can be thought of as memory bytes containing one element. • An Array of four elements: Element 1 Element 2 Element 3 Element 4

  13. Array Declaration • As variable declaration, arrays must be declared before they can be used in the program. • Standard array declaration is as type variable_name[number_of_elements]; • E.g, the declaration float monthly_temperature[12]; //declares an array named monthly_temperature //that can hold 12 float numbers

  14. Indexing Arrays (accessing array elements) • The elements of the array occupy adjacent locations in memory. • In C, first element of an array is located at index 0, and the last element is located at index (array_size – 1) • E.g. 1st element in our example array is indexed as monthly_temperature[0] • and • 5thelement in our example array is indexed as monthly_temperature[4] • and • 12th (last) element in our example array is indexed as monthly_temperature[11]

  15. Array Initialization • Two ways of initializing: • All elements initialized at once • One element at a time • E.g. intmyArray[5] = {10, 20, 30, 40, 50}; // all elements at one intmyArray[5]; //array declaration myArray[0] = 10; //initialization of individual elements myArray[1] = 20; myArray[2] = 30; myArray[3] = 40; myArray[4] = 50;

  16. intmyArray[5] = {10, 20, 30, 40, 50}; … … myArray[0] myArray[1] myArray[2] myArray[3] … 10 20 30 40 50 myArray[4]

  17. Array access pitfalls • Knowing the start and end of an arrays’ elements’ indices • First element – index 0 • Last element – index (arraysize – 1) • Accessing memory locations outside arrays bounds • Unpredictable results

  18. Array Example – 1 • Write a program to ask the user average temperatures for the twelve months of a given year • Input 12 values from user and display contents of the array at the end • Also, display the address of every element of the array

  19. Array Example – 2 • Input the number of students in a class, and then input every student’s score in a quiz • Calculate class’s average score in the quiz and display the result

  20. Array Example – 3 • Copy contents of one array into another array

  21. Two-Dimensional Arrays

  22. Two-dimensional array • The arrays that we have seen so far are single-dimensional arrays. • E.g., if we had to store coordinates of a point in a two-dimensional plane (Cartesian coordinate system), we could use an array: • int point[2]={1, 1}; // coordinates of point (1, 1) • What if we had to store a collection of such points? • A two-dimensional array can be used for this purpose

  23. Two-dimensional array • Lets say we want to store coordinates of 5 such points • We need a two-dimensional array of size 5 x 2 • To declare this array : int points[5][2]; • To initialize this array with x- and y-coordinates of 5 points: points[5][2] = {1, 0, 1, 1, 1, 2, 2, 2, 3, 2};

  24. Two-dimensional array • int points[5][2] = {1, 0, 1, 1, 1, 2, 2, 2, 3, 2}; • To improve readability, we can initialize it as: int points[5][2] = { {1,0}, {1,1}, {1,2}, {2,2}, {3,2} };

  25. 2-D array Example 1 • Write a program that initializes and then traverses through a 6 x 4 matrix. Use nested for loops for traversal.

  26. 2-D array Example 2 • Write a program that asks user to input a series of coordinates, stores them in a 2-D array and outputs total distance travelled while traversing these points.