Your SlideShare is downloading. ×
Labsheet 6 - FP 201
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Labsheet 6 - FP 201

383
views

Published on

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
383
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. FP 201 Programming Fundamentals with C++LAB 6: ArrayLearning OutcomesThis Labsheet encompasses activities 6A, 6B, 6C, 6D and 6EBy 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 arrayHardware/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] <<"tt"; }//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
  • 2. FP 201 Programming Fundamentals with C++ Activity 6B Activity 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 6C Activity 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
  • 3. FP 201 Programming Fundamentals with C++ Step 2: Compile the program. Step 3: Write the output. Step 4: Save the program as ________________ Activity 6D Activity 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
  • 4. FP 201 Programming Fundamentals with C++ Activity 6E Activity 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 << “nn”; 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
  • 5. 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