Practical 8 (Part A) - Array I

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

BACS1013 & BACS1014 Problem Solving and Programming Practical 8 (Part A)

Practical 8 (Part A)

One dimensional Array

1. Declare two string arrays named studID and studName respectively, each can store up to 8
elements. Then perform the following:
(a) Prompt user to enter up to 8 students’ ID and names from your class. Store the data into
the respective studID and studName arrays.
Remark: You may use a while or do-while loop. Ensure stop prompting for input
after 8 iterations. Example of input:
Enter Student Name: -> Chong S. W.
Enter Student ID: -> WAA12345
Press [Y] for more record, press [N] to stop
Option: -> Y
Enter Student Name: ->

(b) Tabularize all the student IDs and student names retrieved from the studID and
studName arrays. Example of output screen is as follows:
No. Student Name Student ID
=== =========== ========
1 Chong S. W. WAA12345
2 Leng Y. L. WAA23456
3 Laurence Leong WAA34567
4 Chris Lim WAA45678
5 Alan Tam WAA56789
6 Wong C. H. WAA67890
7 Caleb Wong WAA78901
8 Wong Y. X. WAA89012

2. Write a C++ program to create an array named Mark which can store 5 marks for 5 students
and initialize it to 0. Then perform the following tasks:

(a) Using a for loop, store 5 students marks entered by user into the Mark array. Display the
marks in the array.

(b) Display the student marks in the Mark array in reverse order.

(c) Display the 3rd student original mark. Then, prompt user to enter a new mark for this 3rd
student which will change the original mark.

(d) Assign 0 mark to the 1st student and display all the marks in the Mark array.

Page 1 of 5
BACS1013 & BACS1014 Problem Solving and Programming Practical 8 (Part A)

3. Write a C++ program to perform the following tasks:

(a) Declare an array of integer type called X and initialize it with values 5, 10, 15, 20 and 25.

(b) Use for loop to copy contents in X into array Y of the same type. Then multiply each
element in Y by 3. Display the contents in X and Y.

(c) Write a C++ program by using for loop to find the sum of the numbers stored in the array
in part (a). Then find and display the average of these numbers.

4. Write a C++ program to create an array of size 10. Prompt user to input 10 integer numbers into
the array. Find and print the largest number and the index of the element where the number is
stored.

5. Write a C++ program to declare three (3) one-dimensional integer arrays X, Y and Z of size 5. At
the time of declaration, initialize each of the arrays X and Y with 5 positive integers as given
below. (Note that the integers are those in the same column as the array.)

Compare the corresponding elements of the arrays X and Y and store the larger number into the
corresponding element of the array Z. Print the three arrays in three parallel columns like
below:
X Y Z
-----------------------
5 8 8
6 2 6
9 8 9
7 10 10
1 3 3

6. Write a C++ program that generates one hundred random integers between 0 and 9 and
displays the counts for each number.
Hint: Use rand() % 10 to generate a random integer between 0 and 9. Use an array of ten
integers, say counts, to store the number of occurrences for the numbers 0 to 9
(inclusive).

7. Write a C++ program that reads in ten numbers and displays distinct numbers (i.e., if a number
appears multiple times, it is displayed only once).
Hint: Read a number and store it to an array if it is new. If the number is already in the array,
discard it. After the input, the array contains the distinct numbers.

Page 2 of 5
BACS1013 & BACS1014 Problem Solving and Programming Practical 8 (Part A)

8. Trace and understand the following programs:


(a)
#include <iostream>
using namespace std;

void doubleArray (int [], int);


void showValues (int [], int);

int main()
{
const int ARRAY_SIZE = 10;
int array1[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 10};

cout << "The array values are:\n";


showValues (array1, ARRAY_SIZE);

doubleArray (array1,ARRAY_SIZE);

cout << "After calling doubleArray, the values are:\n";


showValues (array1, ARRAY_SIZE);

return 0;
}

void doubleArray (int array1[], int size)


{
for (int i=0; i<size; i++)
array1[i] *= 2;
}

void showValues (int array1[], int size)


{
for (int i=0; i<size; i++)
cout << "Array[" << i << "] = " << array1[i] << endl;
cout << endl;
}

Page 3 of 5
BACS1013 & BACS1014 Problem Solving and Programming Practical 8 (Part A)

(b)
#include <iostream>
using namespace std;
void det(int num[],int count, int &l, int &s);

int main()
{
int n[100], count=0, largest, smallest;

cout << "Enter a positive integer: ";


cin >> n[count];
while(n[count]){
cout <<"Enter a positive integer or zero to stop: ";
cin >> n[++count];
}

det(n, count, largest, smallest);


cout << "There are "<< count << " numbers entered." << endl;
cout << "The largest number is " << largest << ".\n";
cout << "The smallest number is " << smallest << ".\n";

return 0;
}

void det(int num[],int count, int &l, int &s)


{
int i;
l=s=num[0];

for(i = 1; i < count; i++)


{
if(num[i]>l)
l=num[i];
else if(num[i]<s)
s=num[i];
}
}

9. Write a C++ program that will get a set of sales data for the whole year from the user (floating-
point values), store in the array monthlySales of size 12. Send the array to five functions:
(a) Function getData that will accept a set of sales data from the user for the whole year.
(b) Function sumArray that will calculate the total of sales for the year.
(c) Function getHighest that will find the highest sales for the year.
(d) Function getLowest that will find the lowest sales for the year.
(e) Function printData that will show all the results on the screen.

Page 4 of 5
BACS1013 & BACS1014 Problem Solving and Programming Practical 8 (Part A)

Extra exercise

1. Write a C++ program to count the vowels and letters in free text given as standard input. Then
print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total
number of other letters, and each of the vowels and other letters as an percentage of the total
text. Declare 3 arrays to store the text, to count the occurrences of characters, and to calculate
the percentage. Declare your text as C-String type.

Sample output:
Enter a line of text:We are in practical lab now

a e i o u rest
Numbers of characters 4 2 2 1 0 18
Percentages of total 14.8% 7.4% 7.4% 3.7% 0.0% 66.7%

Page 5 of 5

You might also like