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;
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
add a comment |
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
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
add a comment |
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
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
java arrays
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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:
- You need to add additional checks for division by zero etc
- You could use
Double
for better precision
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- You need to add additional checks for division by zero etc
- You could use
Double
for better precision
add a comment |
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:
- You need to add additional checks for division by zero etc
- You could use
Double
for better precision
add a comment |
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:
- You need to add additional checks for division by zero etc
- You could use
Double
for better precision
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:
- You need to add additional checks for division by zero etc
- You could use
Double
for better precision
answered Mar 9 at 2:37
Dimitris SkoufisDimitris Skoufis
957
957
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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