Skip to content

Instantly share code, notes, and snippets.

@renso3x
Created March 4, 2026 04:40
Show Gist options
  • Select an option

  • Save renso3x/fee93e820cca9f06562819cd7e1f8f27 to your computer and use it in GitHub Desktop.

Select an option

Save renso3x/fee93e820cca9f06562819cd7e1f8f27 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.31;
contract Activity_5 {
/**
Activity 1
Create a fixed-size array of length 3 called favoriteNumbers.
Add a function setNumber(uint index, uint value) that updates a number at a given index.
Add another function getNumber(uint index) that returns the value at that index.
*/
uint256[3] favoriteNumbers;
function setNumber(uint256 a, uint256 b, uint256 c) public {
favoriteNumbers[0] = a;
favoriteNumbers[1] = b;
favoriteNumbers[2] = c;
}
function getNumber(uint index) public view returns (uint) {
return favoriteNumbers[index];
}
/**
Activity 2: Dynamic Array Practice
Instructions:
Declare a dynamic array named scores.
Add a function addScore(uint value) to push new values.
Add a function removeLast() to pop the last value.
Add a function getScore(uint index) to get a score by position
Add a function getTotalScores() that returns scores.length.
*/
uint[] scores;
function addScores(uint value) public {
scores.push(value);
}
function removeLast() public {
scores.pop();
}
function getScore(uint index) public view returns (uint) {
return scores[index];
}
function getTotalScores() public view returns (uint sum) {
for (uint i=0; i < scores.length;i++) {
sum += scores[i];
}
return sum;
}
/**
Activity 3: Wallet Balances
Instructions:
Create a mapping mapping(address => uint) public balances;
Add a function deposit(uint amount) that increases msg.sender’s balance.
Add a function getBalance(address user) that returns their balance.
Add a function resetMyBalance() that resets your own balance to 0.
*/
mapping(address => uint) public balances;
function deposit(uint amount) public {
balances[msg.sender] += amount;
}
function getBalance(address user) public view returns (uint) {
return balances[user];
}
function resetMyBalance() public {
balances[msg.sender] = 0;
}
/**
Activity4: Struct + Array
Instructions:
Reuse the Student struct.
Declare Student[] public students
Add a function addStudent(string memory _name, uint _age, bool _enrolled) that pushes a new student to the array.
Add a function getStudent(uint index) that returns a specific student.
Add a function getTotalStudents() that returns the array length.
*/
struct Student {
string name;
uint age;
}
Student[] public students;
function addStudent(string memory name, uint age) public {
Student memory newStudent = Student({
name: name,
age: age
});
students.push(newStudent);
}
function getStudent(uint index) public view returns (string memory, uint) {
Student memory student = students[index];
return (student.name, student.age);
}
function getTotalStudents() public view returns (uint) {
return students.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment