labsheet 6 - fp 201

6
FP 201 Programming Fundamentals with C++ LAB 6: Array Learning Outcomes This Labsheet encompasses activities 6A, 6B, 6C, 6D and 6E By the end of this lab, students should be able to : Understand the basic concept of array Differentiate between 1-dimensional array and multi-dimensional array Write program using array Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0) Activity 6A Activity Outcome : Write and compile a program using C++. The following code illustrates how to declare an array, and access its elements. Procedure: Step 1: Type the programs given below #include<iostream> using namespace std; void main() { int i,sum=0; int markah [5] ; //declaring an array cout<<"Enter marks for memory location :"<<endl; //input value in array for (i=0; i < 5; i++) { cout<<"markah ["<<i<<"] = "; cin>>markah[i]; } cout<<"\n"<<endl;//new line //print location of array for (i=0; i < 5; i++) { cout<< "markah["<<i<<"]" <<"\t"; } //print element in array for (i=0; i < 5; i++) { cout<< markah[i] <<"\t\t"; } //process element in array for (i=0; i < 5; i++) 1

Upload: rohassanie

Post on 22-May-2015

397 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Labsheet 6 - FP 201

FP 201 Programming Fundamentals with C++

LAB 6: Array

Learning OutcomesThis Labsheet encompasses activities 6A, 6B, 6C, 6D and 6E

By the end of this lab, students should be able to : Understand the basic concept of array Differentiate between 1-dimensional array and multi-dimensional array Write program using array

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

Activity 6AActivity Outcome: Write and compile a program using C++.

The following code illustrates how to declare an array, and access its elements.

Procedure:Step 1: Type the programs given below#include<iostream>using namespace std;void main(){ int i,sum=0; int markah [5] ; //declaring an array cout<<"Enter marks for memory location :"<<endl;//input value in array for (i=0; i < 5; i++) { cout<<"markah ["<<i<<"] = "; cin>>markah[i]; } cout<<"\n"<<endl;//new line//print location of array for (i=0; i < 5; i++) { cout<< "markah["<<i<<"]" <<"\t"; }//print element in array for (i=0; i < 5; i++) { cout<< markah[i] <<"\t\t"; }//process element in arrayfor (i=0; i < 5; i++) { sum+=markah[i]; }cout<<"\n"<<sum; //print the sum}

Step 2: Compile the program. Step 3: Write the output.Step 4: Save the program as ________________

1

Page 2: Labsheet 6 - FP 201

FP 201 Programming Fundamentals with C++

Activity 6BActivity Outcome: Write and compile a program using C++.

The following code illustrates how to declare an array, initialize and access its elements.

Procedure:Step 1: Type the programs given below #include<iostream>using namespace std; void main() { char element[6] = {‘Z’,’A’,’I’,’N’,’A’,’L’};

cout << "\n element 1 : " << element [0] << endl; cout << "\n element 2 : " << element [1] << endl; cout << "\n element 3 : " << element [2] << endl; cout << "\n element 4 : " << element [3] << endl; cout << "\n element 5 : " << element [4] << endl; cout << "\n element 6 : " << element [5] << endl; }

Step 2: Compile the program. Step 3: Write the output.

Step 4: Save the program as ________________

Activity 6CActivity Outcome: Write and compile a program using C++.

Program to illustrate how to initialise a character array and display its contents.

Step 1: Type the programs given below and fill in the correct statement in line 4, 5 and 6. #include <iostream>using namespace std;void main(){ char alphabet[ _____ ] = {'A', 'B', 'C', 'D', 'E', ‘F’, ‘G’, ‘H’, ‘I’, ‘J’}; for ( int i = 0; i <10; ______ ) cout << _________ cout << endl;}

2

Page 3: Labsheet 6 - FP 201

FP 201 Programming Fundamentals with C++

Step 2: Compile the program. Step 3: Write the output.

Step 4: Save the program as ________________

Activity 6DActivity Outcome: Write and compile a program using C++. Printing a sequence in order.

This program reads five numbers and then prints them in reverse order.

Step 1: Type the programs given below. #include<iostream>using namespace std;void main(){ const int SIZE=5; Double a[SIZE]; cout<<”Enter five number:\t;

for(int i=0; i<SIZE; i++) cin >> a[i]; cout << “In reverse order: “; for(int i=SIZE-1; i>=0; i--) cout<<”\t”<< a[i];}

Step 2: Compile the program. Step 3: Write the output.

Step 4: Save the program as ________________

3

Page 4: Labsheet 6 - FP 201

FP 201 Programming Fundamentals with C++

Activity 6EActivity Outcome: Write and compile a program using C++.

This program illustrates how to write program using multi-dimensional array.

Step 1: Type the programs given below. #include<iostream>using namespace std;void main(){ int k, m, multiD_Array[2][3] = {{1, 2, 3}, {4, 5, 6}};

for (k=0; k<2; k++)for (m=0; m<3; m++){

cout << "element[“ << k <<”][“ << m << “] : ";cout << multiD_Array[k][m] << endl;

}cout << “\n”;for (k=0; k<2; k++)

for (m=0; m<3; m++){

cout << “\nEnter number : ”;cin >> multiD_Array[k][m];

}cout << “\n\n”;for (k=0; k<2; k++)

for (m=0; m<3; m++){

cout << "element[“ << k <<”][“ << m << “] : ";cout << multiD_Array[k][m] << endl;

}}

Step 2: Compile the program. Step 3: Write the output.

Step 4: Save the program as ________________

4

Page 5: Labsheet 6 - FP 201

FP 201 Programming Fundamentals with C++

LAB Exercise 6: Array

1. Declare an array with array size of 10 elements, type double, and initialize the elements to 0.

2. Write a program using array that can sort the list of five number entered by user.

3. Write a program to display values in 2 dimensional arrays as in the given table below.

9 7 5

6 8 10

Display of Output:

Pizza KFC McDonalds

Man 9 7 5

Woman 6 8 1

5