1 / 147

9/4/1435 h Sunday

This course introduces the students to the concepts of object-oriented programming language and arrays in computer programming. It covers topics such as constructors, destructors, functions, and operator overloading. The course also explores the use of arrays for different applications.

mechelleh
Download Presentation

9/4/1435 h Sunday

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. 9/4/1435 h Sunday Lecture 1 Course Contents & Arrays Terminologies

  2. Computer Programming (2)Code & No.: CS 104 CREDIT HOURS: 5 UNIT Lecture 3.0 hours/week Lab: 2.0 hour/on every week This course introduces the students to the concept of object oriented programming language Also deals with instructors, destructors and, functions & operators overloading The course also looks into Arrays in details as one of the applications ..

  3. Learning Objectives: • Identify the role of programming in our life by learning the principals and Concepts of OOP, • Demonstrate understanding of objects, classes • Learning the benefits of some special functions like constructors & destructors • References: Starting Out with C++ TONNY GADDIS Seventh Edition 

  4. Computer Programming (2)Code & No.: CS 104 Pre-requisite: CS 102 Level: 3rd Course Contents • Arrays • Introduction to object oriented programming language. • several C++ enhancements to C. • what classes, objects, member functions and data members are. • how destructors are used to perform termination.

  5. . • housekeeping on an object before it is destroyed . • what operator overloading is and how it makes programs more readable and programming more convenient. • To create classes by inheriting from existing classes and how inheritance promotes software reuse, the use of inheritance to customize existing software.

  6. . • what polymorphism, how it makes programming more convenient and how it makes systems more extensible and maintainable . • The formatted input/output and file processing • Some applications (Arrays, Queues, Linked Lists, and sorting algorithms. Text books: C ++ How to progrm H. M. Deitel, P. J. Deitel, 1998: Prentice Hall.

  7. Arrays

  8. Q: Define an array? An array is a variable that can store multiple values of the same type Q: Write a format for declaring a one dimensional array? EX: int tests[5]; <Data type> < Array Name > [ Array Size declarator] ;

  9. Q: Design an array - Memory Layout for the following array int tests[5]; allocates the following memory: Subscripts 0 1 2 3 4

  10. Array Terminologies Q: What is the difference between the array size declarator and the array size ? The array size declarator shows the number of elements in the array. The size of an array is the total number of bytes allocated for it . It is = (number of elements) * (number of bytes for each element)

  11. . Q: Calculate the size of these arrays? • int tests[5] is an array of 20 bytes, assuming 4 bytes for an int • long double measures[10] is an array of 80 bytes, assuming 8 bytes for a long double

  12. 13/4/1435 h Lecture 2 Accessing Array Elements

  13. Named Constants • Q: Mention the benefits of named constants when used as an array size declarator, give an example? • This eases program maintenance when the size of the array needs to be changed. • Ex: const int SIZE = 5; int tests[SIZE];

  14. Q: Define array’s subscripts? • Each element in an array is assigned a unique subscript. • Subscripts start at 0 • The last element’s subscript is n-1 where n is the number of elements in the array. subscripts:

  15. Q: How can we access array’s elements ? By using : • A constant or literal subscript: EX : • Integer expression as subscript: EX cout << tests[3] << endl; • int i = 5; • cout << tests[i] << endl;

  16. Q : Write a general Format for accessing array’s Elements Array Name [Array Subscript]; OR Array Name [integer Number];

  17. Q: Write a c++ program that asks for the number of hours worked by 6 employee and stores the values in an array #include<iostream> using namespace std; int main( ) { const int num_emp = 6; int hours[num_emp]; cout<<“enter the hours worked by 6 employees”;

  18. . // Get the hours worked by each employee cin>>hours[0]; cin>>hours[1]; cin>>hours[2]; cin>>hours[3]; cin>>hours[4]; cin>>hours[5]; //Display the values in the array Cout<<“the values you entered are:”

  19. . cout<< “ “ <<hours[0]; cout<< “ “ <<hours[1]; cout<< “ “ <<hours[2]; cout<< “ “ <<hours[3]; cout<< “ “ <<hours[4]; cout<< “ “ <<hours[5]<<endl; return 0; }

  20. Program output with example of input Here are the contents of the hours array, with the values entered by the user in the example output:

  21. (Program Continues)

  22. Here are the contents of the hours array, with the values entered by the user in the example output:

  23. 16/4/1435 hSunday Lecture 3 • Using a Loop to Step Through an array • Implicit Array Sizing • No Bounds Checking in arrays in C++

  24. Program 1 : Using loop concept write a c++ Program to enter 5 numbers and print them back in an array form #include<iostream> using namespace std; int main( ) { int N[5]; for(int i = 0; i<5; i++) cin>>N[i]; for(int j = 0; j<5; j++)

  25. . cout<<N[i]; system(“pause”); return 0; }

  26. Program 2 • : Write a code that defines an array, numbers, and assigns 99 to each element using for loop const int SIZE = 5; int numbers[SIZE];for (int i= 0; i< SIZE; i++) numbers[i] = 99;

  27. 2. Implicit Array SizingQ: What do we mean by implicit array sizing? It means that we can determine array size by the size of the initialization list: int quizzes[]={12,17,15,11};

  28. . 3. No Bounds Checking in arrays in C++

  29. Q:What do we mean by No Bounds Checking for arrays in C++? • When you use a value as an array subscript, C++ does not check it to make sure it is a valid subscript. • In other words, you can use subscripts that are beyond the bounds of the array.

  30. Q: Write a C++ Code that explains the concept of No Bound Checking, Then show how these values are set in a memory The following code defines a three-elements array, and then writes five values to it!`

  31. What the Code Does

  32. Q: Mention the affect of No Bounds Checking in C++ ? It can : • corrupt other memory locations. • crash program. • lock up computer.

  33. Q: When does an Off-By-One Errors occurs in arrays? • It happens when you use array subscripts that are off by one. • This can happen when you start subscripts at 1 rather than 0. Example: // This code has an off-by-one error. const int SIZE = 100; int numbers[SIZE]; for (int count = 1; count <= SIZE; count++) numbers[count] = 0;

  34. 20/4/1435 h Thursday Lecture 4 • Array Initialization • Default Array Initialization • Partial Array Initialization

  35. Q: How can array been initialized? Arrays can be initialized with an initialization list:The initialization list cannot exceed the array size. Example: const int SIZE = 5;int tests[SIZE] = {79,82,91,77,84};

  36. Default Initialization Q: How can global and local arrays be initialized by default? • Global array  all elements initialized to zero by default • Local array  all elements uninitialized by default

  37. Partial Array InitializationQ:What do we mean by partial array initialization? If an array is initialized with fewer initial values than the size declarator, the remaining elements will be set to 0: Q: Draw a memory layout for this array int numbers[7] = { 1, 2, 4, 8};

  38. Program 3Q:Write a C++ program that shows each month and the number of days in that month using initialization list #include<iostream> using namespace std; int main( ) { const int MONTH=12; int days[MONTH]={31, 28, 31, 30 31, 30, 31,31 30, 31,30.31};

  39. . for( int count =0; count<MONTH; count ++) { cout<< “month “ <<(count +1)<<“has”; cout<< days[count] << “ days.\n”; } system (“pause”); return 0; }

  40. .

  41. Program 4Write a c++ program that finds the Highest Value in an Array #include<iostream> using namespace std; int main( ) { const int size= 4; int numbers[size] ={70,80,67,34}; int count; int highest; highest = numbers[0];

  42. for (count = 0; count < SIZE; count++){ if (numbers[count] > highest) highest = numbers[count]; } cout<< highest system (“pause”); return 0; }

  43. 23/4/1435 h Sunday lecture 5

  44. Program 5 Write a c++ program that finds the Lowest Value in an Array #include<iostream> Using namespace std; int main( ) { const int size=4; Int numbers[size]={10,45,56,90}; int count; int lowest; lowest = numbers[0];

  45. - for (count = 0; count < SIZE; count++) { if (numbers[count] < lowest) lowest = numbers[count]; } cout<<lowest; system (“pause”); return 0; }

  46. Q: Define parallel arrays? • Parallel arrays: Are two or more arrays that contain related data • A subscript is used to relate arrays: elements at same subscript are related • Arrays may be of different types

  47. Program 6 Write a c++ program that displays: student ID, average and grade from three parallel arrays #include <iostream> using namespace std; int main() { const int SIZE = 5; // Array size int id[SIZE]; // student ID array double average[SIZE];//course average array char grade[SIZE];// course grade array

  48. Test 1 • Write a format for declaring an array ----------------------------------------------------------- • What does the array size declarator shows? ----------------------------------------------------------- • Design an array-memory layout for the following array definition • What is the benefit of using named constants as an array size declarator int tests[5];

  49. 23/4/1434 h SundayLecture 5 C++ Programs using Two-Dimensional Arrays (Processing)

  50. Q:Write a general format for declaring a two dimensional array <array type><array name>[no. of rows][no. of columns];

More Related