Java Array family program Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?Fastest way to determine if an integer's square root is an integerHow to append something to an array?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?Why is it faster to process a sorted array than an unsorted array?

Is a copyright notice with a non-existent name be invalid?

Why not use the yoke to control yaw, as well as pitch and roll?

.bashrc alias for a command with fixed second parameter

Understanding piped commands in GNU/Linux

Where did Ptolemy compare the Earth to the distance of fixed stars?

Is the Mordenkainen's Sword spell underpowered?

Sally's older brother

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Why are current probes so expensive?

How to ask rejected full-time candidates to apply to teach individual courses?

How does TikZ render an arc?

Why does BitLocker not use RSA?

Why do C and C++ allow the expression (int) + 4*5;

Can two people see the same photon?

How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?

What is "Lambda" in Heston's original paper on stochastic volatility models?

What criticisms of Wittgenstein's philosophy of language have been offered?

Was the pager message from Nick Fury to Captain Marvel unnecessary?

"Destructive power" carried by a B-52?

Random body shuffle every night—can we still function?

Is this Kuo-toa homebrew race balanced?

What was the last profitable war?

Weaponising the Grasp-at-a-Distance spell

How do Java 8 default methods hеlp with lambdas?



Java Array family program



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?Fastest way to determine if an integer's square root is an integerHow to append something to an array?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?Why is it faster to process a sorted array than an unsorted array?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















Need help, I need to design a program to read in a sequence of
how many children there are per family for a group of families.



The code should accomplish the following:



  • store number of children in an array,

  • calculate the average number of children per family, and

  • report how many families have an above average number of children.

Also, I need to add a second loop to find out how many families have less than the average number of children.



I am allowed to declare and initialize variables as needed, here is what I have so far:



int numFamilies;

Scanner scan = new Scanner (System.in);
System.out.print("For how many families will a child count be entered?");
numFamilies = scan.nextInt();

// create the array
int[] numChildren = new int[numFamilies];

// read in the number of children for each family and find the average
for (int i = 0; i < numChildren.length; i++)

System.out.print ("Enter number of children for family " + (i+1) +": ");
numChildren[i] = scan.nextInt();


// loop to find out how many families have less than the average
// number of children









share|improve this question
























  • For how many families would the user be entering data? Also, what number of children is considered above or below average?

    – Omari Celestine
    Mar 9 at 1:04











  • Your question is too broad. Please read this: meta.stackoverflow.com/questions/284236/… to understand why.

    – Stephen C
    Mar 9 at 2:28











  • As far as the number of families the values are read in from the user using the scanner so it could be any number entered. I was planning on using 2 for the average number of children.

    – Java The Hut
    Mar 9 at 2:37

















-1















Need help, I need to design a program to read in a sequence of
how many children there are per family for a group of families.



The code should accomplish the following:



  • store number of children in an array,

  • calculate the average number of children per family, and

  • report how many families have an above average number of children.

Also, I need to add a second loop to find out how many families have less than the average number of children.



I am allowed to declare and initialize variables as needed, here is what I have so far:



int numFamilies;

Scanner scan = new Scanner (System.in);
System.out.print("For how many families will a child count be entered?");
numFamilies = scan.nextInt();

// create the array
int[] numChildren = new int[numFamilies];

// read in the number of children for each family and find the average
for (int i = 0; i < numChildren.length; i++)

System.out.print ("Enter number of children for family " + (i+1) +": ");
numChildren[i] = scan.nextInt();


// loop to find out how many families have less than the average
// number of children









share|improve this question
























  • For how many families would the user be entering data? Also, what number of children is considered above or below average?

    – Omari Celestine
    Mar 9 at 1:04











  • Your question is too broad. Please read this: meta.stackoverflow.com/questions/284236/… to understand why.

    – Stephen C
    Mar 9 at 2:28











  • As far as the number of families the values are read in from the user using the scanner so it could be any number entered. I was planning on using 2 for the average number of children.

    – Java The Hut
    Mar 9 at 2:37













-1












-1








-1








Need help, I need to design a program to read in a sequence of
how many children there are per family for a group of families.



The code should accomplish the following:



  • store number of children in an array,

  • calculate the average number of children per family, and

  • report how many families have an above average number of children.

Also, I need to add a second loop to find out how many families have less than the average number of children.



I am allowed to declare and initialize variables as needed, here is what I have so far:



int numFamilies;

Scanner scan = new Scanner (System.in);
System.out.print("For how many families will a child count be entered?");
numFamilies = scan.nextInt();

// create the array
int[] numChildren = new int[numFamilies];

// read in the number of children for each family and find the average
for (int i = 0; i < numChildren.length; i++)

System.out.print ("Enter number of children for family " + (i+1) +": ");
numChildren[i] = scan.nextInt();


// loop to find out how many families have less than the average
// number of children









share|improve this question
















Need help, I need to design a program to read in a sequence of
how many children there are per family for a group of families.



The code should accomplish the following:



  • store number of children in an array,

  • calculate the average number of children per family, and

  • report how many families have an above average number of children.

Also, I need to add a second loop to find out how many families have less than the average number of children.



I am allowed to declare and initialize variables as needed, here is what I have so far:



int numFamilies;

Scanner scan = new Scanner (System.in);
System.out.print("For how many families will a child count be entered?");
numFamilies = scan.nextInt();

// create the array
int[] numChildren = new int[numFamilies];

// read in the number of children for each family and find the average
for (int i = 0; i < numChildren.length; i++)

System.out.print ("Enter number of children for family " + (i+1) +": ");
numChildren[i] = scan.nextInt();


// loop to find out how many families have less than the average
// number of children






java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 2:01









Ru Chern Chong

2,17362030




2,17362030










asked Mar 9 at 0:54









Java The HutJava The Hut

11




11












  • For how many families would the user be entering data? Also, what number of children is considered above or below average?

    – Omari Celestine
    Mar 9 at 1:04











  • Your question is too broad. Please read this: meta.stackoverflow.com/questions/284236/… to understand why.

    – Stephen C
    Mar 9 at 2:28











  • As far as the number of families the values are read in from the user using the scanner so it could be any number entered. I was planning on using 2 for the average number of children.

    – Java The Hut
    Mar 9 at 2:37

















  • For how many families would the user be entering data? Also, what number of children is considered above or below average?

    – Omari Celestine
    Mar 9 at 1:04











  • Your question is too broad. Please read this: meta.stackoverflow.com/questions/284236/… to understand why.

    – Stephen C
    Mar 9 at 2:28











  • As far as the number of families the values are read in from the user using the scanner so it could be any number entered. I was planning on using 2 for the average number of children.

    – Java The Hut
    Mar 9 at 2:37
















For how many families would the user be entering data? Also, what number of children is considered above or below average?

– Omari Celestine
Mar 9 at 1:04





For how many families would the user be entering data? Also, what number of children is considered above or below average?

– Omari Celestine
Mar 9 at 1:04













Your question is too broad. Please read this: meta.stackoverflow.com/questions/284236/… to understand why.

– Stephen C
Mar 9 at 2:28





Your question is too broad. Please read this: meta.stackoverflow.com/questions/284236/… to understand why.

– Stephen C
Mar 9 at 2:28













As far as the number of families the values are read in from the user using the scanner so it could be any number entered. I was planning on using 2 for the average number of children.

– Java The Hut
Mar 9 at 2:37





As far as the number of families the values are read in from the user using the scanner so it could be any number entered. I was planning on using 2 for the average number of children.

– Java The Hut
Mar 9 at 2:37












1 Answer
1






active

oldest

votes


















0














If you use Java8 or higher you can use java.util.Arrays(docs) class:



int sum = Arrays.stream(numChildren).sum();


If not you can iterate the array:



int sum = 0;
for (int children : numChildren)
sum += children;



For the average:



int average = numFamilies / sum;


Finally you can then iterate over the array and check your criteria:



for (int i = 0; i < numChildren.length; i++) 
String result = numChildren[i] >= sum ? "above or equal to" : "less than";
System.out.println("Family: " + (i+1) + " is " + result + " the average";



Notes:



  1. You need to add additional checks for division by zero etc

  2. You could use Double for better precision





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072937%2fjava-array-family-program%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you use Java8 or higher you can use java.util.Arrays(docs) class:



    int sum = Arrays.stream(numChildren).sum();


    If not you can iterate the array:



    int sum = 0;
    for (int children : numChildren)
    sum += children;



    For the average:



    int average = numFamilies / sum;


    Finally you can then iterate over the array and check your criteria:



    for (int i = 0; i < numChildren.length; i++) 
    String result = numChildren[i] >= sum ? "above or equal to" : "less than";
    System.out.println("Family: " + (i+1) + " is " + result + " the average";



    Notes:



    1. You need to add additional checks for division by zero etc

    2. You could use Double for better precision





    share|improve this answer



























      0














      If you use Java8 or higher you can use java.util.Arrays(docs) class:



      int sum = Arrays.stream(numChildren).sum();


      If not you can iterate the array:



      int sum = 0;
      for (int children : numChildren)
      sum += children;



      For the average:



      int average = numFamilies / sum;


      Finally you can then iterate over the array and check your criteria:



      for (int i = 0; i < numChildren.length; i++) 
      String result = numChildren[i] >= sum ? "above or equal to" : "less than";
      System.out.println("Family: " + (i+1) + " is " + result + " the average";



      Notes:



      1. You need to add additional checks for division by zero etc

      2. You could use Double for better precision





      share|improve this answer

























        0












        0








        0







        If you use Java8 or higher you can use java.util.Arrays(docs) class:



        int sum = Arrays.stream(numChildren).sum();


        If not you can iterate the array:



        int sum = 0;
        for (int children : numChildren)
        sum += children;



        For the average:



        int average = numFamilies / sum;


        Finally you can then iterate over the array and check your criteria:



        for (int i = 0; i < numChildren.length; i++) 
        String result = numChildren[i] >= sum ? "above or equal to" : "less than";
        System.out.println("Family: " + (i+1) + " is " + result + " the average";



        Notes:



        1. You need to add additional checks for division by zero etc

        2. You could use Double for better precision





        share|improve this answer













        If you use Java8 or higher you can use java.util.Arrays(docs) class:



        int sum = Arrays.stream(numChildren).sum();


        If not you can iterate the array:



        int sum = 0;
        for (int children : numChildren)
        sum += children;



        For the average:



        int average = numFamilies / sum;


        Finally you can then iterate over the array and check your criteria:



        for (int i = 0; i < numChildren.length; i++) 
        String result = numChildren[i] >= sum ? "above or equal to" : "less than";
        System.out.println("Family: " + (i+1) + " is " + result + " the average";



        Notes:



        1. You need to add additional checks for division by zero etc

        2. You could use Double for better precision






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 2:37









        Dimitris SkoufisDimitris Skoufis

        957




        957





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072937%2fjava-array-family-program%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved