Arrays, Structs & Mappings

pragma solidity^0.4.0;

contract Group{

struct Person{
   string name;
   uint age;
}

Person[] person;

function addPerson(string _name,uint _age){

  person.push(Person(_name,_age));

}

function getAverageAge() returns (uint){

   uint total=0;
   uint length=person.length;

   for(uint i=0;i<length;i++){
       total+=person[i].age;
   }

   uint ave=total/length;
   return ave;

}
}

Hey. Nice challenge for my lunch break :slight_smile: Here’s my code…

pragma solidity ^0.4.0;

contract averageAge {
    
    struct Person {
        string name;
        uint age;
    }
    
    Person[] persons;
    
    function addPerson(string _name, uint _age) {
        persons.push(Person(_name, _age));
    }
    
    function getAverageAge() view returns(uint) {
        uint num = 0;
        uint totalAge = 0;
        uint numberOfPeople = persons.length;
        
        for(num; num < numberOfPeople; num++) {
            totalAge += persons[num].age;
        }
        return totalAge / numberOfPeople;
    }
    
}
1 Like

Here is my solution for Assignment 2

pragma solidity^0.4.0;

contract PeopleRegister {
    struct Person {
      string name;
      uint age;
    }
    Person[] people;
    
    function addPerson(string _name, uint _age) public {
        people.push(Person(_name, _age));
    }
    
    function getAverageAge() public view returns(uint) {
        uint averageAge;
        uint totalAge;
        uint arrayLength = people.length;
        for (uint i = 0; i < arrayLength; i++) {
          totalAge += people[i].age;
        }
        averageAge = totalAge / arrayLength;
        return averageAge;
    }
}

Danny1 good afternoon,

I think msg is solidity’ function, which sender is msg’s property.
So use msg.sender can retreivce ETH’S contract’s address.

1 Like

//Contract KeyLottery
//Imagine 10 Mans and 8 Girls want’s outing play , each Man have a motorcycle key
//8 girls need randon decide which motor is Taked to ride ,they have only one chance to match .
//So let’s design the contract

pragma solidity ^0.4.23;

contract KeyLottery{
    
    struct Key{
        string driver;
        uint carNumber;
    }
    
    Key[] Keys;
    mapping(address => uint ) GirlToCar;
    
    function setKey(string _driver,uint _carNumber) {
      //if Keys[] counts >10 then showmessage('Too many keys') else
      Keys.push(Key(_driver,_carNumber));    
    }
    
    function setGirl() {
       address lotteryGirl = msg.sender;
       //randon get Keys id
       uint i = 2;
       //check if the Girl(addr) is mapped to an id 
       GirlToCar[lotteryGirl] = i;
        
        
    }
    function getKey() view returns(string) {
        
       uint id = GirlToCar[msg.sender];
       return Keys[id-1].driver;
    }
    
}
1 Like

we can basically make a whole personal database connected to the right owner! on the blockchain. only with this video! cool 😎

pragma solidity ^0.4.0;

contract PersonContract{

struct Persons {
    string name;
    uint age;
}

Persons[] member; // arrray

function addPerson(string _name, uint _age){
    member.push(Persons(_name, _age));
}

function getAverageAge() view returns (uint){
    uint sumAge;
    for(uint i=0; i<member.length; i++)
    {
        sumAge += member[i].age;
        
    }
    return sumAge/member.length;
}    

}

1 Like
pragma solidity ^0.4.0;

contract Peoples{

	// Define what is needed to make a person
	struct Person{
		string name;
		uint age;
	}

	// make an array of persons
	Person[] persons;


	function addPerson(string _name, uint _age){
		// push the Person to the persons array
		persons.push(Person(_name, _age));
	}

	function getAverageAge() view returns (uint) {

		// define a var to hold the sum of all ages
		uint total;

		// iterate through the array and get the ages
		// WARNING: Cannot use <= as it could go forever using infinite resources (buffer overflow)
		for (uint i=0; i < persons.length; i++){
			total += persons[i].age;
		}
        
        // return average
		return (total / persons.length);
	}
}
1 Like

Assignment 2:

pragma solidity^0.4.0;

contract Assignemtn02b {
    
    struct Person {
        string name;
        uint age;
    }
    
    Person[] listPersons;
    
    
    function setPerson(string _name, uint _age) {
        listPersons.push(Person(_name, _age));
    }
    
    function getPerson() view returns (uint){
        
        uint sumAge = 0;
        uint count = 0;
        
        for(uint i=0; i < listPersons.length; i++){
            sumAge += listPersons[i].age;
            count++;
        }
        
        return sumAge / count;
    }
    
}
1 Like

My AverageAge code:

pragma solidity ^0.4.0;

contract PeopleContract {
struct  Person {
    string name;
    uint age;
}

Person[] peopleDB;
mapping (address => uint) socialSecure;

function addPerson(string _name, uint _age){
    address SSNumber = msg.sender;
    uint id = peopleDB.push(Person(_name, _age));
    socialSecure[SSNumber] = id;
}


function getAverageAge() view returns(uint){
      
    uint average = 0;
    
    for(uint i = 0; i < peopleDB.length; i++)
        average += peopleDB[i].age;
     
    if(i != 0)
        return (average/i);
    else
        return 0;
}
1 Like
pragma solidity ^0.4.0;

contract Person {
   
    struct PersonStruct{ 
        string name;
        uint age;
    }
   
    PersonStruct[] personArray;
    
    function addPerson(string _name, uint _age){
        personArray.push(PersonStruct(_name, _age));
    }
    
    function getWackyAverage() returns (uint){
        uint total = 0;
        for (uint i=0; i<personArray.length; i++){
            total += personArray[i].age;
        }
            
        return total/personArray.length;
    }
}
1 Like

Hi Filip,

Regarding our Persons contract…

Division rounds off when dividing using uint types, so our final average was not accurate. How would you correct our average to be more precise?

3 Likes

Hi,

Unfortunately there is not easy way to do decimal numbers in solidity due to the way the EVM works. So in this case we are stuck with integer numbers, even when we do division.

1 Like

that’s a shame Filip… makes me think less of the maturity of Ethereum… also makes me more interested in EOS as they use C++ I believe.

Assignment 2:
pragma solidity ^0.4.0;

contract Assignment2{

struct Person{
    string name;
    uint age;
}

Person[] people;

function addPerson(string _name, int _age) public returns(uint){
    require(keccak256(_name)!=keccak256("")&&_age>0,"no name");
    return people.push(Person(_name,uint(_age)));
}

function getPerson(uint _id) public view returns(string,uint){
    return (people[_id].name,people[_id].age);
}

function getAverageAge() public view returns(uint){
    uint avgAge=0;
    for(uint count=0;count<people.length;count++){
        avgAge+=uint(people[count].age);
    }
    return avgAge/people.length;
}

}

hi pilip,
I was checking the code for this topic on your github which is :

it is different than the video. There is event and you make the addDog function internal, that is why I cannot call the addDog function. Could you please explain why it is internal ?

Hi @icemduru,
Filip actually explains what internal means in the Inheritance & Visibility video, and events is covered in the next reading assignment as well as the Events video.

1 Like

Thanks a lot, I also realized that he explains these terms after I watched the following videos. I guess the code was updated for the following courses.

Shiny happy people. It worked but I made a plus one error on the first go and assigned i in my for loop to 1 instead of 0 :frowning:

pragma solidity ^0.4.0;

contract personContract {

struct Person{
    string name;
    uint age;

}

Person[] people;
mapping(address => uint) ownerToDog;
function addPerson(string _name, uint _age){
//address owner = msg.sender;
people.push(Person(_name, _age));
//ownerToDog[owner] = id;
}

function averageAge() returns(uint) {
uint aAge = 0;
for (uint i=0;i < people.length; i++){
aAge += people[i].age;
}
return aAge/people.length;
}

}