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

BC Prac 2

Bc Prac 2

Uploaded by

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

BC Prac 2

Bc Prac 2

Uploaded by

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

Experiment No.

Aim: Write a solidity smart contract using array and function in Remix IDE with
following functionalities:
o Prime number
o Palindrome
o Inheritance
o Performing Various on static and dynamic array

Lab Outcomes: To create and deploy smart contract using solidity and Remix IDE.

Theory:
Solidity - Solidity is an object-oriented, high-level language for implementing smart contracts.
Smart contracts are programs which govern the behavior of accounts within the Ethereum state.
Solidity is statically typed, supports inheritance, libraries and complex user-defined types among
other features.
Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain
declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and
Enum Types. Furthermore, contracts can inherit from other contracts.
There are also special kinds of contracts called libraries and interfaces.
Remix IDE - Remix IDE is an open-source web and desktop application. It encompasses sub-
projects including Remix Plugin Engine, Remix Libraries, and of course Remix IDE.
It requires no setup, fosters a fast development cycle and has a rich set of plugins with intuitive
GUIs. Remix is used for the entire journey of contract development with Solidity language as well
as a playground for learning and teaching Ethereum.

Array in Solidity

Arrays are data structures that store the fixed collection of elements of the same data types in which
each and every element has a specific location called index. Instead of creating numerous individual
variables of the same type, we just declare one array of the required size and store the elements in
the array and can be accessed using the index. In

Solidity, an array can be of fixed size or dynamic size.

Arrays have built in functions that allow you to add, update and delete information.

Syntax for creating an array:


<data type> <array name>[size] = <initialization>

Static Array

The size of the array should be predefined. The total number of elements should not exceed the size
of the array.
To declare a static array

<data type>[n] <array name>

where n= number of items you want in the array

Dynamic Array

The size of the array is not predefined when it is declared. As the elements are added the size of
array changes and at the runtime, the size of the array will be determined.

To declare a dynamic array

<data type>[] <array name>

Addition of elements in array

Push function is used when a new element is to be added in a dynamic array. The new element is
always added at the last position of the array.

Syntax:

<array name>. push(<element>)

Deletion of elements in array

Pop function is used when the last element of the array is to be removed in any dynamic array.

Syntax:

<array name>. pop ()

Finding Length of given array

Length of the array is used to check the number of elements present in an array. The size of the
memory array is fixed when they are declared, while in case the dynamic array is defined at runtime
so for manipulation length is required.

function array_length( ) public returns(uint) { uint x =


data.length;
return x;
}

Printing array elements in sorted order

Given an array of n strings. The task is to print the strings in sorted order. The
approach should be such that no string should be copied to another string during the
sorting process.
This is how the strings are compared and their corresponding indexes in the
indexed_arr are being manipulated/swapped so that after the sorting process is
completed, the order of indexes in the indexed_arr gives us the sorted order of the
strings.
Searching particular element in array
The elements of the array are accessed by using the index. If you want to access ith
element then you have to access (i-1)th index.
Create a function search (array, length of array, element to search). This search ()
will take three arguments - a dynamic uint type array, length of the array, element
that we need to search.
The search () will search an element in an array.
If element is found then it will return 1 otherwise 0. For Example - If array = [4, 2,
3, 4] search element = 2 then search () will return 1. If array = [1, 2, 3] search
element = 4 then search () will return 0.

Inheritance is a way to extend functionality of a contract. Solidity supports both


single as well as multiple inheritance

 A derived contract can access all non-private members including internal


methods and state variables. But using this is not allowed.

 Function overriding is allowed provided function signature remains same.


In case of difference of output parameters, compilation will fail.

 We can call a super contract's function using super keyword or using super
contract name.
 In case of multiple inheritance, function call using super gives preference
to most derived contract.
Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Base contract to handle array operations


contract ArrayOperations {
uint[] public dynamicArray; // Dynamic array
uint[3] public staticArray; // Static array of size 3

// Function to add a value to the dynamic array


function addToDynamicArray(uint _value) public {
dynamicArray.push(_value);
}

// Function to get the dynamic array


function getDynamicArray() public view returns (uint[] memory) {
return dynamicArray;
}

// Function to set the static array


function setStaticArray(uint[3] memory _array) public {
staticArray = _array;
}

// Function to get the static array


function getStaticArray() public view returns (uint[3] memory) {
return staticArray;
}
}

// Contract inheriting from ArrayOperations and adding more functionalities


contract MySmartContract is ArrayOperations {

// Function to check if a number is prime


function isPrime(uint _number) public pure returns (bool) {
if (_number <= 1) return false;
if (_number <= 3) return true;
if (_number % 2 == 0 || _number % 3 == 0) return false;
for (uint i = 5; i * i <= _number; i += 6) {
if (_number % i == 0 || _number % (i + 2) == 0) return false;
}
return true;
}
// Function to check if a string is a palindrome
function isPalindrome(string memory _str) public pure returns (bool) {
bytes memory strBytes = bytes(_str);
uint length = strBytes.length;
for (uint i = 0; i < length / 2; i++) {
if (strBytes[i] != strBytes[length - i - 1]) {
return false;
}
}
return true;
}
}

Outputs :
Observation and Findings:
In the experiment with the Solidity smart contract, key observations reveal that the contract
effectively manages both dynamic and static arrays, performs prime number and palindrome
checks, and demonstrates functional inheritance. The dynamic array grows as new elements
are added, while the static array remains fixed in size at three elements, reflecting the contract’s
ability to handle different types of array operations. The "isPrime" function correctly identifies
prime numbers, and the "isPalindrome" function accurately determines palindromic strings.
The contract handles inputs efficiently, though performance and error handling considerations
may arise with larger datasets or incorrect input sizes. Overall, the contract showcases
fundamental Solidity functionalities and serves as a practical example for managing arrays and
performing basic checks.

Conclusions:
The Solidity smart contract demonstrates essential functionalities by efficiently managing
dynamic and static arrays, accurately performing prime number and palindrome checks, and
illustrating functional inheritance. While the contract handles inputs effectively, considerations
for performance and error handling are necessary when dealing with larger datasets or incorrect
input sizes. Overall, this contract serves as a solid example of Solidity’s capabilities in array
management and basic computational tasks, offering valuable insights for further development
and optimization in smart contract design.

You might also like