0% found this document useful (0 votes)
12 views3 pages

Assign 3 - B

The document provides examples of accessing elements in an array, finding the length of an array, and using the push operation to add elements to an array in Solidity. Arrays can be declared and initialized, individual elements can be accessed by index, the length property returns the number of elements, and push adds new elements to the end of the array.

Uploaded by

Simran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Assign 3 - B

The document provides examples of accessing elements in an array, finding the length of an array, and using the push operation to add elements to an array in Solidity. Arrays can be declared and initialized, individual elements can be accessed by index, the length property returns the number of elements, and push adds new elements to the end of the array.

Uploaded by

Simran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Accessing Array Elements

// Solidity program to demonstrate


// accessing elements of an array
pragma solidity ^0.5.0;

// Creating a contract
contract Types {

// Declaring an array
uint[6] data;

// Defining function to
// assign values to array
function array_example(
) public payable returns (uint[6] memory){

data
= [uint(10), 20, 30, 40, 50, 60];
return data;
}

// Defining function to access


// values from the array
// from a specific index
function array_element(
) public payable returns (uint){
uint x = data[2];
return x;
}
}

Output:
2. Length of Array

// Solidity program to demonstrate

// how to find length of an array

pragma solidity ^0.5.0;

// Creating a contract

contract Types {

// Declaring an array

uint[6] data;

// Defining a function to

// assign values to an array

function array_example(

) public payable returns (uint[6] memory){

data = [uint(10), 20, 30, 40, 50, 60];

return data;

// Defining a function to

// find the length of the array

function array_length(

) public returns(uint) {

uint x = data.length;

return x;

Output:
3. Push

// Solidity program to demonstrate


// Push operation
pragma solidity ^0.5.0;

// Creating a contract
contract Types {

// Defining the array


uint[] data = [10, 20, 30, 40, 50];

// Defining the function to push


// values to the array
function array_push(
) public returns(uint[] memory){

data.push(60);
data.push(70);
data.push(80);

return data;
}
}

Output:

You might also like