Click here to load reader

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming

Embed Size (px)

Citation preview

  • Slide 1
  • Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming
  • Slide 2
  • Syntax Summary 2 Punctuators []
  • Slide 3
  • Example 1 3 Input the marks for 10 students Store the marks in variables Compute the average marks Print the marks of the students and the average 100 30 44 66 50 60 80 75 80 100 The mark of the students are: 100, 30, 44, 66, 50, 60, 80, 75, 80, 100 Average mark=68
  • Slide 4
  • The program 4 /*define variables for storing 10 students' mark*/ int mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8, mark9, mark10, average; /*input marks of student*/ cin >> mark1 >> mark2 >> mark3 >> mark4 >> \\ mark5 >> mark6 >> mark7 >> mark8 >> mark9 >> mark10; /*print the marks*/ cout
  • Correct program for example 2 17 #include using namespace std; void main(){ int count[10]={0}; //number of occurrence of digits, and initialization int digit; //input digit int i; //loop index //read the digits do { cin >> digit; if (digit>=0 && digit a[i]; cout >x; for (i=0; i>x; " title="The program 27 void main() { const int N = 6; int a[N], i, x, position = -1; for (i=0; i> a[i]; cout >x; ">
  • The program 27 void main() { const int N = 6; int a[N], i, x, position = -1; for (i=0; i> a[i]; cout >x; for (i=0; i > a[j]; for(j=0; jj; k--) // bubbling if (a[k]
  • CS2363 A 31 void main() { const int n = 10; int a[n], j, k, tmp; cout
  • Multi-dimensional Array 34 To access an element of the array, we specify an index for each dimension: cin >> page [i][j];//[row][column] The above statement will input an integer into row-i and column j of the array. When passing a multi-dimensional array into a function, the size of all dimensions except the first must be specified. The function below can take any two-dimensional integer array with size 100 in the second dimension (size of first dimension is specified in parameter size1 void display (int a[][100], int size1)
  • Slide 35 > data[i][0]; cin >> data[i][1]; } for (i=0; i