Question

In: Computer Science

Write the following task in C++1) Write the definition of a function numOccurrences thatsearches...

Write the following task in C++

1) Write the definition of a function numOccurrences that searches for a character in a character array and returns the number of times it occurs in the array. The function has three formal parameters: a char array array, an int variable arraySize representing the size of the array, and a character variable letter representing the character to be searched for in the array.

2) Assume the array numbers is an int array of size 10 and the array has been initialized, write C++ statements to find and output the smallest element of the array as well as the position of the smallest element in the array

Solutions

Expert Solution

---------q1.cpp----------------

#include
using namespace std;

//This function searches for a character in a character array and returns the number of times it occurs in the array
int numOccurrences(char array[], int arraySize, char c)
{
   int count = 0;
   for(int i = 0; i < arraySize; i++)   //loop for all the indexes of array
   {  
       if(array[i] == c){   //check if chracter at index i matches c
           count++;       //then increment the count
       }
   }
   return count;   //return the count
}

int main()
{
   char array[10] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'}; //declare char array
   cout << "The array is: ";      
   for(int i = 0; i < 10; i++)
   {
       cout << array[i] << ", ";       //print the array
   }

   //test the function numOccurrences by calling it
   cout << "\nThe number of occurrences of 'e' = " << numOccurrences(array, 10, 'e');
   cout << "\nThe number of occurrences of 'o' = " << numOccurrences(array, 10, 'o');
   cout << "\nThe number of occurrences of 'd' = " << numOccurrences(array, 10, 'd');
   cout << "\nThe number of occurrences of 't' = " << numOccurrences(array, 10, 't') << endl;
   return 0;
}

---------q2.cpp----------------

#include
using namespace std;

int main()
{
   int numbers[10] = {10, 12, 19, 22, 3, 33, 4444, 34, 9333, 93}; //int array of size 10 and the array has been initialized
   cout << "\nThe array is: ";
   for(int i = 0; i < 10; i++)
   {
       cout << numbers[i] << ", ";    //print the array
   }


   int smallest_element = numbers[0];   //initialize smallest element to index 0 element of array
   int smallest_index = 0;               //initialize smallest index to index 0

   for(int i = 1; i < 10; i++)           //loop from index 1 to the last index of array
   {
       if(numbers[i] < smallest_element){   //check if element at index i is smaller than the smallest_element
           smallest_element = numbers[i];   //update the smallest_element and smallest_index
           smallest_index = i;
       }
   }

   //print the smallest element and the index
   cout << "\nThe smallest element of the array is " << smallest_element << endl;
   cout << "The smallest element is present at index " << smallest_index << endl;
   return 0;
}

--------Screenshots---------------

--------Outputs---------------


Related Solutions

Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
1. For each of the following, write C++ statements that perform the specified task. Assume that...
1. For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in four bytes and that the starting address of the built-in array is at location 1002500 in memory. Declare an unsigned int built-in array values with five elements initialized to the even integers from 2 to 10. Assume that the constant size has been defined as 5. Declare a pointer vPtr that points to an object of type unsigned int....
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
In C++ Write the definition for following methods of List data structure. 1. setList – The...
In C++ Write the definition for following methods of List data structure. 1. setList – The function set the value of list elements equal to a value passed as the function input. 2. getAt – The function returns an element of the list referred by its position which is given to the function as input. 3. insertAt – The function inserts a given element passed as function input in the list at the specified position also passed as second function...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
answer in c++ , Write the definition of the function NthOccurrence() whose header is int NthOccurrence(Array&...
answer in c++ , Write the definition of the function NthOccurrence() whose header is int NthOccurrence(Array& data,const T& value,int n) template It returns the index of the nth occurrence of value in data. If n is not positive, value appears less than n times in data or data is empty, it returns -1.
C++ Write the definition of a function minMax that has five parameters. The first three parameters...
C++ Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */ **ONLY THE FUNCTION
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It...
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It returns the amount of times the maximum value of data appears in data. If data is empty, it returns 0. For instance, if data = [7, 1, 4, 9, 6, 7, 7, 3, 2, 6, 9, 5, 9], it will return 3 since 9 appears three times
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT