Write a function search() that receives an array of char type, its size and anot
ID: 3745972 • Letter: W
Question
Write a function search() that receives an array of char type, its size and another character, finds the location (the subscript value) of the character in the array that matches the received character and returns this location number.
The program I have is however it does not match any values to the array and it only outputs -1, What is wrong with it?:
#include<iostream>
using namespace std;
int search(char arr[], int size, char ch)
{
int b;
for(b = 0; b < size; ++b) {
if(ch==arr[b])
return b;
else if(ch!=arr[b])
{
return -1;
}
}
}
int main()
{
int size=0, i=0;
char arr[size], ch=0;
int x=search(arr, size, ch);
cout<<"Enter size"<<endl;
cin>>size;
cout<<"Enter characters"<<endl;
for(i=0; i<size; i++ )
{
cin>>arr[i];
}
cout<<"Enter character to find subscript"<<endl;
cin>>ch;
cout<<x<<endl;
return 0;
}
Explanation / Answer
#include using namespace std; int search(char arr[], int size, char ch) { int b; for (b = 0; bRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.