Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2011 C++ Programming Language Lab # 3 Elementary Programming C++ Programming Language Lab # 3 Elementary Programming Objective: To be familiar with C++ Programming Language. Introduction: Identifier: -An identifier is a sequence of characters that consists of letters, digits, and underscores (_). -An identifier must start with a letter or an underscore. It cannot start with a digit. -An identifier cannot be a reserved word. Constants: -const data type CONSTANTNAME = VALUE; -ex:: const double PI = 3.14159; Sizeof Function: - To find the size of a type - cout << sizeof(int) << " " << sizeof(long) << " " << sizeof(double); Casting: perform standard conversion between basic data types . - double d = 4.5; - int i = static_cast<int>(d); // d is not changed Exercise : Find the result: #include <iostream.h> int main() { int sum = 1000; int count = 21; double average1 = sum/count; cout<<"Before conversion = "<<average1<<endl; double average2 = static_cast<double>(sum)/count; cout<<"After conversion = "<<average2<<endl; system("pause"); return 0; } 2 Character Data Type: #include <iostream> int main(){ using namespace std; char letter = 'A', b='3'; char ch = 'a'; cout<< ++ch<<endl; char c = 97; // Same as char c = (char)97; int i = '2' + '3'; // (int)'2' is 50 and (int)'3' is 51 cout << "i is " << i << endl; // i is decimal 101 int j = 2 + 'a'; // (int)'a' is 97 cout << "j is " << j << endl; cout << j << " is the ASCII code for character " << static_cast<char>(j) << endl; system("PAUSE"); return 0; } The result: Exercise : Find the result: #include <iostream> int main(){ int i ,j; // Variable Declaration with no value in memory i=5; // Variable Assigned a value j=2; float x,y ; x=i/j ; // Variable Assigned a value y=(float)i/j; j =3.4; //implicit type casting std::cout<< i << std::endl << j << std::endl << x << std::endl << y << std::endl; char ch; ch='A'; std::cout<< "the letter is "<<ch<<" the ASCCII code is "<< (int) ch<<std::endl; std::cout<<" the next letter is "<<++ch<<std::endl; std::cout<<ch+3<<std::endl; j='3'+'C'; std::cout<<"j ="<<j<<" it represent the character "<< (char)j<< std::endl; system("PAUSE"); return 0; } 3 Characters: Character Escape Sequence Name ASCII Code \b Backspace 8 \t Tab 9 \n Linefeed 10 \f Formfeed 12 \r Carriage Return 13 \\ Backslash 92 \' Single Quote 39 \" Double Quote 34 Exercise : write a program that print the following : And then re_write the first line ,,to be : The second part code: 4 Exercise : write a program that input a five digit number, separates the number into it's individual digits and prints the digits separated from one another by three spaces each. For example ,if the user types in 42339 the program should print : 4 2 3 3 9 Hint: Use the integer division and remainder operators Lab work: Apply the programming exercises practically on DEV C++ Program , and show the results to your instructor. 5 Home work: write a program that read an integer between 0—1000 & adds all the digits in the integer .for example if the number is 932 the sum is 14=9+3+2 Use the % operator to extract & use the / operator to remove the extracted digit. For example, 932 % 10 = 2, 932 / 10 = 93 what does the following code print? Cout <<"*\n**\n***\n****\n"; Write a program that calculates the squares and cubes of the numbers from 0 to 10 and uses tabs to print the following table of values: Number square cube 0 0 0 1 1 1 2 2 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 6