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;
    }
}

#74

pragma solidity ^0.4.0;

contract AvgAge {

struct PeopleAges {
    string name;
    uint age;
}   

PeopleAges[]  people;

function newPerson(string _name, uint _age)  {
    people.push(PeopleAges(_name, _age));
}

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

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

}


#75

Hello Filip, I think the problem with de “addDog” function is that we only have access to the last dog I mean we can’t go through the map to have other dogs.


#76

I’m not sure what you mean exactly. Please explain what issue you are talking about. That should not be an issue with the addDog function, since it adds the dog into an array. But you can only get your own dogs of course. Not anyone else’s dogs.