Upload Arrays.txt
Browse files- Arrays.txt +88 -0
Arrays.txt
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// SPDX-License-Identifier: MIT
|
| 2 |
+
pragma solidity ^0.8.17;
|
| 3 |
+
|
| 4 |
+
contract ArraysExercise {
|
| 5 |
+
// Declare state variables to store arrays of numbers, timestamps, and senders
|
| 6 |
+
uint[] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Array of numbers initialized with values
|
| 7 |
+
uint[] timestamps; // Dynamic array to store timestamps
|
| 8 |
+
address[] senders; // Dynamic array to store sender addresses
|
| 9 |
+
|
| 10 |
+
uint256 constant Y2K = 946702800; // Constant representing the Unix timestamp for the year 2000
|
| 11 |
+
|
| 12 |
+
// Function to retrieve the array of numbers
|
| 13 |
+
function getNumbers() external view returns (uint[] memory) {
|
| 14 |
+
// Create a memory array to hold the numbers
|
| 15 |
+
uint[] memory results = new uint[](numbers.length);
|
| 16 |
+
|
| 17 |
+
// Copy the numbers from the state array to the memory array
|
| 18 |
+
for(uint i=0; i<numbers.length; i++) {
|
| 19 |
+
results[i] = numbers[i];
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// Return the memory array
|
| 23 |
+
return results;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// Function to reset the numbers array to its initial values
|
| 27 |
+
function resetNumbers() public {
|
| 28 |
+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Function to append new numbers to the numbers array
|
| 32 |
+
function appendToNumbers(uint[] calldata _toAppend) public {
|
| 33 |
+
// Iterate through the array to be appended
|
| 34 |
+
for (uint i = 0; i < _toAppend.length; i++) {
|
| 35 |
+
// Push each element of the array to be appended to the numbers array
|
| 36 |
+
numbers.push(_toAppend[i]);
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Function to save a timestamp along with the sender's address
|
| 41 |
+
function saveTimestamp(uint _unixTimestamp) public {
|
| 42 |
+
// Push the timestamp and sender's address to their respective arrays
|
| 43 |
+
timestamps.push(_unixTimestamp);
|
| 44 |
+
senders.push(msg.sender);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Function to retrieve timestamps and senders after the year 2000
|
| 48 |
+
function afterY2K() public view returns (uint256[] memory, address[] memory) {
|
| 49 |
+
// Initialize counter for timestamps after Y2K
|
| 50 |
+
uint256 counter = 0;
|
| 51 |
+
|
| 52 |
+
// Count the number of timestamps after Y2K
|
| 53 |
+
for (uint i = 0; i < timestamps.length; i++) {
|
| 54 |
+
if (timestamps[i] > Y2K) {
|
| 55 |
+
counter++;
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Initialize memory arrays to hold timestamps and senders after Y2K
|
| 60 |
+
uint256[] memory timestampsAfterY2K = new uint256[](counter);
|
| 61 |
+
address[] memory sendersAfterY2K = new address[](counter);
|
| 62 |
+
|
| 63 |
+
// Initialize index for inserting elements into memory arrays
|
| 64 |
+
uint256 index = 0;
|
| 65 |
+
|
| 66 |
+
// Iterate through timestamps and senders arrays to extract elements after Y2K
|
| 67 |
+
for (uint i = 0; i < timestamps.length; i++) {
|
| 68 |
+
if (timestamps[i] > Y2K) {
|
| 69 |
+
timestampsAfterY2K[index] = timestamps[i];
|
| 70 |
+
sendersAfterY2K[index] = senders[i];
|
| 71 |
+
index++;
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// Return timestamps and senders after Y2K
|
| 76 |
+
return (timestampsAfterY2K, sendersAfterY2K);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// Function to reset the senders array
|
| 80 |
+
function resetSenders() public {
|
| 81 |
+
delete senders;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// Function to reset the timestamps array
|
| 85 |
+
function resetTimestamps() public {
|
| 86 |
+
delete timestamps;
|
| 87 |
+
}
|
| 88 |
+
}
|