0% found this document useful (0 votes)
45 views2 pages

005 An Algorithm A Day

The document provides an example algorithm to add up the numbers in an array and display the result. It specifies an array called "numbers" with values [2, 5, 3, 6, 2, 3, 6, 4]. It outlines the logic of declaring a variable to hold the total, using a loop to iterate through the array items and add each value to the running total, and printing the total after the loop completes. An example algorithm is given that initializes total to 0, uses a for loop from 0 to the array length minus 1 to add each array element to the total, and prints the total.

Uploaded by

Charlie Rogers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

005 An Algorithm A Day

The document provides an example algorithm to add up the numbers in an array and display the result. It specifies an array called "numbers" with values [2, 5, 3, 6, 2, 3, 6, 4]. It outlines the logic of declaring a variable to hold the total, using a loop to iterate through the array items and add each value to the running total, and printing the total after the loop completes. An example algorithm is given that initializes total to 0, uses a for loop from 0 to the array length minus 1 to add each array element to the total, and prints the total.

Uploaded by

Charlie Rogers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

005 An algorithm a day…

Algorithm Question
Consider the array numbers = [2, 5, 3, 6, 2, 3, 6, 4] where the value of numbers[2] is 3.

Write an algorithm which:


- Adds up the numbers in the array
- Displays the result.

To get full marks a loop should be used in your algorithm.


[5 marks]
Algorithm Example Answer
Consider the array numbers = [2, 5, 3, 6, 2, 3, 6, 4] where the value of numbers[2] is 3.

Write an algorithm which:


- Adds up the numbers in the array
- Displays the result.

To get full marks a loop should be used in your algorithm.


[5 marks]

***There are always different ways to solve a problem. This algorithm is just an example.
What is important is that the logic is correct!***

LOGIC:

- A variable to hold the total (running total) should be declared


- A loop should be used which will run for the same number of times as there are
items in the array
- Inside the loop the next array item should accessed using the counter as the
index…
- …and it should be added to the total
- At the end of the loop, the total should be outputted to the screen

EXAMPLE ALGORITHM:

total = 0
for counter = 0 to (numbers.length – 1)
total = total + numbers(counter)
print(total)

You might also like