Question

The function takes three parameters: string array, int arraySize, and string word The function will loop...

  1. The function takes three parameters: string array, int arraySize, and string word
  2. The function will loop through the string array. If word is found in the array, return the matched element’s position (index) in the array. Otherwise, return -1.
  3. When a match is found in the array, the loop should be terminated at the time --- no need to continue the loop.
  4. If no match is found, the function should display all elements in the array, as well as the search word.
  5. Please make sure the function cannot modify any data in the array.

USE C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
int search(string array[],int arraySize,string word)
{
   int index;
   for(int i=0;i<arraySize;i++)
   {
       if(word==array[i])
       return i;
   }
   cout<<"array elements : ";
   for(int i=0;i<arraySize;i++)
   {
       cout<<array[i]<<" ";
   }
   cout<<"\nThe word is : "<<word;
   return -1;
}
int main() {
   string arr[]={"HomeworkLib","india","us","uk"};
   string word="us";
   int index=search(arr,4,word);
   if(index>=0)
   cout<<"index : "<<index;
   return 0;
}

OUTPUT:-

// If any doubt please comment

Add a comment
Know the answer?
Add Answer to:
The function takes three parameters: string array, int arraySize, and string word The function will loop...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • Given an array and a starting position write a function replaceFromN, that takes an integer array...

    Given an array and a starting position write a function replaceFromN, that takes an integer array 'array', the size of the array size and a starting positions 'n' as parameters and replaces the elements starting from that index onward with the sequence 1,2,3,... The function returns nothing. void replaceFromN(int array[], int size, int n) For example, given array= {15,12,4,9,2,3} n =2 the function should modify array to be {15,12,1,2,3,4}

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • 50 MARKS TOTAL 3 Marks Question 1 If word c is present in array w of...

    50 MARKS TOTAL 3 Marks Question 1 If word c is present in array w of words, the following method is intended to return the index corresponding to the matched value. If c is not present, n (the number of elements in the array) is to be returned. At int match( String w, int n, String c) { for int i 0; i < n; it+) if w[i].equals( c) ) break; return i; Ar ae oakch C91g wntn g However,...

  • (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of...

    (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert insertItem in the array provided at the position specified by index. If index is out of range, output the following: Position of the item to be inserted is out of range or if the index is negative: Position of the item to be inserted must be nonnegative...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and...

    help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and an int parameter. If both string parameters represent binary numbers and the int parameter is equal to a positive number less than or equal to the length of the longest string parameter, the function should return a binary string whose length is equal to two times the length of the maximum length of the two string parameters whose value is equal to the sum...

  • Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array...

    Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

  • Need help with these array problems. int countAllPunctuation( const string array[ ], int n ); Return...

    Need help with these array problems. int countAllPunctuation( const string array[ ], int n ); Return the total number of punctuation symbols found in all the elements of the passed array argument. For the purpose of this function, the characters '.' , ',', '!', ';', ''', '-', '/', ':', '?', '"' count as punctuation symbols (that is, period, comma, exclamation mark, semicolon, apostrophe, dash, slash, colon, question mark, and double quote). Return -1 if n <= 0. For example, for...

  • Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an...

    Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an integer array index: index at which to insert the new item numitems: number of items currently in the array arrayCapacity: capacity of the array newVal: value to insert into the array If the array is at capacity then the function should return-1. Otherwise, insert newVal at index and return O for success int insertInArray(int listl 1, int index, int numItems, int arrayCapacity, int newVal)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT