Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 1 of 7
LABORATORY EXERCISE
1. Write a program that will accept five (5) integers and display them to the users. You are limited to using
only two (2) variables (including the array).
FLOWCHART PSEUDOCODE

START DECLARE x[5], i as int


OUTPUT “Please Enter 5 Numbers”
SET i AS 0
FOR i < 5
DECLARE
x[5], i OUTPUT “Number [“+i+1+”]:”
INPUT x[i]
SET i AS (i + 1)
DISPLAY “Please LOOP
Enter 5 Numbers” OUTPUT “You Enter These Numbers:”+
x[0] +”,” +x[1] +”,” +x[2] +”,” +x[3] +”,”
+x[4]

SET i = 0

True DISPLAY
i<5 “Numbers [“+ i + 1
+ “]:”

False
ACCEPT x[i]

DISPLAY “You Entered


These Numbers: “+[0]
SET i = 0
+”,” + x[1] +”,” +x[2] +”,”
+ x[3] +”,” + x[4]

END

VS CODE
#include <iostream>

using namespace std;

int main (){


int x[5], i;
cout << "Please Enter 5 Numbers." <<endl;

for(i = 0; i < 5; i++)


{
cout <<"Number[" << i + 1 << "]: ";
cin >> x[i];
}
cout << "You Entered These Integers: " << x[0] << ", "<< x[1] << ", " << x[2] << ", "<< x[3]
<< ", " << x[4] << endl;

return 0;
}

1
Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 2 of 7
LABORATORY EXERCISE

2. Write a program that will display an equilateral triangle with a height depending on the user. The minimum height is
1, the maximum height is 10. Use an array to display the specific character on the specific row. The array will be: { 0 :=
“A”, 1:= “B”, 2 := “C”, 3 := “D”, 4 := “E”, 5 := “F”, 6 := “G”, 7 := “H”, 8 := “I”, 9 := “J” }. You are limited to four (4)
variables only (including the array).
PSEUDOCODE
DECLARE x[ ], n
SET x TO “ABCDEFGHIJ”
INPUT n
DECLARE i
FOR (i = 0; i < = i + 1)
DECLARE s
FOR (s = 0; s<n-i-1; s = s+1)
OUTPUT “ “
LOOP
DECLARE t
FOR (t = 0; t < (i*2)+1; t = t + 1)
OUTPUT x[i]
LOOP
CALL DoEndline[]
LOOP
END

FLOWCHART

START

DECLARE
x[ ], n

2
Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 3 of 7
LABORATORY EXERCISE

x[ ] = “ABCDEFGHIJ”

ACCEPT
n

DECLARE
i

i=0

False True
DECLARE
i<n s

s=0

False True

DECLARE
s<n–i-1 DISPLAY “ “
t

t=0 s=s+1

False True
t <(i*2) + 1

Endline DISPLAY x[i]

i=i+1 t=t+1

END
VS CODE
#include <iostream>

using namespace std;

int main () {

char x[] = "ABCDEFGHIJ";


int n;

cout << "Enter Height (1 - 10): ";


cin >>n;
3
Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 4 of 7
LABORATORY EXERCISE
cout << endl;

for (int i=0; i < n; i++) {


for(int s = 0; s < n - i - 1; s++) {
cout << " ";
}
for (int t=0; t < (i*2) + 1; t++) {
cout << x[i];
}
cout << endl;
}
cout << endl;

//_pause ();
system("PAUSE");
return 0;

3. Write a program that will ascendingly sort six (6) integers from the user. Use only four (4)
variables (including the array).
PSUEDOCODE
DECLARE n[6]
DECLARE i
FOR (i = 0; i < 6; i = i + 1)
INPUT n[i]
LOOP
FOR (i = 0; i < 6; i = i + 1)
DECLARE t
FOR (t = 0; t < 6; t = t + 1)
IF n[i] > n[t] THEN
DECLARE temp
SET temp TO n[i]
SET n[i] TO n[t]
SET n[t] TO temp
END
LOOP 4
LOOP
Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 5 of 7
LABORATORY EXERCISE

FLOWCHART
START

DECLARE
n [6]

DECLARE
i

i=0

False True
i<6

ACCEPT
n [i]
i=0
2
5
Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 6 of 7
LABORATORY EXERCISE
i=i+1
False True
i<6
1

DECLARE
t

t=i

False True
t<6

i=i+1
False True
n[i] > n[t]
2
DECLARE
t=t+1 temp

3
4 temp = n[i]
DISPLAY “The sorted
numbers are: “+n[0] +”,” +
n[1] +”,” +n[2] +”,” +n[3] +” “
+n[4] +” and “ +n[5] +”.” n[i] = n[t]

n[t] = temp
END
VSCODE 3
#include <iostream>

using namespace std;

int main () {

int n[6];

for (int i = 0; i < 6; i++) {


cout << "Enter number[ " << i + 1 << "]: ";
cin >> n[i];
}

for (int i = 0; i < 6; i++) {


for (int t = i; t < 6; t++) {
if (n[i] > n[t]) {
int temp = n[i];
n[i] = n[t];
n[t] = temp;
}
}
}
6
Course Code CS201

Description Computer Programming 1

Laboratory
Title of
Exercise 007 Arrays
Activity
No.
Page 7 of 7
LABORATORY EXERCISE

cout << "The sorted numbers are: " << n[0] << ", " << n[1] << ", " << n[2] << ", " <<
n[3] << ", " << n[4] << " and " << n[5] << ".";
cout << endl;

//_pause();
system("PAUSE");
return 0;

4. What can you conclude from this activity?

I therefore conclude that this activity that arrays in C++ is used to store a collection of data in a
more systematic way.

You might also like