In real life, there is no such thing as second place. Either you are a winner, or you’re not.
--Your friends at LectureNotes

Note for Computer Programming 2 - CP2 By Pratibha kumari

  • Computer Programming 2 - CP2
  • Note
  • 2 Offline Downloads
  • Uploaded 8 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

ARRAY: Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Declaring Arrays: To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows – This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. Initializing Arrays: You can initialize an array in C either one by one or using a single statement as follows – The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ]. Accessing array element: An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example The above statement will take the 10th element from the array and assign the value to salary variable. The following example shows how to use all the three above mentioned concepts viz. declaration, assignment, and accessing arrays

Text from page-2

When the above code is compiled and executed, it produces the following result − Arrays in detail: Arrays are important to C and should need a lot more attention. The following important concepts related to array should be clear to a C programmer –

Text from page-3

1. Multidimensional Arrays: C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration – Two dimensional arrays: The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a twodimensional integer array of size [x][y], you would write something as follows – Where type can be any valid C data type and arrayName will be a valid C identifier. A twodimensional array can be considered as a table which will have x number of rows and y number of columns. A two-dimensional array a, which contains three rows and four columns can be shown as follows – Initializing Two-Dimensional Arrays: Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. Accessing Two-Dimensional Arrays: An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example –

Text from page-4

When the above code is compiled and executed, it produces the following result − 2. Passing an array to function: If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received. Similarly, you can pass multi-dimensional arrays as formal parameters. Way-1: Formal parameter as a pointer Way-2: Formal parameter as a sized array Way-3: Formal parameter as an unsized array Example: Now, consider the following function, which takes an array as an argument along with another argument and based on the passed arguments, it returns the average of the numbers passed through the array as follows –

Lecture Notes