## ← total score for all students - Intro to Java Programming

• 2 Followers
• 44 Lines

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=cKy2qng7UDg" data-team="udacity"></div> ``` 5 Languages

Showing Revision 2 created 05/07/2015 by Udacity Robot.

1. I'm going to show you two ways to do this.
2. We know that for each student, we want to write
3. that student's total into an array and return it. We
4. also know exactly how big we want the array to
5. be because we want one for each student. So I
6. want numStudents items in my array. Now, for all of
7. the student from the 0 student to the last student
8. Going up one at a time. I want to fill that student's
9. spot in the array of totals with that
10. student's total. I already have a method that calculates
11. this. So, I can use totalForStudent. And then,
12. when I've done that for all of the students,
13. I can return the totals. Looks like I forgot to
14. call it totals. Let's check that it works. I'll run
15. the tester, and it looks like it's got what I
16. expecting. Now, we could use nested for loops. I'll still
17. want all of my totals, and I'll still want to
18. return them, but let's think about solving this problem for
19. just one student, for each topic, starting with the first
20. one. And we going up to the number of topics,
21. one at a time. I'll add the score
22. for that topic to that student's total, let's say
23. for now I'm thinking about student 1. To
24. calculate total for student 1, I'll start at 0
25. and for each topic And want to add
26. that student's grade, in that topic. Then, once I
27. have the student total in my larger array
28. totals, I'll set the total for that student to
29. be the student total that I just calculated.
30. So this is what I do for one student.
31. And now, I want to do that for every single
32. student. So that all of the spaces in total
33. gets filled, so I will indent that a little
34. bit and now for each student starting with the
35. zeroth one so we don't need this line anymore
36. I will go up to the number of students
37. one at a time. I will do all of these steps. So
38. now the inter-loop is calculating the student totals and the outer loop is
39. storing the student totals in totals. So now we're fix up syntax errors. And
40. this way should work just like the last one. I'll
41. run it to make sure and it looks like it's
42. still good. Hopefully the number of values you see here.
43. Makes it obvious why it would be be nice as
44. a teacher, have a computer do this for you.