You are given a block of memory (array of n bytes) which includes n-1 data bytes and the last byte as checksum.
Your task is to verify whether the last byte equals the XOR of all previous bytes (excluding itself).
Return:
Checksum: It’s a simple error-detection method that ensures data hasn’t been corrupted during storage or transmission.
The sender calculates based on specific algorithm and sends it with the data; the receiver recalculates it similarly from the received data—if both match, the data is intact, otherwise it’s likely corrupted.
Some methods used are
Example-1
Input: n = 5, mem = [10, 20, 30, 40, 60]
Output: 1(XOR = 10 ^ 20 ^ 30 ^ 40 = 60)
Example-2
Input: n = 4, mem = [5, 9, 1, 3]
Output: 0(XOR = 5 ^ 9 ^ 1 = 13, checksum = 3)
Example-3
Input: n = 3, mem = [7, 3, 4]
Output: 1(XOR = 7 ^ 3 = 4)
An array in C is a contiguous block of memory that stores multiple elements of the same data type. It allows you to:
Array Declaration
int numbers[10]; // Array of 10 integers
uint8_t buffer[32]; // 32-byte buffer (commonly used in firmware)
Declares a fixed-size array — memory is allocated statically.
Array Initialization
int values[4] = {10, 20, 30, 40}; // full initialization
int empty[4] = {0}; // all elements = 0
char message[] = "Hi"; // string-style initIf you skip the size (like message[ ]) , the compiler counts the size automatically.
Accessing Array Elements
int arr[3] = {5, 10, 15};
printf("%d", arr[1]); // prints 10
arr[2] = 20; // modify element at index 2The index starts from 0. Always ensure you stay within 0 to n-1.
Array Size with sizeof()
int arr[5];
int total_bytes = sizeof(arr); // e.g., 20 if int = 4 bytes
int element_bytes = sizeof(arr[0]); // gives size of int i.e. 4 bytes
int element_count = sizeof(arr) / sizeof(arr[0]); // gives 5This only works within the same scope where the array is declared (not when passed to a function).
int arr[4] = {10, 20, 30, 40};
Assuming the starting address is 0x2000, the memory looks like:
| Address | Value |
|---|---|
| 0x2000 | 10 |
| 0x2004 | 20 |
| 0x2008 | 30 |
| 0x200C | 40 |
Each int = 4 bytes (on most systems).
In embedded systems, arrays are used for: