Arrays, Structs & Mappings


#66

assignment Person

pragma solidity^0.4.0;

contract Persons{

struct Person{
    string name;
    uint age;
}
Person[] man;

function addPerson(string _name, uint _age)public {
    man.push(Person(_name,_age));
}
function getAverageAge()view public returns(uint){
    uint len = man.length;
    uint sum = 0;
    for(uint i = 0; i < len; ++i){
        sum += man[i].age;
    }
    return sum/len;
}

}


#67

My solution is slightly different.In my Solution I the getter returns Average Age, Number of people and Sum of Age.
I did not encrypt (use the mapping function) the person object as there is no way bring back the elements without storing the address in the code.

pragma solidity 0.4.24 ;

contract PersonAverage{

struct Person {
string Name;
uint Age;
}

Person[] People;

function SetPerson(string _Name, uint _Age){
    
    People.push(Person(_Name,_Age));
}

function GetAverageAge() view returns (uint AverageAge, uint NumberOfPeople,uint SumOfAge)
{
    NumberOfPeople = People.length;
    for(uint i = 0;i < NumberOfPeople; i++){
    SumOfAge += People[i].Age;
    }
    
AverageAge = SumOfAge/NumberOfPeople;
return (AverageAge, NumberOfPeople, SumOfAge);
}

}


#68

Hello there,

I need help. Every time I try to run a program in Remix IDE the Create button is not coming up, the only button that is showing up is Deploy ( Please see attached print screen) and on the right side column of the page Configure is showing under Browser could this be the reason why I am unable to see Create button after I click RUN? Please see attached print screen


#69

I have changed the Environment to Java Script VM but still the Create button is not coming up every time I click RUN (Please see attached print screen)


#70

Deploy button does the same thing. Use that one.


#71

pragma solidity ^0.4.0;

contract PersonContract{

struct Person {
    string name;
    uint age;
}

Person[] People;

    // setter function 
function addPerson(string _cname, uint _age) {
   People.push(Person(_cname, _age));
}

// getter function that returns Average Age over all the persons in the array People
function getAverageAge() returns (uint){
    
    uint numOfPersons = 0;
    uint totalAge = 0;
    
    for(uint i = 0; i < People.length; i++ ) {
        totalAge += People[i].age;
        numOfPersons++;
    }
    
    return totalAge/numOfPersons;
}

}


#72
pragma solidity ^0.4.0;

contract Assignment2{
    
    uint average = 0;
    uint total = 0;
    
    struct Person {
        string name;
        uint age;
    }
    
    Person[] individual;
    
    function addPerson(string _name, uint _age){
        individual.push(Person(_name, _age));
        }
        
    function showAverage() view returns (uint) {
          for (uint i = 0; i < individual.length; i++) {
            total = total + individual[i].age;
            average = total/individual.length;
          }
       return average;
       }
}

#73

My answer…

pragma solidity ^0.4.25;

contract PersonInfo{

struct Persons {
    string name;
    uint age;
}

uint ageAverage;

Persons[] people;

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

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