complete the //TODOs  #include #include "swap.h" using namespace std; int main(int argc, char const *argv[]) { intfoo[5] = {1, 2, 3, 4, 5}; cout<<"Addresses of elements:"<>a; cout<<"Second index"<>b; cout<<"Enter 0 for pass-by-value, 1 for pass-by-pointer"<>f; switch (f) { case0: // Pass by Value // Compare the resultant array with the array you get after passing by pointer cout<<"Before swapping"<>flag; } cout<<"Reversing the array"; // Reverse your array // TODO complete the function in swap.cpp file reverse(foo,5); cout<<"\nAfter reversing"<,5); cout<<"\nAfter cumulative summation"<

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 17PE
icon
Related questions
Question
complete the //TODOs 
#include<iostream>
#include "swap.h"

using namespace std;
int main(int argc, char const *argv[])
{
intfoo[5] = {1, 2, 3, 4, 5};

cout<<"Addresses of elements:"<<endl;
//TODO Print the addresses of array elements


cout<<endl;

cout<<"Elements:"<<endl;;
//TODO Print all the elements using pointers

cout<<endl;

inta,b;
intf;
intflag = 1;
while(flag == 1){
cout<<"Enter indices of elements you want to swap?"<<endl;
cout<<"First index"<<endl;
cin>>a;
cout<<"Second index"<<endl;
cin>>b;
cout<<"Enter 0 for pass-by-value, 1 for pass-by-pointer"<<endl;
cin>>f;

switch (f) {
case0:
// Pass by Value
// Compare the resultant array with the array you get after passing by pointer
cout<<"Before swapping"<<endl;
for(inti = 0;i<5;i++){
cout<<foo[i]<<" ";
}
cout<<endl;

swap(foo[a],foo[b]);

cout<<"\nAfter swapping"<<endl;
for(inti = 0;i<5;i++){
cout<<foo[i]<<" ";
}
cout<<endl;
break;

case1:
// Pass by pointer
cout<<"Before swapping"<<endl;
for(inti = 0;i<5;i++){
cout<<foo[i]<<" ";
}
cout<<endl;

// TODO complete the function in swap.cpp file
swap_by_pointers(&foo[a],&foo[b]);

cout<<"\nAfter swapping"<<endl;
for(inti = 0;i<5;i++){
cout<<foo[i]<<" ";
}
cout<<endl;
break;

}
cout<<endl;
cout<<"Press 1 to continue else press 0"<<endl;
cin>>flag;
}

cout<<"Reversing the array";
// Reverse your array
// TODO complete the function in swap.cpp file
reverse(foo,5);

cout<<"\nAfter reversing"<<endl;
for(inti = 0;i<5;i++){
cout<<foo[i]<<" ";
}
cout<<endl;


// GOLD problem
// Cumulative sum of the array
// TODO call this function cumulative_sum properly so that it passes the array 'foo'
// and updates 'foo' with cumulative summation.
// After this function call, you will get the updated array here
// TODO complete the function declaration in swap.h file
// TODO complete the function in swap.cpp file
 
// cumulative_sum(<TODO>,5);

cout<<"\nAfter cumulative summation"<<endl;
for(inti = 0;i<5;i++){
cout<<foo[i]<<" ";
}
cout<<endl;

return0;
}
Expert Solution
Step 1

swap.h

void swap_by_value(int x,int y);
void swap_by_pointers(int *x,int *y);
void reverse_array(int x[],int n);
void cumulative_sum(int x[],int n);

swap.cpp

#include <iostream>
#include "swap.h"

void swap_by_value(int x,int y)
{
    int temp=x;
    x=y;
    y=temp;
}

void swap_by_pointers(int *x,int *y)
{
    int temp=*x;
    *x=*y;
    *y=temp;
}

void reverse_array(int x[],int n)
{
    int i=0,j=n-1;
    while(i<j)
    {
        int temp=x[i];
        x[i]=x[j];
        x[j]=temp;
        i++;
        j--;
    }
}

void cumulative_sum(int x[],int n)
{
    int sum=x[0];
    for(int i=1;i<n;i++)
    {
        sum=sum+x[i];
        x[i]=sum;
    }
}

main.cpp

 

#include<iostream>
#include "swap.h"
using namespace std;

int main(int argc, char const *argv[])
{
    int foo[5] = {1, 2, 3, 4, 5};
    cout<<"Addresses of elements:"<<endl;
    for(int i=0;i<5;i++)
    {
        cout<<&foo[i]<<" ";
    }
    cout<<endl;
    cout<<"Elements:"<<endl;;
    int *ptr=foo;
    for(int i=0;i<5;i++)
    {
        cout<<*ptr<<" ";
        ptr=ptr+1;
    }
    cout<<endl;
    int a,b;
    int f;
    int flag = 1;
    while(flag == 1)
    {
        cout<<"Enter indices of elements you want to swap?"<<endl;
        cout<<"First index"<<endl;
        cin>>a;
        cout<<"Second index"<<endl;
        cin>>b;
        cout<<"Enter 0 for pass-by-value, 1 for pass-by-pointer"<<endl;
        cin>>f;

        switch (f)
        {
            case 0:
                // Pass by Value
                // Compare the resultant array with the array you get after passing by pointer
                cout<<"Before swapping"<<endl;
                for(int i = 0;i<5;i++)
                {
                    cout<<foo[i]<<" ";
                }
                cout<<endl;

                swap_by_value(foo[a],foo[b]);

                cout<<"\nAfter swapping"<<endl;
                for(int i = 0;i<5;i++)
                {
                    cout<<foo[i]<<" ";
                }
                cout<<endl;
                break;

            case 1:
                // Pass by pointer
                cout<<"Before swapping"<<endl;
                for(int i = 0;i<5;i++)
                {
                    cout<<foo[i]<<" ";
                }
                cout<<endl;

                swap_by_pointers(&foo[a],&foo[b]);

                cout<<"\nAfter swapping"<<endl;
                for(int i = 0;i<5;i++)
                {
                    cout<<foo[i]<<" ";
                }
                cout<<endl;
                break;

        }
        cout<<endl;
        cout<<"Press 1 to continue else press 0"<<endl;
        cin>>flag;
    }

    cout<<"Reversing the array";
    // Reverse your array
    reverse_array(foo,5);

    cout<<"\nAfter reversing"<<endl;
    for(int i = 0;i<5;i++){
    cout<<foo[i]<<" ";
    }
    cout<<endl;


    // GOLD problem
    // Cumulative sum of the array
    // TODO call this function cumulative_sum properly so that it passes the array 'foo'
    // and updates 'foo' with cumulative summation.
    // After this function call, you will get the updated array here
    // TODO complete the function declaration in swap.h file
    // TODO complete the function in swap.cpp file

    cumulative_sum(foo,5);

    cout<<"\nAfter cumulative summation"<<endl;
    for(int i = 0;i<5;i++){


    cout<<foo[i]<<" ";
    }
    cout<<endl;

    return 0;
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning