Sie sind auf Seite 1von 5

Computing For Engineers

Lab Activity 6: Arrays Objectives: To be familiar with arrays

Learning Outcomes: At the end of the session, the students are able:th To understand and use arrays in programming

Apparatus: PC with C++ compiler

Sample Code 1: array. The program code below shows one dimensional array int main() { int size = 10; int a[size]; for (size = 1; size<=4; size++) { cout<<"Enter a number: "; cin>>a[size]; } nThe cout<<"\nThe numbers stored in the array are"<<endl; for (int x = 1; x<=4; x++) { cout<<a[x]<<" "; } }

Computing For Engineers Arrays

Sample Code 2: The program code below shows a two dimensional array. #include <iostream> using namespace std; int main() { int row = 2; 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"); }

Computing For Engineers Arrays

Questions 1 A. Fill in the blanks in each of the following: . a) C++ stores lists of values in ___________________. ANS: b) The names of the four elements of array p are ____, ____, ____ and ____. ANS: n c) An m-by-n array contains ____ rows, ____columns and ____ elements. ANS: d) The name of the element in row 2 and column 5 of array d is ________. ANS:

B. Consider a 3-by-5 integer array t. a) Write a declaration for t. ANS: b) How many rows does t have? ANS: c) How many columns does t have? ANS: d) How many elements does t have? ANS: e) Write the names of all the elements in the second row of t. ANS: f) Write the names of all the elements in the third column of t. ANS: g) Write a single statement that sets the element of t in row 1 and column 2 to zero. ANS: h) Write a series of statements that initialize each element of t to zero. Do not use a repetition structure. ANS:

Computing For Engineers Arrays

Questions 2 Write a program code that is able to determine and display the largest and smallest number from a series of 10 numbers. The series of 10 numbers should be user input from keyboard. Array must be used for this program. Output

Questions 3 Write a program code that is able to search and display a certain number from a series of predetermined numbers (initialized and declared in program). Example int a[size] = {1,10,2,5,7,8,12,15,9,2}; Sample output #1

Sample output #2

Computing For Engineers Arrays

Questions 4 The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, .. begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a program code that is able to display the Fibonacci series until the 30 term. 30-th Array must be used for this program. The output of the program code (print screen) should also be attached. Fibonacci 10-th Sample output #1 (Fibonacci series until the 10 term)

Questions 5 Write a program code that is able to display all the prime numbers from 2 until 500. Any form of loop can be used. The output of the program code (print screen) should also be attached.

Computing For Engineers Arrays

Das könnte Ihnen auch gefallen