1 / 24

Object Oriented Programming Spring - 2012

COMSATS Institute of Information Technology. Object Oriented Programming Spring - 2012. Intro to Programming Kaleem Ullah kaleemullah@ciitvehari.edu.pk. Range of values. What is the largest integer value that can be expressed in 32 bits? How to calculate range? [0…(2 n – 1)]

Download Presentation

Object Oriented Programming Spring - 2012

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. COMSATS Institute of Information Technology Object Oriented ProgrammingSpring - 2012 Intro to Programming KaleemUllah kaleemullah@ciitvehari.edu.pk

  2. Range of values • What is the largest integer value that can be expressed in 32 bits? • How to calculate range? • [0…(2n – 1)] • For 2 bits? • 8 bits? • For 32-bitcomputers • from 0 to 4,294,967,295

  3. Range of values • For 32-bitcomputers • Integer range is from -2,147,483,648 to 2,147,483,647 • Integer overflow and underflow What will be the output? int a=2147483647; cout<<++a; int a=-2147483648; cout<<a--; cout<<" "<<a;

  4. Range of values What will be the output? int a=2147483647; cout<<++a; //-2147483648 int a=-2147483648; cout<<a--; //-2147483648 cout<<" "<<a; //2147483647

  5. Data items • Store data used in program: • read in from user • constants used in program • A data item can be declared either as a constantor a variable • Constants are initialized with a value, but their value cannot be changed after that. • The value of a variable can be changed as needed. • The keyword const in the declaration indicates that the data item is a constant

  6. Example void main() { // Declaring constants const int MIN_VALUE = 0; const int MAX_VALUE; // Error MIN_VALUE = 45; // Error cout << “MIN_VALUE is now “ << MIN_VALUE; }

  7. Declaration of data items. • We need todeclaredata items in our program prior to using them. • The declaration tells: • whether the data item is a constant or a variable. • the identifier that will be used in the program to name the data item. • the data type for the data item.

  8. Example void main() { // Declaring a constant. const floatPI = 3.1416; // Single variable declared at a time. intmy_number; floatGPA; charinitial_letter; // Can declare many data-items of the same type together. int height, base; }

  9. Arrays • When we declare variables arbitrary memory locations are assigned to them • Array • Same type of elements at contiguous location • Same name and type • To refer to an element, specify • Array name and position number

  10. Arrays Name of array (Note that all elements of this array have the same name, c) c[0] -45 c[1] 6 c[2] 0 c[3] 72 c[4] 1543 c[5] -89 c[6] 0 c[7] 62 c[8] -3 c[9] 1 c[10] 6453 c[11] 78 Position number of the element within array c

  11. Arrays • Format: arrayname[ position number ] • First element at position 0 • n element array c: • c[ 0 ], c[ 1 ]…c[ n - 1 ] • Array elements are like normal variables • c[ 0 ] = 3; • cout << c[ 0 ]; • Performing operations in subscript. If x = 3, • c[ 5 – 2 ] • or c[ 3 ] • Or c[ x ]

  12. Arrays • Total size of an array in bytes? • Total bytes = number of bytes in type x number of elements • e.g. int sample[10];

  13. Declaring Arrays • Declaring arrays - specify: • Name • Type of array • Number of elements • Examples int c[ 10 ]; float hi[ 3284 ]; • Declaring multiple arrays of same type • Similar format as other variables • Example int b[ 100 ], x[ 27 ];

  14. Getting Values • int MyArray[5]; cin>>MyArray[0]; cin>>MyArray[1] ; cin>>MyArray[2] ; cin>>MyArray[3] ; cin>>MyArray[4] ; • Or for(int i = 0; i < 5; i++) cin>>MyArray[i];

  15. Examples Using Arrays • Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; • If not enough initializers, rightmost elements become 0 • If too many initializers, a syntax error is generated int n[ 5 ] = { 0 }; • Sets all the elements to 0 • If size omitted, the initializers determine it int n[] = { 1, 2, 3, 4, 5 }; • 5 initializers, therefore n is a 5 element array

  16. 1 2 // Initializing an array with a declaration Notice how they array is declared and elements referenced. 3 #include <iostream> 4 5 int main() 6 { 7 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 8 9 cout << "Element \t Value" << endl; 10 11 int i = 0; 12 13 while(i < 10) 14 { 15 cout<<i<<“\t”<<n[i]; 16 i++; 17 } 18 19 20 21 return 0; 22 }

  17. Arrays int a[10], b[10]; // ... a = b; // error – illegal for(int i = 0; i < 10; i++) a[i] = b[i]; //legal

  18. Write a program to input an array of size 10 from the user and • Find and print the minimum value in array • Find and print the maximum value in array

  19. Finding Minimum and Maximum int main() { inti, min_value, max_value, list[10]; for(i=0; i<10; i++) cin>>list[i]; // find minimum value min_value = list[0]; for(i=0; i<10; i++) if(min_value>list[i]) min_value = list[i]; cout << "\nminimum value: " << min_value << '\n'; // find maximum value max_value = list[0]; for(i=0; i<10; i++) if(max_value<list[i]) max_value = list[i]; cout << "maximum value: " << max_value << '\n'; return 0; }

  20. Strings • One dimensional arrays is used to create character strings • In C++, a string is defined as a character array that is terminated by a null • A null is specified using ‘\0’ • Because of the null terminator, it is necessary to declare a string to be one character longer

  21. Strings • Arrays of characters • All strings end with null ('\0') • Examples • char string1[] = "hello"; • Null character implicitly added • string1 has 6 elements • char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0’ }; • Subscripting is the same • string1[ 0 ] is 'h‘ • string1[ 2 ] is 'l'

  22. Reading strings from keyboard • The easiest way to read a string entered from the keyboard is to make a character array int main() { char str[80]; cout << "Enter a string: "; cin >> str; // read string from keyboard cout << "Here is your string: "; cout << str<<"\n"; return 0; }

  23. String • Input from keyboard char string2[ 10 ]; cin >> string2; • Puts user input in string • Stops at first whitespace character • Adds null character • If too much text entered, data written beyond array • Printing strings • cout << string2 << endl; • Does not work for other array types • Characters printed until null found

  24. Reading strings from keyboard There is a problem with previous program, if the string has whitespace characters // Using gets() to read a string from the keyboard. int main() { char str[80]; cout << "Enter a string: "; gets(str); // read a string from the keyboard cout << "Here is your string: "; cout << str<<“\n”; return 0; }

More Related