object oriented programming spring - 2012
DESCRIPTION
COMSATS Institute of Information Technology. Object Oriented Programming Spring - 2012. Intro to Programming Kaleem Ullah [email protected]. Range of values. What is the largest integer value that can be expressed in 32 bits? How to calculate range? [0…(2 n – 1)] - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/1.jpg)
Object Oriented ProgrammingSpring - 2012
COMSATS Institute of Information Technology
Intro to Programming
Kaleem [email protected]
![Page 2: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/2.jpg)
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-bit computers• from 0 to 4,294,967,295
2
![Page 3: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/3.jpg)
Range of values
• For 32-bit computers• Integer range is from -2,147,483,648 to
2,147,483,647• Integer overflow and underflow
3
What will be the output?
int a=2147483647;cout<<++a;
int a=-2147483648;cout<<a--;cout<<" "<<a;
![Page 4: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/4.jpg)
Range of values
4
What will be the output?
int a=2147483647;cout<<++a; //-2147483648
int a=-2147483648;cout<<a--; //-2147483648cout<<" "<<a; //2147483647
![Page 5: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/5.jpg)
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 constant or 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
![Page 6: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/6.jpg)
6
Examplevoid main(){ // Declaring constants const int MIN_VALUE = 0; const int MAX_VALUE; // Error
MIN_VALUE = 45; // Error
cout << “MIN_VALUE is now “ << MIN_VALUE;
}
![Page 7: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/7.jpg)
7
Declaration of data items.• We need to declare data 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.
![Page 8: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/8.jpg)
8
Examplevoid main(){ // Declaring a constant. const float PI = 3.1416;
// Single variable declared at a time. int my_number; float GPA; char initial_letter;
// Can declare many data-items of the same type together. int height, base;}
![Page 9: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/9.jpg)
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
![Page 10: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/10.jpg)
10
Arrays
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all elements of this array have the same name, c) c[0]
c[1]c[2]c[3]
c[11]c[10]c[9]c[8]c[7]
c[5]c[4]
Position number of the element within array c
![Page 11: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/11.jpg)
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 variablesc[ 0 ] = 3;cout << c[ 0 ];
• Performing operations in subscript. If x = 3,c[ 5 – 2 ]or c[ 3 ]Or c[ x ]
![Page 12: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/12.jpg)
12
• Total size of an array in bytes?• Total bytes = number of bytes in type x number
of elements• e.g. int sample[10];
Arrays
![Page 13: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/13.jpg)
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 ];
![Page 14: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/14.jpg)
14
Getting Values
• int MyArray[5];cin>>MyArray[0];cin>>MyArray[1] ;cin>>MyArray[2] ;cin>>MyArray[3] ;cin>>MyArray[4] ;
• Orfor(int i = 0; i < 5; i++)
cin>>MyArray[i];
![Page 15: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/15.jpg)
15
Examples Using Arrays
• Initializersint n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements become 0
If too many initializers, a syntax error is generatedint 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
![Page 16: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/16.jpg)
16
1
2 // Initializing an array with a declaration3 #include <iostream>45 int main()6 {7 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };89 cout << "Element \t Value" << endl;1011 int i = 0;1213 while(i < 10)14 {15 cout<<i<<“\t”<<n[i];16 i++;17 }18192021 return 0;22}
Notice how they array is declared and elements referenced.
![Page 17: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/17.jpg)
17
int a[10], b[10];
// ...
a = b; // error – illegal
for(int i = 0; i < 10; i++)
a[i] = b[i]; //legal
Arrays
![Page 18: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/18.jpg)
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
![Page 19: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/19.jpg)
19
Finding Minimum and Maximumint main(){ int i, 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;}
![Page 20: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/20.jpg)
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
![Page 21: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/21.jpg)
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'
![Page 22: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/22.jpg)
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;
}
![Page 23: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/23.jpg)
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
![Page 24: Object Oriented Programming Spring - 2012](https://reader030.vdocuments.net/reader030/viewer/2022033108/56812ac3550346895d8e9204/html5/thumbnails/24.jpg)
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;
}