Page 1 of 1

Question 4 Consider the program below that maintains a list of data in descending order. #include using names

Posted: Thu May 26, 2022 9:22 am
by answerhappygod
Question 4 Consider The Program Below That Maintains A List Of Data In Descending Order Include Iostream Using Names 1
Question 4 Consider The Program Below That Maintains A List Of Data In Descending Order Include Iostream Using Names 1 (78.56 KiB) Viewed 12 times
Question 4 Consider the program below that maintains a list of data in descending order. #include <iostream> using namespace std; void print (int data[], int size) { cout << "Numbers: "; for (int i = 0; i < size; i++) cout<<data << " "; cout << endl; } int main() { int data [5] = {}; int count = 0; int n; // for storing input numbers // number of items in array. cout << "Input 5 numbers: \n"; for (int i = 1; i <= 5; i++) { cout << i << ": "; cin >>n; // Your code for Q4 (a) should be inserted here print (data, count); } Sample output: Input 5 numbers: 1: 15 Numbers: 15 2: 99 Numbers: 99 15. 3: -6 Numbers: 99 15 -6 4: 48 Numbers: 99 48 15 -6 5: 2042 Numbers: 2042 99 48 15 -6. (a) By moving numbers towards the end of the array, write your code to insert the input value n into array data[], such that the resulted array is still in descending order. The value of count should be updated to indicate the number of input items in the array. You may declare more variables when necessary. You should NOT implement sorting in your answer. (8 marks) } return 0;
Question 4 (continued) Consider the program below that performs counting on user input sentences. #include <iostream> using namespace std; void count (char text [] [30], int &numSmalls, int &numWords) { // Your code for Q4 (c) should be inserted here } int main() { char text [5] [30]; int numSmalls, numWords; // Your code for Q4 (b) should be inserted here cout << endl; count (text, numSmalls, numWords); cout << "Number of Small letters: " << numSmalls << endl; cout << "Number of words: << numWords << endl; 11 return 0; } Sample output: Input 5 sentences: 1: SEHH2042 Computer Programming 2: Exam is easy! 3: C++ vs Java? 4: Python is also good. 5: My grade will be A+ Number of Small letters: 58 Number of words: 18 (b) Write your code to prompt for user input of 5 sentences. The input sentences are stored in the given 2D-array text[][]. (5 marks) (c) Write your code to complete the function count(). The function counts the number of small letters (i.e. 'a' - 'z') and number of words in text[][], and stores the values in the parameters numSmalls and numWords, respectively. Each word ends with a space, or it is the end of the (7 marks) sentence.
(a) for (int index = 0; index <= 5; index++) { if (data[index] <n) { for (int rr = 4; (true); rr--) { data [rr] = data[rr-1]; if ((rr) == index) { break; } } data [index] (b) cout << "Input 5 sentences: for (int no = 1; no <= = 5, no++) { cout << no << “.”; cin.getline(text[no-1],30); } << endl;