PPS_9_1D and 2D Arrays.pptx

Introduction to Arrays

  • Lecturer: Pratidnya S. Hegde Patil, Assistant Professor-IT

What is an Array?

  • An array is a collection of elements of the same data type, referenced by a common name.

  • It is considered an aggregate or derived data type compared to basic data types (int, float, char).

  • All elements of an array occupy contiguous memory locations.

Declaration of Arrays

  • Basic Declaration Example:

    • Without arrays:

      int studMark0, studMark1, studMark2, ..., studMark999;
    • With arrays:

      int studMark[1000];
  • This reserves 1000 contiguous memory locations for storing student marks.

  • Simplifies declaration and manipulation of data.

One Dimensional Arrays

Declaration Format

  • The declaration of a one-dimensional array follows this syntax:

    array_element_data_type array_name[array_size];

Example Declarations

  • Examples of one-dimensional array declarations:

    • int xNum[20], yNum[50];

    • float fPrice[10], fYield;

    • char chLetter[70]; (for strings, does not count null terminator)

Array Initialization

  • Can be initialized at declaration:

    • Example: int idNum[7] = {1, 2, 3, 4, 5, 6, 7};

  • For strings: char chVowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};

    • Each character requires a null terminator.

  • Compact declaration:

    • char chVowel[6] = "aeiou";

    • Automatically adds null terminator.

Contiguous Memory and Size Calculation

  • For 2D arrays, total size is calculated using the formula:

    • First index x second index (e.g., Name[6][10] = 60)

  • 3D arrays follow the same principle.

Program Examples

Example 1: Sum of Array Elements

void main() {
    int arr[40], i, n;
    cout<< "Enter total number of numbers ";
    cin>>n;
    cout<< "Enter the elements ";
    for(i=0;i<n;i++) cin>>arr[i];
    for(i=0,sum=0;i<n;i++) sum+=arr[i];
    cout<< "Sum of " << n << " numbers is = " << sum << endl;
    cout<< "Average is " << (float)sum/n << endl;
}

Example 2: Find Smallest Value in Array

Example 3: Find Largest Value

Example 4: Reverse an Array

void main() {
    int my_arr[40], i, n;
    cout<< "Enter total number of numbers ";
    cin>>n;
    cout<< "Enter the elements ";
    for(i=0;i<n;i++) cin>>my_arr[i];
    cout<< "Printing the reverse of the array…\n";
    for(i=n-1;i>=0;i--) cout<< "\t" << my_arr[i];
}

Example 5: Storing and Reading a String

  • One word:

void main() {
    char str[40];
    cout << "Enter a word ";
    cin >> str;
    cout << "Printing string: " << str << endl;
}
  • Multiple words: using a loop to read until a newline.

Example 6: Input and Read Array Content and Index

Example 7: Assigning and Printing 2D Array Content

void main() {
    int i, j, arr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
    for(i=0; i<3; i++) {
        for(j=0; j<4; j++)
            cout << "intArray[" << i << "][" << j << "] = " << arr[i][j];
        cout << "\n";
    }
}

Example 8: Swapping Indexes in a 2D Array

Example 9: Reading Strings in Rows

/

Conclusion

  • Arrays are a powerful tool for managing related data of the same type, simplifying both data manipulation and organization in programming.