SlideShare a Scribd company logo
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- info@cpphomeworkhelp.com or
reach us at :- https://www.cpphomeworkhelp.com/
Problems
Initializing multidimensional arrays:
The examples of multidimensional arrays in lecture did not give the syntax for initializing
them. The way to assign a multidimensional array in the array declaration is as follows:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
Inside the inner braces, commas still separate individual numbers. Outside, they separate
rows of the array. After this declaration, matrix[0][1] will return the value 2.
Each one of the rows is initialized like a regular array. For instance, if instead of {1, 2, 3}
we’d put {1}, the second and third elements of the first row would have been initialized
to 0.
1. Write a program that declares the 2D array of student test grades shown below, and
stores the students’ averages in a 1D array. Each row in the 2D array represents the
grades of a particular student (their parents uncreatively named them Student 0,
Student 1, etc.). Output the array of averages.
int studentGrades[6][5] = { {97, 75, 87, 56, 88}, {76, 84, 88, 59, 99}, {85, 86, 82, 81, 88},
{95, 92, 97, 97, 44}, {66, 74, 82, 60, 85}, {82, 73, 96, 32, 77} };
cpphomeworkhelp.com
Multidimensional arrays as arguments:
The syntax for passing multidimensional arrays as arguments is very similar to that of
passing regular arrays. The main difference is that you need to include the number of
columns in the function definition. For instance, a function that takes a 2-dimensional
array with 3 columns might be declared as follows:
void func( int array[][3], const int rows, const int columns ) {…}
2. Write a function based on your program from question 1 to calculate a set of grade
averages. It should take a few arguments, including a 2D array of student grades and a
1D array to store the averages in. (Recall that arrays are passed by reference.) It should
return nothing.
3. Multidimensional arrays are often used to store game boards. A tic-tac-toe board is a
basic example: the board can be stored as a 2D array of integers, where a positive
number represents Player 1, a negative number represents Player 2, and 0 represents a
space not yet taken by either player. Write a void function printTTTBoard that takes such
a 2D array as an argument and prints the board as a series of X’s, O’s, or spaces,
separated horizontally by tabs and vertically by newlines. For instance, it should output
the following board for the array
cpphomeworkhelp.com
4. Assume that the following variable declaration has already been made:
char *oddOrEven = "Never odd or even";
Write a single statement to accomplish each of the following tasks:
a. Print the 4th character in oddOrEven
b. Create a pointer to a char value named nthCharPtr that stores the memory address
of the 4th character in oddOrEven
c. Using pointer arithmetic, update nthCharPtr to point to the 7th character in
oddOrEven
d. Print the value currently pointed to by nthCharPtr
e. Create a new pointer to a pointer (a char ** – yes, those are legal too!) named
pointerPtr that points to nthCharPtr
f. Print the value stored in pointerPtr
g. Update nthCharPtr to point to the next character in oddOrEven (i.e. one character
past the location it currently points to)
h. Using pointer arithmetic, print out how far away from the character currently
pointed to by nthCharPtr is from the start of the string
cpphomeworkhelp.com
{ {0, -1, 1}, {-1, 1, -1}, {-1, 1, 1} }:
O X
O X O
O X X
5. Write a function swap that takes two arguments – pointers to character values –
and swaps the values of the variables these pointers point to. It should return nothing.
6. a. Write a function reverseString that takes one argument – a C-style string (a char
*) – and reverses it. The function should modify the values of the original string, and
should return nothing. (You may assume that you have a properly implemented swap
function as described in the previous problem. You may also use the strlen function,
which takes a char * and returns the length of the string. It is found in the cstring
standard header file.)
b. Indicate with two lines of code how you would declare a character array (a char[],
not a char*) containing the string "aibohphobia", and reverse it using reverseString.
Function pointers:
We mentioned in lecture that, since functions are really just blocks of instructions in
memory, you can create a pointer to a function. Schematically, the syntax for declaring
such a pointer is as follows:
return_type (*name)(argument_types);
The syntax for declaring the pointer is identical to the syntax for declaring a function,
except that where you’d put the name of the function in the function definition, you
put (*pointerName). To give an example:
cpphomeworkhelp.com
void (*functionPtr)(int, char) = &someFunction; // & is optional
Here, we declare a function pointer named functionPtr which points to some function
named someFunction. That function must be a void function that takes as arguments
one int and one char. Once we’ve declared functionPtr as a function pointer, we can
use it to call the function it points to simply by writing something like functionPtr(1,
'a').
7. a. Define two functions, cmpAscending and cmpDescending. Each should take two
integers. cmpAscending should return true if the first integer is greater than the second
and false otherwise. cmpDescending should do the reverse. Each one should be no
more than 3 lines of code.
b. One common application of function pointers is sorting – you can pass a function
pointer to a sorting function to decide whether to sort in ascending or descending
order. Extract the nested loop from the bubble sort code snippet from the Lecture 4
notes and place it into its own function. The function should take 3 arguments: an
integer array, the length of the array, and a function pointer. The function pointer (call
it comparator) should be of a type that points to a function that takes two integers and
returns a boolean. Modify the sorting code snippet to use comparator to compare
values for sorting, and show how you’d call your sort function with either
cmpDescending or cmpAscending.
cpphomeworkhelp.com
8. Write a line to accomplish each of the following tasks. You will want to look up the
documentation for string functions online. (Recall that the way to call a function on a
string variable is, for instance, myString.find("a"). Also note that the usual cin and cout
syntaxes work for strings, but cin will stop reading at the first whitespace character.)
a. Declare a variable papayaWar of type string (assuming header file <string> has
been included). Initialize it to the value "No, sir, away! A papaya war is on!"
b. Print the index of the first instance of the exclamation point character in
papayaWar.
c. Print the length of the papayaWar string.
d. Print the last 5 characters of papayaWar.
e. Replace the contents of the string with a string that the user enters.
f. Append the string "rotator" to the end of papayaWar.
g. Print the contents of papayaWar.
cpphomeworkhelp.com
1. #include <iostream>
using namespace std;
int main() {
int grades[6][5] = { {97, 75, 87, 56, 88},
{76, 84, 88, 59, 99}, {85, 86, 82, 81, 88},
{95, 92, 97, 97, 44}, {66, 74, 82, 60, 85},
{82, 73, 96, 32, 77} };
double averages[6];
for (int i = 0; i < 6; i++) {
int sum = 0;
for (int j = 0; j < 5; j++)
{
sum += grades[i][j];
}
averages[i] = sum/5.0;
}
for (int k = 0; k < 6; k++)
cout << "Student " << k
<< " has an average of " << averages[k] << endl;
return 0;
}
cpphomeworkhelp.com
Solutions
2. void average( int grades[][5],
const int rows, const int columns, double averages[] ) {
for (int i = 0; i < rows; i++) {
int sum = 0;
for (int j = 0; j < columns; j++) {
sum += grades[i][j];
}
average [i] = sum / static_cast<double>(columns);
}
}
3. void printTTTBoard(int array[][3]) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << (array[i][j] > 0 ? 'X' :
(array[i][j] == 0 ? ' ' : 'O') ) << 't';
}
cout << endl;
}
}
cpphomeworkhelp.com
4. a. cout << oddOrEven[3]; // or cout << *(oddOrEven + 3);
b. char *nthCharPtr = oddOrEven + 3; (or, less preferably, &oddOrEven[3])
c. nthCharPtr += 3;
d. cout << *nthCharPtr;
e. char **pointerPtr = &nthCharPtr;
f. cout << pointerPtr;
g. nthCharPtr++; h.
h. cout << nthCharPtr - oddOrEven;
5. void swap( char *xPtr, char *yPtr ) {
char temp = *xPtr;
*xPtr = *yPtr;
*yPtr = temp;
}
6. a. void reverseString ( char string[] ) {
int stringLen = strlen(string);
for (int x=0; x< stringLen/2; x++) {
int y = stringLen-x-1;
swap (string[x], string[y]);
}
}
cpphomeworkhelp.com
b. char aibophobia[12] =
{'a', 'i', 'b', 'o', 'h', 'p', 'h', 'o', 'b', 'o', 'a', '0');
reverseString(aibophobia);
7. bool cmpAscending(int a, int b) {
return a < b;
}
bool cmpDescending(int a, int b) {
return a > b;
}
void sort(int arr[], const int arrLen,
bool (*comparator)(int,int)) {
for(int i = 0; i < arrLen; i++) {
for(int j = 0; j < i; j++) {
if( comparator(arr[i],arr[j]) ) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
cpphomeworkhelp.com
8. a. string papayaWar = "No, sir, away! A papaya war is on!";
b. cout << papayaWar.find('!'); // or find("!")
c. cout << papayaWar.length();
d. cout << papayaWar.substr( papayaWar.length() - 5 );
or, less preferably,
cout << papayaWar.substr( 29 );
e. getline(cin, papayaWar); // or cin >> papayaWar, but that
// will stop at whitespace
f. papayaWar.append("rotator"); // or papayaWar += "rotator";
g. cout << papayaWar;
cpphomeworkhelp.com

More Related Content

PPTX
CPP Homework Help
PPTX
CPP Homework Help
PPTX
Array and string in C++_093547 analysis.pptx
PPTX
Arrays & Strings.pptx
PPT
2DArrays.ppt
PPTX
Library functions in c++
PPTX
Get Fast C++ Homework Help
PPT
Lecture#9 Arrays in c++
CPP Homework Help
CPP Homework Help
Array and string in C++_093547 analysis.pptx
Arrays & Strings.pptx
2DArrays.ppt
Library functions in c++
Get Fast C++ Homework Help
Lecture#9 Arrays in c++

Similar to C++ Programming Homework Help (20)

PDF
CBSE Question Paper Computer Science with C++ 2011
PDF
C++ normal assignments by maharshi_jd.pdf
PPTX
Structured data type
PPTX
Chapter1.pptx
PPTX
3 (3)Arrays and Strings for 11,12,college.pptx
PDF
Arrays and strings in c++
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPTX
C++ Homework Help
PPTX
arraytypes of array and pointer string in c++.pptx
PDF
Computer science-2010-cbse-question-paper
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
PDF
Arrays and library functions
PDF
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
PDF
Programming fundamentals using c++ question paper 2014 tutorialsduniya
DOCX
#include iostream #include cstringusing namespace std;PR.docx
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
PPTX
CPP Homework Help
PDF
Data Structure and Algorithm
PDF
Arrays and function basic c programming notes
CBSE Question Paper Computer Science with C++ 2011
C++ normal assignments by maharshi_jd.pdf
Structured data type
Chapter1.pptx
3 (3)Arrays and Strings for 11,12,college.pptx
Arrays and strings in c++
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
C++ Homework Help
arraytypes of array and pointer string in c++.pptx
Computer science-2010-cbse-question-paper
C++ How to Program 10th Edition Deitel Solutions Manual
Arrays and library functions
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Programming fundamentals using c++ question paper 2014 tutorialsduniya
#include iostream #include cstringusing namespace std;PR.docx
C++ How to Program 10th Edition Deitel Solutions Manual
CPP Homework Help
Data Structure and Algorithm
Arrays and function basic c programming notes
Ad

More from C++ Homework Help (15)

PPTX
cpp promo ppt.pptx
PPTX
CPP Homework Help
PPT
CPP homework help
PPTX
CPP Programming Homework Help
PPTX
C++ Programming Homework Help
PPTX
Online CPP Homework Help
PPTX
Best C++ Programming Homework Help
PPTX
CPP Homework Help
PPTX
CPP Programming Homework Help
PPTX
CPP Homework Help
PPTX
Online CPP Homework Help
PPTX
CPP Assignment Help
PPTX
CPP Homework help
PPTX
CPP homework help
PPTX
Cpp Homework Help
cpp promo ppt.pptx
CPP Homework Help
CPP homework help
CPP Programming Homework Help
C++ Programming Homework Help
Online CPP Homework Help
Best C++ Programming Homework Help
CPP Homework Help
CPP Programming Homework Help
CPP Homework Help
Online CPP Homework Help
CPP Assignment Help
CPP Homework help
CPP homework help
Cpp Homework Help
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Business Ethics Teaching Materials for college
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Business Ethics Teaching Materials for college
human mycosis Human fungal infections are called human mycosis..pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Types and Its function , kingdom of life
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Week 4 Term 3 Study Techniques revisited.pptx
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Insiders guide to clinical Medicine.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial diseases, their pathogenesis and prophylaxis
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

C++ Programming Homework Help

  • 1. For any Homework related queries, Call us at : - +1 678 648 4277 You can mail us at :- info@cpphomeworkhelp.com or reach us at :- https://www.cpphomeworkhelp.com/
  • 2. Problems Initializing multidimensional arrays: The examples of multidimensional arrays in lecture did not give the syntax for initializing them. The way to assign a multidimensional array in the array declaration is as follows: int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} }; Inside the inner braces, commas still separate individual numbers. Outside, they separate rows of the array. After this declaration, matrix[0][1] will return the value 2. Each one of the rows is initialized like a regular array. For instance, if instead of {1, 2, 3} we’d put {1}, the second and third elements of the first row would have been initialized to 0. 1. Write a program that declares the 2D array of student test grades shown below, and stores the students’ averages in a 1D array. Each row in the 2D array represents the grades of a particular student (their parents uncreatively named them Student 0, Student 1, etc.). Output the array of averages. int studentGrades[6][5] = { {97, 75, 87, 56, 88}, {76, 84, 88, 59, 99}, {85, 86, 82, 81, 88}, {95, 92, 97, 97, 44}, {66, 74, 82, 60, 85}, {82, 73, 96, 32, 77} }; cpphomeworkhelp.com
  • 3. Multidimensional arrays as arguments: The syntax for passing multidimensional arrays as arguments is very similar to that of passing regular arrays. The main difference is that you need to include the number of columns in the function definition. For instance, a function that takes a 2-dimensional array with 3 columns might be declared as follows: void func( int array[][3], const int rows, const int columns ) {…} 2. Write a function based on your program from question 1 to calculate a set of grade averages. It should take a few arguments, including a 2D array of student grades and a 1D array to store the averages in. (Recall that arrays are passed by reference.) It should return nothing. 3. Multidimensional arrays are often used to store game boards. A tic-tac-toe board is a basic example: the board can be stored as a 2D array of integers, where a positive number represents Player 1, a negative number represents Player 2, and 0 represents a space not yet taken by either player. Write a void function printTTTBoard that takes such a 2D array as an argument and prints the board as a series of X’s, O’s, or spaces, separated horizontally by tabs and vertically by newlines. For instance, it should output the following board for the array cpphomeworkhelp.com
  • 4. 4. Assume that the following variable declaration has already been made: char *oddOrEven = "Never odd or even"; Write a single statement to accomplish each of the following tasks: a. Print the 4th character in oddOrEven b. Create a pointer to a char value named nthCharPtr that stores the memory address of the 4th character in oddOrEven c. Using pointer arithmetic, update nthCharPtr to point to the 7th character in oddOrEven d. Print the value currently pointed to by nthCharPtr e. Create a new pointer to a pointer (a char ** – yes, those are legal too!) named pointerPtr that points to nthCharPtr f. Print the value stored in pointerPtr g. Update nthCharPtr to point to the next character in oddOrEven (i.e. one character past the location it currently points to) h. Using pointer arithmetic, print out how far away from the character currently pointed to by nthCharPtr is from the start of the string cpphomeworkhelp.com { {0, -1, 1}, {-1, 1, -1}, {-1, 1, 1} }: O X O X O O X X
  • 5. 5. Write a function swap that takes two arguments – pointers to character values – and swaps the values of the variables these pointers point to. It should return nothing. 6. a. Write a function reverseString that takes one argument – a C-style string (a char *) – and reverses it. The function should modify the values of the original string, and should return nothing. (You may assume that you have a properly implemented swap function as described in the previous problem. You may also use the strlen function, which takes a char * and returns the length of the string. It is found in the cstring standard header file.) b. Indicate with two lines of code how you would declare a character array (a char[], not a char*) containing the string "aibohphobia", and reverse it using reverseString. Function pointers: We mentioned in lecture that, since functions are really just blocks of instructions in memory, you can create a pointer to a function. Schematically, the syntax for declaring such a pointer is as follows: return_type (*name)(argument_types); The syntax for declaring the pointer is identical to the syntax for declaring a function, except that where you’d put the name of the function in the function definition, you put (*pointerName). To give an example: cpphomeworkhelp.com
  • 6. void (*functionPtr)(int, char) = &someFunction; // & is optional Here, we declare a function pointer named functionPtr which points to some function named someFunction. That function must be a void function that takes as arguments one int and one char. Once we’ve declared functionPtr as a function pointer, we can use it to call the function it points to simply by writing something like functionPtr(1, 'a'). 7. a. Define two functions, cmpAscending and cmpDescending. Each should take two integers. cmpAscending should return true if the first integer is greater than the second and false otherwise. cmpDescending should do the reverse. Each one should be no more than 3 lines of code. b. One common application of function pointers is sorting – you can pass a function pointer to a sorting function to decide whether to sort in ascending or descending order. Extract the nested loop from the bubble sort code snippet from the Lecture 4 notes and place it into its own function. The function should take 3 arguments: an integer array, the length of the array, and a function pointer. The function pointer (call it comparator) should be of a type that points to a function that takes two integers and returns a boolean. Modify the sorting code snippet to use comparator to compare values for sorting, and show how you’d call your sort function with either cmpDescending or cmpAscending. cpphomeworkhelp.com
  • 7. 8. Write a line to accomplish each of the following tasks. You will want to look up the documentation for string functions online. (Recall that the way to call a function on a string variable is, for instance, myString.find("a"). Also note that the usual cin and cout syntaxes work for strings, but cin will stop reading at the first whitespace character.) a. Declare a variable papayaWar of type string (assuming header file <string> has been included). Initialize it to the value "No, sir, away! A papaya war is on!" b. Print the index of the first instance of the exclamation point character in papayaWar. c. Print the length of the papayaWar string. d. Print the last 5 characters of papayaWar. e. Replace the contents of the string with a string that the user enters. f. Append the string "rotator" to the end of papayaWar. g. Print the contents of papayaWar. cpphomeworkhelp.com
  • 8. 1. #include <iostream> using namespace std; int main() { int grades[6][5] = { {97, 75, 87, 56, 88}, {76, 84, 88, 59, 99}, {85, 86, 82, 81, 88}, {95, 92, 97, 97, 44}, {66, 74, 82, 60, 85}, {82, 73, 96, 32, 77} }; double averages[6]; for (int i = 0; i < 6; i++) { int sum = 0; for (int j = 0; j < 5; j++) { sum += grades[i][j]; } averages[i] = sum/5.0; } for (int k = 0; k < 6; k++) cout << "Student " << k << " has an average of " << averages[k] << endl; return 0; } cpphomeworkhelp.com Solutions
  • 9. 2. void average( int grades[][5], const int rows, const int columns, double averages[] ) { for (int i = 0; i < rows; i++) { int sum = 0; for (int j = 0; j < columns; j++) { sum += grades[i][j]; } average [i] = sum / static_cast<double>(columns); } } 3. void printTTTBoard(int array[][3]) { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cout << (array[i][j] > 0 ? 'X' : (array[i][j] == 0 ? ' ' : 'O') ) << 't'; } cout << endl; } } cpphomeworkhelp.com
  • 10. 4. a. cout << oddOrEven[3]; // or cout << *(oddOrEven + 3); b. char *nthCharPtr = oddOrEven + 3; (or, less preferably, &oddOrEven[3]) c. nthCharPtr += 3; d. cout << *nthCharPtr; e. char **pointerPtr = &nthCharPtr; f. cout << pointerPtr; g. nthCharPtr++; h. h. cout << nthCharPtr - oddOrEven; 5. void swap( char *xPtr, char *yPtr ) { char temp = *xPtr; *xPtr = *yPtr; *yPtr = temp; } 6. a. void reverseString ( char string[] ) { int stringLen = strlen(string); for (int x=0; x< stringLen/2; x++) { int y = stringLen-x-1; swap (string[x], string[y]); } } cpphomeworkhelp.com
  • 11. b. char aibophobia[12] = {'a', 'i', 'b', 'o', 'h', 'p', 'h', 'o', 'b', 'o', 'a', '0'); reverseString(aibophobia); 7. bool cmpAscending(int a, int b) { return a < b; } bool cmpDescending(int a, int b) { return a > b; } void sort(int arr[], const int arrLen, bool (*comparator)(int,int)) { for(int i = 0; i < arrLen; i++) { for(int j = 0; j < i; j++) { if( comparator(arr[i],arr[j]) ) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } cpphomeworkhelp.com
  • 12. 8. a. string papayaWar = "No, sir, away! A papaya war is on!"; b. cout << papayaWar.find('!'); // or find("!") c. cout << papayaWar.length(); d. cout << papayaWar.substr( papayaWar.length() - 5 ); or, less preferably, cout << papayaWar.substr( 29 ); e. getline(cin, papayaWar); // or cin >> papayaWar, but that // will stop at whitespace f. papayaWar.append("rotator"); // or papayaWar += "rotator"; g. cout << papayaWar; cpphomeworkhelp.com