Docsity

Arrays in C++: Definition, Initialization, and Examples, Slides of C programming

Partial preview of the text

Download Arrays in C++: Definition, Initialization, and Examples and more Slides C programming in PDF only on Docsity!

EEE 145

Programming in C++

Arrays

Motivation

  • (^) You may need to define many variables of the same

type.

  • (^) Defining so many variables one by one is cumbersome.
  • (^) Probably you would like to execute similar statements

on these variables.

  • (^) You wouldn't want to write the same statements over and

over for each variable.

Example #1 (cont'd)

  • (^) Soln: #include using namespace std; int main() { int i, sum=0, grade; float avg; for (i=0; i<3 0 0; i++) { cin>>grade; sum += grade; } avg = sum/3 0 0.0; cout<<"Average = " <<avg<<endl; return 0; } This was simple. Since we don't need to store all values, taking the sum is enough. So, we don't need an array.

Example

  • (^) Write a program that reads the grades of 3 0 0 students

in EEE145 and finds those that are below the average.

Example #2 (cont'd)

  • (^) Soln (^) #2: #include using namespace std; int main() { int i,sum,gr0,gr1,gr2,...,gr349; float avg; cin>>gr0; cin>>gr 1 ; cin>>gr 2 ; ... sum = gr0+gr1+gr2+...+gr349; avg = sum/3 0 0.0; if (gr0<ave) cout<<"Below avg: " << gr0 << endl; if (gr 1 <ave) cout<<"Below avg: " << gr1 << endl; if (gr 2 <ave) cout<<"Below avg: " << gr2 << endl; ... if (gr 299 <ave) cout<<"Below avg: " << gr 299 << endl; return 0; } You cannot skip these with "..." You have to repeat each of these statements 3 0 0 times. What is still wrong here?

Example #2 (cont'd)

  • (^) Soln^ #3: #include using namespace std; int main() { int i, sum=0, grade[3 0 0]; float avg; for (i=0; i<3 0 0; i++) { cin >> grade[i]; sum += grade[i]; } avg = sum/3 0 0.0; for (i=0; i<3 0 0; i++) if (grade[i]<avg) cout<<"Below avg: "<<grade[i]<<endl; return 0; } Defines an array consisting of 350 integer values. In the definition, the value in the brackets is the number of elements (size). This means the ith^ element of the array. Here, the value in the brackets is the index, not the size.

Arrays

  • (^) The index must of int type. int k[5]; k[k[4]/k[1]]=2; /* Correct as long as k[4]/k[1] is nonnegative/ k[1.5] = 3; / Error since 1.5 is not int */

Arrays

  • (^) The lower bound must be nonnegative.

float m[8]; int i;

m[-2] = 9.2; /* Syntax error */

i=-2;

m[i] = 9.2; /* Run-time error */

Initializing Arrays

  • (^) You may initialize an array during definition as

follows:

int array[5] = {10, 8, 36, 9, 13};

  • (^) However, you cannot perform such an initialization

after the definition, i.e.,

int array[5]; array = {10, 8, 36, 9, 13};

is syntactically wrong.

Initializing Arrays

  • (^) If the number of initializers is less than the size of the array:
    • (^) initialization starts by assigning the first value to the first element and continues as such,
    • (^) remaining elements are initialized to zero (even if the array was local)
  • (^) Eg: For the definition int array[5] = {10, 8, 36}; the first 3 elements get the values 10, 8, and 36, respectively. array[3] and array[4] become 0.

Example

  • (^) Read 100 integers and find the unbiased variance. #include using namespace std; int main() { int X[100], i; float avg=0,var=0; for (i=0; i<100; i++) { cin >> X[i]; avg += X[i]; } avg /= 100; for (i=0; i<100; i++) var += (X[i]-avg) * (X[i]-avg); var /= 99; cout<<"variance: " << var << endl;; return 0; } Unbiased variance of a sample is defined as 1 ( ) 1 2  ^   N X N i i^ 

Example

  • (^) Find the histogram of the scores in Midterm 1. #include using namespace std; int main() { int i, hist[101]={0}, score; for (i=0; i<3 0 0; i++) { cin >> score; hist[score]++; } for (i=0; i<101; i++) cout<<hist[i]<<" student(s) got ā€œ<< i <<endl; return 0; }

Arrays Have Fixed Size!

  • (^) The size of an array must be stated at compile time.
  • (^) This means you cannot define the size when you run the

program. You should fix it while writing the program.

This is a very serious limitation for arrays. Arrays are

not fit for dynamic programming.

  • (^) You should use pointers for this purpose.

Arrays Have Fixed Size!

  • (^) What you can do is to define very large arrays, making

the size practically infinite  Wastes too much memory.

  • (^) Your program may exceed the maximum memory limit

for the process.