How to Troubleshoot Apps for the Modern Connected Worker
Labsheet 6 - FP 201
1. 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] <<"tt";
}
//process element in array
for (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