Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

04_array_string

The document outlines a lab assignment for CSC10012 focusing on arrays and strings, detailing function prototypes for both 1D and 2D arrays, including operations like input, output, sum, and finding minimum values. It also includes string manipulation functions such as reversing a string, checking for palindromes, and formatting numeric strings. Additionally, it presents practice problems involving election results, temperature data analysis, and seat assignment on a commercial aircraft.

Uploaded by

phuctran180406
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

04_array_string

The document outlines a lab assignment for CSC10012 focusing on arrays and strings, detailing function prototypes for both 1D and 2D arrays, including operations like input, output, sum, and finding minimum values. It also includes string manipulation functions such as reversing a string, checking for palindromes, and formatting numeric strings. Additionally, it presents practice problems involving election results, temperature data analysis, and seat assignment on a commercial aircraft.

Uploaded by

phuctran180406
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSC10012

LAB 04 – ARRAY & STRING


FIT-HCMUS

Part 1 – Array

Problem 1 – 1D Array
Implement the following function declarations/prototypes:

1. Input an array of size n


void inputArray(int arr[], int &n);

2. Output/print an array of size n


void outputArray(int arr[], int n);

3. Calculate the sum of elements in an array of size n


int sumArray(int arr[], int n);

4. Find the minimum value in an array of size n


int findMin(int arr[], int n);

5. Find the position/index of value x in an array of size n


int findIndexOfX(int arr[], int n, int x);

6. Count the number of occurrences of value x in an array of size n


int countOccurrences(int arr[], int n, int x);

7. Count the number of prime numbers in an array of size n


int checkPrime(int num);
int countNumberofPrime(int arr[], int n);

8. Check if an array is in ascending order


int isAscendingOrder(int arr[], int n);

9. Remove all elements that equal to x


void removeElements(int arr[], int &n, int x);

10. Remove num_del consecutive elements starting from the given position idx
void removeElementsFromIndex(int arr[], int &n, int idx, int num_del);

11. Concatenate two arrays


void concatTwoArrays(int a[], int na, int b[], int nb, int c[], int &nc);

12. Merge two arrays into one by interleaving elements (c = a[0], b[0], a[1], b[1], . . . )
void interleaveArrays(int a[], int na, int b[], int nb, int c[], int &nc);

1
Problem 2 – 2D Array
Implement the following function declarations/prototypes:

1. Input a 2D array with m rows and n columns


void input2dArray(int arr[][MAX], int m, int n);

2. Output/print a 2D array with m rows and n columns


void output2dArray(int arr[][MAX], int m, int n);

3. Calculate the sum of elements of a 2D array with m rows and n columns


int sumArray(int arr[][MAX], int m, int n);

4. Calculate the sum of the diagonal elements in a 2D array with m rows and n columns
int sumDiagonal(int arr[][MAX], int m, int n);

5. Find the minimum value in a 2D array with m rows and n columns


int findMin(int arr[][MAX], int m, int n);

6. Count the number of occurrences of value x in a 2D array with m rows and n columns
int countOccurrences(int arr[][MAX], int m, int n, int x);

7. Count the number of prime numbers in a 2D array with m rows and n columns
int countNumberofPrime(int arr[][MAX], int m, int n);

8. Remove rows and columns that have the value x


void removeElements(int arr[][MAX], int &m, int &n, int x);
Hint: Consider breaking down the problem into smaller tasks like removing rows and columns. Experiment
with different scenarios, such as:

• There is only one x value in the array: This is the simplest case, as removing the row and column
containing the x value will be straightforward.
• The x values are not in the same row or column: This is a slightly more complex case, as removing one
row or column might affect the location of other x values.
• The x values are located randomly: This is the most general case, where x values can be anywhere in
the array.

Part 2 – String
Implement the following function declarations/prototypes:

1. Reverse a string
void reverseString(char str[])

2. Check if a string is palindrome. A string is a palindrome if it reads the same forward and backward, or if it
remains the same when reversed
int isPalindromeString(char str[]);

3. Count the number of occurrences of a character c


int countOccurrencesChar(char str[], char c);

2
4. Find the most common character and its occurrence in a string
int findMostCommonChar(char str[], char &most_common_char);

5. Insert a character chr into a string at a given position idx


void insertCharAtPosition(char str[], char chr, int idx);

6. Convert a numeric string into a formatted string with commas as thousands separators
void formatNumberWithCommas(char str[]);

Implement the above function declarations/prototypes using std::string.

Part 3 – Practice Problems

Problem 1
Write a program that calculates and displays election results. The program should prompt users to enter the names
of candidates and their respective vote counts. The program then calculates and displays the percentage of votes
each candidate received and identifies the winning candidate.
An example:

Candidate Votes Received % of Total Votes


---------------------------------------------------------
Hoa 5000 25.91
Thang 4000 20.73
Thanh 6000 31.09
Hang 2500 12.95
Tuan 1800 9.33
---------------------------------------------------------
Total 19300

The Winner of the Election is Thanh.

Problem 2
Write a program that uses a 2D array to store the highest and lowest temperatures for each month of the year. The
program’s output should include the average high temperature, the average low temperature, and the highest and
lowest temperatures for the entire year. The program must include the following functions:

• getData: This function is used to collect and store data in a 2D array.

• averageHigh: This function calculates and returns the average high temperature for the 12 months of the
year.

• averageLow: This function calculates and returns the average low temperature for the 12 months of the year.

• indexHighTemp: This function returns the index (or position) of the highest temperature in the 2D array.

• indexLowTemp: This function returns the index (or position) of the lowest temperature in the 2D array.

3
Problem 3
Write a program that is used to assign seats on a commercial aircraft. The plane has 13 rows, with six seats per
row. Rows 1 and 2 are first class, rows 3 to 7 are business class, and rows 8 to 13 are economy class.
An example of a seat map:

A B C D E F
Row 1 * * X * X X
Row 2 * X * X * X
Row 3 * * X X * X
Row 4 X * X * X X
Row 5 * X * X * *
Row 6 * X * * * X
Row 7 X * * * X X
Row 8 * X * X X *
Row 9 X * X X * X
Row 10 * X * X X X
Row 11 * * X * X *
Row 12 * * X X * X
Row 13 * * * * X *

An asterisk (*) signifies a vacant seat, while an ’X’ denotes an occupied seat.
The program should provide a menu with options such as “View Seat Map”, “Book a Seat”, etc. When booking a
seat, users should be prompted to select their ticket type (first class, business class, or economy class) and choose
a specific seat.

You might also like