Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 12 Oct 16, 02.

Similar presentations


Presentation on theme: "Lecture 12 Oct 16, 02."— Presentation transcript:

1 Lecture 12 Oct 16, 02

2 Arrays

3 1- D arrays A one – dimensional array or a vector is a list of related values with the same data type that is stored using a single group name. The group name is referred to as the array name

4 1-D array declaration Temperatures 95.75
float temp[5]; // array declaration /* The array named temp has storage reserved for 5 floating point numbers in the computer memory. */ /* Each value in an array is called an element of the array. */ /*Each value in the table is an element of the array named temp which is declared above. */ Temperatures 95.75 83 97.625 72.5 86.25

5 Other examples of 1-D array declarations
int volt[10]; /* declares an array named volt and had storage reserved for 10 integer values. const int size=4; char code[size]; float amount[100];

6 Accessing array elements
Elements in an array are stored sequentially in computer memory. Any individual element can be accessed by giving the name of the array and the elements position. This position is called elements index or subscript value. The first element has an index of 0. Second element has an index 1 and third 2 and so on….

7 Accessing array elements contd…
eg: Given the declaration, float temp[5]; // array declaration //temp[0] refers to first temperature stored in temp array //temp[1] refers to second temperature stored in temp array. //temp[2] refers to third temperrature stored in temp array. //temp[3] refers to fourth temperature stored in temp array. //temp[4] refers to fifth temperature stored in temp array.

8 Assigning values to arrays
float sum; // variable declaration float temp[5]; // array declaration // assigning values below temp[0] = 95.75; temp[1] = temp[0] -11; temp[2] = 5.0 * temp[0]; temp[3] = 79.0; temp[4] = (temp[1] + temp[2] – 3.1 ) / 2.2; sum = temp[0] + temp[1] + temp[2] + temp[3]+temp[4]; // sum is a variable which holds a single floating point value/

9 Assigning values to arrays contd..
float temp[10]; float sum = 0; int i; for (i=0; i<5; i++) { sum = sum + temp[i]; cout<<temp[i]<<sum; }

10 Example 1 (1-D array input and output)
#include<iostream.h> int main() { int i, temp[5] ; // array declaration for(i=0; i<5; i++) // assigning values to an array cout<<“Enter a temperature:”; cin >> temp[i]; } cout<<endl; // dispaying values of the array for( i=0; i<5;i++) cout<<“temperature “<<i<<“is”<<temp[i]<<endl; return 0;

11 Output of example 1 Enter a temperature : 85 Enter a temperature : 90
temperature 0 is 85 temperature 1 is 90 temperature 2 is 78 temperature 3 is 75 temperature 4 is 92 // Note that the there are 5 elements with index values of

12 Example 2 ( Assigning values to a 1-D array and finding their sum)
#include<iostream.h> int main() { int i, temp[100], total =0; // array declaration for(i=0; i<5 ; i++) // assigning values for array cout<<“Enter a temperature:”<<endl; cin>> temp[i]; } cout <<“\nThe total of the temperatures”; for(i=0;i<5<i++) // summing arrays values and displaying cout<<“ “<<temp[i]; total = total + temp[i]; cout <<“ is “<<total<<endl; return 0;

13 Enter a temperature: 85 Example 2 output Enter a temperature: 90
The total of the temperatures is 420


Download ppt "Lecture 12 Oct 16, 02."

Similar presentations


Ads by Google