04_array_string
04_array_string
Part 1 – Array
Problem 1 – 1D Array
Implement the following function declarations/prototypes:
10. Remove num_del consecutive elements starting from the given position idx
void removeElementsFromIndex(int arr[], int &n, int idx, int num_del);
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:
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);
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);
• 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[]);
2
4. Find the most common character and its occurrence in a string
int findMostCommonChar(char str[], char &most_common_char);
6. Convert a numeric string into a formatted string with commas as thousands separators
void formatNumberWithCommas(char str[]);
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:
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:
• 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.