It might be a normal end of data condition It might indicate something abnormal

It might be a normal end of data condition it might

This preview shows page 15 - 24 out of 30 pages.

It might be a normal end-of-data condition It might indicate something abnormal If there’s no data but the internal counter says there is data, then it’s definitely abnormal. Regardless, this function is supposed to return something and you don’t have something to return. How to handle it? Throw an exception and let the other developer decide whether to ignore it or to sound red alert
Image of page 15
PROBLEM SOLVING & PROGRAMMING II ARRAYS
Image of page 16
INTRODUCTION Arrays behaves like a list of variables with a uniform naming mechanism that can be declared in a single line of simple code. The data inside each array must be of the same type Once array is declared, the size of the array is given in square brackets after the array name. Arrays have index of integer type that starts from 0 (NOT 1) and ends at size-1. Declaration of array: int score[5]; (it means declare 5 variables of int type) score[0],score[1],score[2],score[3],score[4]
Image of page 17
ARRAYS You can declare arrays and regular variables together. int x, score[5],max; Here the number in the brackets , is the size of the array . But when we use score[2] inside the program, this means the variable of the array previously declared. score[2]=10; You can use any expression in the square brackets as long as the expression evaluates to one of the integers ranging form 0 to size-1. int n=3; score[n+1]=50;
Image of page 18
FILL ARRAY void main() { int arr[10]; cout<<"Enter your array variables:\n"; for(int i=0;i<10;i++) { cin>>arr[i]; } }
Image of page 19
INPUT/OUTPUT ARRAY int main() { int arr[10]; //Input array variables of index 0 to (size -1) cout<<"Enter your array variables:\n"; for(int i=0;i<10;i++) { cin>>arr[i]; } //Output array variables on the screen cout<<"Array variables are : \n"; for(int i =0;i<10;i++) { cout<<arr[i]<<endl; } return 0; } OUTPUT Enter your array variables: 10 20 30 40 50 60 70 80 90 100 Array variables are : 10 20 30 40 50 60 70 80 90 100
Image of page 20
ARRAYS Sometimes you need to use variables as the size of an array: int students_number=5; int score[student_number]; int main() { int student_number=5; int score[student_number]; cout<<"Enter your array variables:\n"; for(int i=0;i<student_number;i++) { cin>>score[i]; } cout<<"Array variables are : \n"; for(int i =0;i<student_number;i++) { cout<<score[i]<<" "; } return 0; }
Image of page 21
ARRAY IN MEMORY The location of the various array indexed variables are always placed next to each other in memory. int x[6]; The computer reserves enough memory to hold 6 variables of type int, then it remembers the address of indexed variable x[0]. When program needs the address of some indexed variable in this array , the computer calculates the address for this other index variable from the address of x[0]. So to obtain the address of x[3],computer starts with address of x[0] , then adds the number of bytes needed to hold 3 variables of type int to the number for the address of x[0].
Image of page 22
ARRAY IN MEMORY Out of range : when an index expression evaluates to some value other than those allowed by the array declaration.
Image of page 23
Image of page 24

You've reached the end of your free preview.

Want to read all 30 pages?

  • Fall '18
  • Eric Swartz

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes