Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013
One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the resulted two arrays. int arr1[10], arr2[10]; cout << "Enter the first array values\n"; cin >> arr1[i]; cout << "Enter the second array values\n"; cin >> arr2[i]; int temp; temp = arr1[i]; arr1[i] = arr2[i]; arr2[i] = temp; cout << "The first array values now are:\n"; cout << arr1[i] << " "; cout << "The second array values now are:\n"; cout << arr2[i] << " "; P a g e 2
Q2: Write a program that takes 10 integers as input and prints the largest integer and its location in the array. int arr[10]; cout << "Enter the array values\n"; cin >> arr[i]; int max = arr[0], loc = 0, i; for (i=1; i<10; i++) if (arr[i] > max) max = arr[i]; loc = i; cout << "The maximum value is: " << max << " and it is in location " << loc << endl; P a g e 3
Q3: Write a program that defines a one dimensional array of integers of size 10 and fill it from the user. Then find the negative values and store them in another array and display new array. int arr[10]; cout << "Enter the array values\n"; cin >> arr[i]; int new_arr[10], index = 0; if (arr[i] < 0) new_arr[index++] = arr[i]; cout << "The new array values are:\n"; for (int i=0; i<index; i++) cout << new_arr[i] << " "; P a g e 4
Q4: Write a program that defines a one dimensional array of integers of size 11 and fill the first 10 values from the user. Then calculate the summation of the 10 values and store it in the 11 th location. int arr[11]; arr[10] = 0; cout << "Enter the array values\n"; cin >> arr[i]; arr[10] += arr[i]; cout << "The new array values are:\n"; for (int i=0; i<11; i++) cout << arr[i] << " "; P a g e 5
Q5: Write a C++ program that reverses the order of a one-dimensional array of size 10 without using another array. int x, j=9, a[10] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; for(int i=0; i<(10/2); i++) x = a[i]; a[i] = a[j]; a[j] = x; j--; for(int i=0; i<10; i++) cout << a[i] << " "; P a g e 6
Q6: Write a C++ program that declares two arrays of size 5. Read the first array from the user and store the factorial of each element in this array in its corresponding location in the second array. double a[5], b[5]; int fact; cout << "Enter the first array values\n"; for(int i=0; i<5; i++) cin >> a[i]; fact = 1; for(int j=1; j<=a[i]; j++) fact *= j; b[i] = fact; cout << "The factorials array values are:\n"; for (int i=0; i<5; i++) cout << b[i] << " "; P a g e 7
Two Dimensional Q7: Write a program that adds up two [4x4] arrays and stores the sum in a third array. int a[4][4], b[4][4]; cout << "Enter the first array values\n"; cin >> a[i][j]; cout << "Enter the second array values\n"; cin >> b[i][j]; int c[4][4]; c[i][j] = a[i][j] + b[i][j]; cout << "The factorials array values are:\n"; cout << c[i][j] << " "; P a g e 8
Q8: Write a C++ program that declares a tow dimensional array of size [4x4] and generates the values of its elements using conditional statements as the following: The main diagonal contains 0 in all its locations The upper triangle contains 1 in all its locations The lower triangle contains 2 in all its locations int a[4][4]; if(i<j) a[i][j] = 1; else if (i==j) a[i][j] = 0; else a[i][j] = 2; cout << "The factorials array values are:\n"; cout << a[i][j] << " "; P a g e 9
Q9: Write a program that defines a two-dimensional array of integers of size 4*4. The program will fill the array of values using the equation array_name[i][j] = i+j+2 (i refers to the row index, j refers to the column index). Then, define a one-dimensional array of size 4. The one-dimensional array should be filled with the values along the main diagonal of the two-dimensional array. For example: If the two-dimensional array is: V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 The one-dimensional array will be: V1 V6 V11 V16 #include <iostream> int a[4][4]; a[i][j] = i+j+2; int b[4], index=0; if(i==j) b[index] = a[i][j]; index++; P a g e 10
Q10: Write a program that defines a two-dimensional array of integers of size 10*10. The program will fill each location of the array of it index summation (array[i][j] = i+j). Then print the summation of the elements on the array circumference as shown below: #include <iostream> int a[4][4]; a[i][j] = i+j; int sum = 0; if(i==0 j==0 i==9 j==9) sum += a[i][j]; cout << "The result is: " << sum << endl; P a g e 11
Q11: Write a program that stores the grades of 5 students in a two-dimensional array. Each student has 3 marks. Each row will represent a student and the last cell in the row will store the calculated average for the student's marks. Finally, display the average of all student averages. #include <iostream> float a[5][4], sum; for(int i=0; i<5; i++) sum = 0; cout << "Enter the 3 marks for the student number: " << i+1 << endl; for(int j=0; j<3; j++) cin >> a[i][j]; sum += a[i][j]; a[i][3] = sum/3; cout << "The students marks and averages are:" << endl; for(int i=0; i<5; i++) cout << a[i][j] << " "; sum = 0; for(int i=0; i<5; i++) sum += a[i][3]; cout << "The AVG for all sudents is: " << (float)sum/5 << endl; P a g e 12