Computing For Engineers: Lab Activity 6: Arrays Objectives
Computing For Engineers: Lab Activity 6: Arrays Objectives
Computing For Engineers: Lab Activity 6: Arrays Objectives
Learning Outcomes: At the end of the session, the students are able: To understand and use arrays in programming
Sample Code 1: The program code below shows one dimensional array. int main() { const int size = 4; int a[size]; for (size = 1; size<=4; size++) { cout<<"Enter a number: "; cin>>a[size]; } cout<<"\nThe numbers stored in the array are"<<endl; for (int x = 1; x<=4; x++) { cout<<a[x]<<" "; } }
Sample Code 2: The program code below shows a two dimensional array. #include <iostream> using namespace std; int main() { const int row = 2; const int col = 3; int table[row][col]; cout<<"Enter the values in the table..."<<endl; for (int r = 0; r<row; r++) { for (int c = 0; c<col; c++) { cout<<"Row "<<r + 1<<" -- Column "<<c+1<<" : "; cin>>table[r][c]; } } cout<<"The values stored in the table are..."<<endl; for (int r = 0; r<row; r++) { for (int c = 0; c<col; c++) { cout<<"Table ["<<r + 1<<"]["<<c+1<<"] : "<<table[r][c]; cout<<endl; } } system("pause"); }
Questions 1 A. Write array declarations for the following: a) A list of 100 double-precision grades b) A list of 50 double-precision temperatures c) A list of 30 integers, each representing a code d) A list of 100 integers years B. Write appropriate notation for the first, third and seventh elements of the following arrays a) int grade [20] b) double prices [10] c) double amps [16] d) double velocity [25]
Questions 2 a. Write a program to input ten integer numbers into an array named fmax and determine the maximum value entered. Your Program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values) b. Repeat Question 2a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, prints these two messages: The maximum value is: ___________ This is element number ___________ in the list of numbers Have your program display the correct values in place of the underlines in the messages. c. Repeat Question 2b, but have your program locate the minimum of the data entered.
Questions 3 a. Write a program that has declaration in main() to store string Vacation is near into array named message. There should be a function call to display() that accepts message in a parameter named string and then display the message.
b. Modify the display() function written in Question 3a to display the first eight elements of the message array Questions 4 Write a C++ program that adds equivalent elements of the two-dimensional arrays named first and second. Both arrays should have two rows and three columns. For example, elements [1][2] of the resulting array should be the sum of first [1][2] and second [1][2]. The first and second arrays should be initialized as follows: First 18 91 Second 24 52 77 16 19 59
16 54
23 11