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"<

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education