Lecture 12 - Lecture 12 Oct 16, 02 Arrays Arrays 1 D Arrays...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 12 Oct 16, 02 Arrays Arrays 1 D Arrays 2D Arrays Multidimensional 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 1D array declaration Temperatures 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. */ 95.75 83 97.625 72.5 86.25 Other examples of 1D 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]; 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.... 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. 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/ 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; } #include<iostream.h> Example 1 (1D array input and output) 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; } Enter a temperature : 85 Enter a temperature : 90 Enter a temperature : 78 Enter a temperature : 75 Enter a temperature : 92 Output of example 1 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 0 1 2 3 4 . 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; } Example 2 ( Assigning values to a 1D array and finding their sum) #include<iostream.h> Example 2 output Enter a temperature: 85 Enter a temperature: 90 Enter a temperature: 78 Enter a temperature: 75 Enter a temperature: 92 The total of the temperatures 85 90 78 75 92 is 420 ...
View Full Document

This document was uploaded on 07/06/2011.

Ask a homework question - tutors are online