Question: How can I modify my code (below) to meet the following requirements?: The program should now search for two numbers in the array instead of

How can I modify my code (below) to meet the following requirements?:

The program should now search for two numbers in the array instead of one, and instead of counting them, it should report how many of the two numbers that occur in the array. Task 1: Modify the function header so it looks like this: int countElement(int inputArray[], int arraySize, int element1, int element2) The function should now be modified to return either 0, 1 or 2, according to the following: Return 0 if neitherelement1 nor element2 occur in inputArray. Return 1 if exactly one of element1 or element2 occurs at least once in inputArray. Return 2 if bothelement1 and element2 occur at least once in inputArray.

Task 2: Modify main() so that it asks the user for a second number to be searched for, and make the text give an answer like in the example runs based on the return value from countElement() instead of counting.

Example run:

Example runs (user input in bold, comments not partof the output initalic): Number: 5 Number: 7 Number: 3 Number: 9 Number: 1 Number: 10 Number: 8 Number: 1 Number: 9 Number: 7 What to search for: 9 And another number to search for: 1 Both numbers occur in the array Another example, same array as above:

What to search for: 9 And another number to search for: 13 One of the numbers occurs in the array Yet another example, same array as above: What to search for: 24 And another number to search for: 13 None of the numbers occur in the array

My code:

#include #include #define SIZE 10 int CreateGenerator(int* arr) { int i; for (i = 0; i < SIZE; i++) { arr[i] = rand() % 10 + 1; printf("number: %d ", arr[i]); } return arr; } int countElement(int* inputArray, int arraySize, int elementToCount) { int count = 0; for (int i = 0; i < SIZE; i++) { if (elementToCount == inputArray[i]) { count++; } } return count; } int main() { int arr[SIZE]; int number, choice = 1; while (choice == 1) { CreateGenerator(arr); printf("What to search for: "); scanf_s("%d", &number); int occured = countElement(arr, SIZE, number); printf("The number %d occurs %d times ", number, occured); printf("Do you want to generate a new sequence (1 for yes, 0 for no)? "); scanf_s("%d", &choice); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!