Question
In C++ modify the code as much as possible to use vectors instead of arrays. Is the output different? 1. void swap(int &a, int &b)
In C++ modify the code as much as possible to use vectors instead of arrays. Is the output different?
1.
void swap(int &a, int &b) { int hold; hold = a; a = b; b = hold; }
int main() {
int x[5] = {14, 3, 9}; // what is the value of x[3]?
swap(x[0], x[2]);
swap(x[1], x[3]);
for(int i=0; i<4; i++)
cout << x[i] << ", ";
}
2.
void dostuff(char a[], char b[]);
int main() {
char s1[] = "PAPOA";
char s2[] = "WEHLO";
dostuff(s1, s2);
cout << s1;
}
void dostuff(char a[], char b[]) {
a[0] = 'Y';
a[2] = b[2];
a[4] = b[4];
}
3.
const int ROWS=2, COLS=3;
int a[ROWS][COLS] = { {2, 4, 6}, {7, 5, 3} };
for(int i=0; i for(int j=0; j cout << a[i][j] << ", "; cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started