Arrays, Structs & Mappings

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

}

1 Like

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

}

1 Like

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

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)

Deploy button does the same thing. Use that one.

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

}

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

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

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

}

1 Like

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.

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.

This is for quiz S.4.7 Structs C++
#include

using namespace std;

/*
*
*/
//struct must end with;

struct Website{
string name;
int money;
string advertisement;
};

void printPersonInfo(Website w){
cout << "The name of the company is: "<<w.name<<endl;
cout << "The money spent is: "<<w.money<<endl;
cout << "The advertisement ran for: "<<w.advertisement<<endl;
}

int main(int argc, char** argv) {

/*represent a person
 * 1.name
 * 2.age
 * 3.gender
 * */
//zzz is the varible
Website zzz;
zzz.name = "Ikea";
zzz.money = 100000;
zzz.advertisement = "21 days";
//most use variable to print Info
printPersonInfo(zzz);

return 0;

}

Hi guys.

The new videos are fantastic!

Sorry if I missed this answer in somewhere else, but I have one question in the mapping lesson.

As we are creating people, I create 3 people as an example. So, how can I visualize those 3 people that I have created? Once I use the getPerson function it returns the last person I have added only.

How would it work in real life in a situation where I am selling a tokenized asset and I need to keep a control over who(addresses) have purchased in terms of quantity, date, etc?

Thanks for your support.

1 Like

Patience, my friend. The answers to that question is explained in the next videos.