Hello Everyone, Welcome back to Rajasthan Polytechnic BTER. This is Garima Kanwar Welcome you all to Rajasthan Polytechnic BTER at where you will get notes of rajasthan polytechnic 1st semester, acnd cs branch 3rd and 5th semester.
Arrays are a fundamental data structure in programming that allow the storage of multiple values in a single variable. They provide a way to organize data efficiently and access it using indices.
4.1 Arrays and Memory Organization
An array is a collection of elements, all of the same type, stored in contiguous memory locations. This allows for efficient access and manipulation of data.
Key Characteristics of Arrays:
- Homogeneous Data: All elements in an array must be of the same data type (e.g., all integers or all floats).
- Fixed Size: The size of the array is defined at the time of declaration and cannot be changed during runtime.
- Indexed Access: Each element in an array can be accessed using an index, starting from 0 for the first element.
Memory Organization: When an array is declared, the system allocates a block of memory large enough to hold all the elements. For example, in a C-style declaration:
"C"int numbers[5];
This allocates space for 5 integers in memory. The address of the first element can be used to calculate the address of any other element using the formula:
"CSS"Address of A[i] = Base Address + (i * Size of each element)
Where Base Address
is the address of the first element, i
is the index, and Size of each element
is the size of the data type.
4.2 Strings
Strings are a special type of array that represent a sequence of characters. In most programming languages, strings are treated as arrays of characters terminated by a null character ('\0'
).
Declaration and Initialization: In C, a string can be declared as:
"C"char name[20] = "John Doe";
This allocates space for 20 characters, including the null terminator.
Common String Operations:
- String Length: The length of a string can be found using the
strlen()
function. - String Copy: Strings can be copied using the
strcpy()
function. - String Concatenation: Strings can be combined using the
strcat()
function. - String Comparison: Strings can be compared using the
strcmp()
function.
Example of String Usage:
"C"#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, " ");
strcat(str1, str2);
printf("%s\n", str1); // Output: Hello World
return 0;
}
4.3 Multidimensional Arrays
Multidimensional arrays are arrays of arrays, allowing the storage of data in more than one dimension. The most common type is the two-dimensional array, which can be visualized as a matrix.
Declaration and Initialization: A two-dimensional array can be declared as follows:
"C"int matrix[3][4]; // 3 rows and 4 columns
This allocates space for 12 integers.
Accessing Elements: Elements are accessed using two indices:
"C"matrix[0][1] = 5; // Assigns 5 to the element in the first row and second column
Example of a Two-Dimensional Array:
"C"#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 2 3 4 5 6
4.4 Functions and Parameter Passing
Functions are reusable blocks of code that perform a specific task. They can take inputs, known as parameters, and may return a value.
Function Declaration and Definition: A function is declared and defined as follows:
"C"return_type function_name(parameter_type parameter_name) {
// function body
}
Example of a Function:
"C"int add(int a, int b) {
return a + b;
}
Calling a Function: Functions are called by their name, and arguments are passed:
"C"int result = add(5, 3); // Calls the add function
Parameter Passing: There are two primary methods of passing parameters to functions:
Pass by Value: The function receives a copy of the argument's value. Changes made to the parameter within the function do not affect the original argument.
"C"void increment(int num) { num++; }
Pass by Reference: The function receives a reference (address) to the argument, allowing modifications to the original variable.
"C"void increment(int *num) { (*num)++; }
Example of Pass by Reference:
"C"#include <stdio.h>
void increment(int *num) {
(*num)++;
}
int main() {
int value = 10;
increment(&value); // Passing the address of value
printf("%d\n", value); // Output: 11
return 0;
}
This blog post is written by Garima Kanwar, and the blog name is 'Rajasthan Polytechnic BTER.' We hope this post has provided you with a clear understanding of arrays, strings, multidimensional arrays, and functions in programming. For more such detailed notes, please visit 'Rajasthan Polytechnic BTER' regularly.
If you want any other subject notes or any other study material please let me know in Comment Section.
0 Comments