top of page

Algorithms, and example with arrays

  • a01337106
  • 10 feb 2015
  • 1 Min. de lectura

Imagine you want to store data of the same type, or that is going to be used for the same purpose. Or maybe even used in the same operation. You would use an array, let’s see how arrays are used in algorithms.

First, and array must be declared:

int a[];

The array will have a size of N

int N=10;

In this program we will sum all of the values inside de array a[]

First, a loop is needed for changing the index of the selected array.

Loop I from 0 to N-1

The loop is declared with the name I , the value of I is initialized in 0 and it will increase each time by one until it gets to the value of N-1. This is because when an array has a value of N, its indexes start from 0. So, we can now that an array with a size of 6 will have indexes from 0 to 5. For example:

N=5

A[0]

A[1]

A[2]

A[3]

A[4]

It contents five elements with indexes from 0 to N-1

With this been said we can continue, inside the loop we will declare the sum, for this an accumulator is needed. The value in the actual array (the value of I) will be summed each time.

int sum=sum+A[I];

Finally we end the loop

All together this program looks like this:

  1. int A[];

  2. int N=10;

  3. loop I from 0 to N-1

  4. int sum=sum+A[I];

  5. end loop

 
 
 

Comments


 THE ARTIFACT MANIFAST: 

 

This is a great space to write long text about your company and your services. You can use this space to go into a little more detail about your company. Talk about your team and what services you provide. Tell your visitors the story of how you came up with the idea for your business and what makes you different from your competitors. Make your company stand out and show your visitors who you are. Tip: Add your own image by double clicking the image and clicking Change Image.

 UPCOMING EVENTS: 

 

10/31/23:  Scandinavian Art Show

 

11/6/23:  Video Art Around The World

 

11/29/23:  Lecture: History of Art

 

12/1/23:  Installations 2023 Indie Film Festival

 FOLLOW THE ARTIFACT: 
  • Facebook B&W
  • Twitter B&W
  • Instagram B&W
 RECENT POSTS: 
 SEARCH BY TAGS: 

© 2023 by Karla Lugo. Proudly created with Wix.com

bottom of page