0

I need some help with this question

Complete the missing parts of this program. The inserted comments will help you in specifying the functionality of this program: #include<iostream > using namespace std; void exchange(int *a, int index1, int index2) { ​//This function swaps the two elements (specified by the parameters index1 //and index2) of the passed array (a). // Note: Do not use array notation ([]). You can only use pointer notations. } void printElements (int *a,int size) { ​// This function prints the array elements in one line separated by 3 spaces. // Note: Do not use array notation ([]). You can only use pointer notations. } void reverseElements(int *a,int size) { ​// This function stores the array elements in reverse order. // Note: Do not use array notation ([]). You can only use pointer notations. ​ } void main() { ​int a[5] = {10,20,30,40}; ​ ​exchange(a,2,3); ​ ​printElements(a,5); ​reverseElements(a,5); ​printElements(a,5);​ ​ }

3rd Oct 2020, 10:00 PM
Aya Dmaidi
Aya Dmaidi - avatar
2 Answers
+ 1
Array notation is a[index1] and pointer notation for this equalent is *(a+index1); By this you can implement swap and print elements.. For swap use a temporary variable and to print use a for loop from index 0 to 5.. For reverse, just traverse array elements from 5 to 0. Try these, and post your try if any problem... Hope it helps.
3rd Oct 2020, 10:14 PM
Jayakrishna 🇮🇳
0
#include <iostream> using namespace std; void exchange(int *a, int index1, int index2) { *a = index1; index1 = index2; index2 = *a; // to swaps the two elements } void printElements(int *a, int size) { for (; *a != '\0'; a++) cout << *a << " "; // to elements in one line } void reverseElements(int *a, int size) { int *p1 = a; // *p1 pointing at the beginning of the array int *p2 = a + size - 1; // p2 pointing at end of the array while (p1 < p2) { swap(p1, p2); p1++; p2--; } } int main() { int a[5] = { 10,20,30,40 }; exchange(a, 2, 3); printElements(a, 5); reverseElements(a, 5); printElements(a, 5); return 0; } sorry for being late is this right ??
21st Oct 2020, 6:10 PM
Aya Dmaidi
Aya Dmaidi - avatar