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

Similar to C++ Programming Homework Help

The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
rajatryadav22
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Unit 2
Unit 2Unit 2
Unit 2
TPLatchoumi
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
Session 4
Session 4Session 4
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
arshpreetkaur07
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2docSrikanth
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
ManjeeraBhargavi Varanasi
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
SURBHI SAROHA
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
NelyJay
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
C++ Homework Help
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYRajeshkumar Reddy
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
lodhran-hayat
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
Mrhaider4
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
Sowri Rajan
 

Similar to C++ Programming Homework Help (20)

The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Session 4
Session 4Session 4
Session 4
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 

More from C++ Homework Help

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
C++ Homework Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
C++ Homework Help
 

More from C++ Homework Help (18)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.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