In: Computer Science
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
#include<bits/stdc++.h>
using namespace std;
void swapElements(char aray[],ifstream &file)
{
int count= 0;
int val;
int index1,index2; //declaring index variable to store
index from file
int arr[30];
while (count < arr[2] && file >>
val)
arr[count++] =
val; //storing
index first in array
for (int i = 0; i < count; i++) {
index1=arr[0];
//assigning index from array to index variables
index2=arr[1];
}
char temp;
temp=aray[index1];
//swapping character array element with index1 and index2
aray[index1]=aray[index2];
aray[index2]=temp;
}
int main()
{
ifstream file("C:\\Users\\hp\\Desktop\\file.txt"); //importing file using ifstream object
char aray[6]={'s','w','g','j','q','n'};
swapElements(aray,file); //calling function by passing array and file
for(int i=0;i<6;i++)
{
cout<<aray[i]<<" ";
}
file.close(); //closing file
}
input file-
Output-