1 / 19

Array in C++ / review

Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred to as array elements , can be addressed using a number, called index or subscript .

Télécharger la présentation

Array in C++ / review

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. Array in C++ / review

  2. An array contains multiple objects of identical types stored sequentially in memory. • The individual objects in an array, referred to as array elements, can be addressed using a number, called index or subscript. • The array is the most commonly used data storage structure. • It helps to see how object-oriented programming and data structures relate to each other.

  3. Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [ no of elements]; • You can create an array from any type with the exception of some special types. • An array always occupies a contiguous memory space. To know how much memory spaces an array needs, for example, if we have an array of 10 elements, this space is 10*sizeof(float) = 40 bytes.

  4. when declaring an array, there is a possibility to assign initial values to each one of its elements. • To access the values of an array; being able to both read and modify its value by using this format : • name[index]

  5. # include <iostream> # include <iomanip> using namespace std; int main() { int n[10]; for( inti=0;i<10;i++) n[i] =0; cout<<“Element”<<setw(13)<<“value”<<endl; for(int j=0; j<10;j++) cout<<setw(7)<<j<<setw(13)<<n[j]<<endl; return 0; }

  6. How to assign initial values to a char array : char name[ ] = “welcome back"; This definition is equivalent to char name[ ] = {‘w’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘ ‘ , ‘b’, ‘a’, ‘c’, ‘k’, ‘\0’};

  7. Multidimensional arrays • can be described as "arrays of arrays". • Multidimensional arrays are not limited to two indices. #define WIDTH 3 #define HEIGHT 2 int table [HEIGHT][WIDTH]; intn,m; int main () { for (n=0;n<HEIGHT; n++) for (m=0; m<WIDTH; m++) { table [n][m]=(n+1)*(m+1); } return 0; }

  8. Array Advantages: • Easier to declare and use. • Can be used with most of the data types. Array Disadvantages: • Fixed size data. • Ex: if the number of elements to be stored are more than the maximum size, the array cannot accommodate those new values.

  9. Functions/ review

  10. FUNCTIONS • If you are looking to provide a solution for a more complex problem, it will help to divide the problem into smaller units. • Functions can be defined in any order, however, the first function is normally main. • Definition syntax: type name ( parameter1, parameter2, ...) { statements }

  11. Prototype and Definition • In a function definition, the function header is similar in form to the prototype(declaration) of a function. • State the differences between the prototype and function declaration? • When do the function must be declare? • The prototype provides the compiler with all the information it needs when a function is called, what are these information and what will happen after that? • Can the function declaration be omitted ? When ?

  12. ■ RETURN VALUE OF FUNCTIONS • When a function is called, an argument of the same type as the parameter must be passed to the function for each parameter. • Note: the arguments can be also any kind of expressions. • If the function is any type other than void, the return statement will cause the function to return a value to the function that called it. • When the program flow reaches a return statement or the end of a function code block, where it goes?

  13. ■ PASSING ARGUMENTS • Passing values to a function when the function is called is referred to as passing by value, does it access the object it self directly? • However, function arguments can also be passed by reference.

  14. Other topics to review • Overloading function . • Inline function . • Recursion . • Default arguments.

  15. Any Question? • Refer to chapter 1 of the book for further reading

More Related