Question

What's wrong with my code? : I'm trying to use recursive functions to display and count...

What's wrong with my code? :

I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers .

Here's the output I want:

Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are.
2 8 14 18 22

MY CODE IS AT THE VERY BOTTOM OF THE PAGE

Here are the complete instructions to give you an Idea of what we're up against....

Write a program that uses a recursive function to find all even numbers in an array.

1.) Declare an array named  values
- Size = 10
- Initialize the array with values like these: 2, 5, 7 ,8, 11, 14, 17, 18, 22, 23

2.) In main(), call a function named  showEvens
- The function is a recursive, void-returning function.
- The array, as well as one or two other parameters is passed to the function.
- The function begins by checking the first array value (at index 0) to see if it’s
an even number.
o When the program is finished, only even numbers are displayed. (see
output)

- Each time the function is called (recursively), one array value is checked to see if it
is even.
o If an even number is encountered, a count variable is incremented. The
count variable keeps track of the number of even numbers in the array.

3.) Remember, that a recursive function must have a base case that stops the recursion.
- So after all array values have been checked, the recursion stops.

4.) In the end, the number of even numbers is display, along with the numbers.

/* OUTPUT
Here are the 5 even numbers:
2 8 14 18 22

MY CODE:

Overall, the program won't allow me to increment the amount of even integers. That's the problem I have.

# include <iostream>
# include <cstring>
# include <string>
using namespace std;

void showEvens(int values[], int index);

int main()
{


   int values[10] = { 2,5,7,8,11,14,17,18,22,23 };
   int evenCounter; // The variable used to count the number of even integers
   int index;

   index = 0;
evenCounter = 0;

   showEvens(values, index);
   cout << endl;


   cout << evenCounter; // I wan't this output "5". However, the program won't allow me.
  
  
   cout << endl;
   system("pause");
   return 0;
}

void showEvens(int values[], int index)
{
   int evenCounter;

   if (index == 10) // Base Case -- The Function is exited when the index is 10
   {

       return;

   }
  
   if ((values[index] % 2) == 0) // Determines whether or not an element array is even
   {

       cout << values[index] << '\t';
evenCounter++; // Including this part will result in an error.  I don't understand why . It won't allow me to increment.
      
   }
  
   showEvens(values, index + 1); // The index gets incremented every time the function is called

}

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

C++ Program:

# include <iostream>
# include <cstring>
# include <string>

using namespace std;

/* Update function prototype so that variable evenCounter is passed as reference variable */
void showEvens(int values[], int index, int &evenCounter);

int main()
{

int values[10] = { 2,5,7,8,11,14,17,18,22,23 };
int evenCounter; // The variable used to count the number of even integers
int index;
index = 0;
evenCounter = 0;

/* Pass variable eventCounter as reference variable */
showEvens(values, index, evenCounter);

cout << endl;

cout << evenCounter; // I wan't this output "5". However, the program won't allow me.


cout << endl;
system("pause");

return 0;
}

/* Update with one more variable of reference type */
void showEvens(int values[], int index, int &evenCounter)

{
if (index == 10) // Base Case -- The Function is exited when the index is 10
{
return;
}

if ((values[index] % 2) == 0) // Determines whether or not an element array is even
{
cout << values[index] << '\t';
evenCounter++; // Including this part will result in an error. I don't understand why . It won't allow me to increment.
}

/* Change function call by including extra variable */
showEvens(values, index + 1, evenCounter); // The index gets incremented every time the function is called

}

__________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
What's wrong with my code? : I'm trying to use recursive functions to display and count...
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
  • C++, THIS IS FOR AN UNSORTED ARRAY. I'm trying to implement an "if" statement that will...

    C++, THIS IS FOR AN UNSORTED ARRAY. I'm trying to implement an "if" statement that will check if there are 2 or more modes in a data set. I tried checking "if (count == total)" then it would increment "pos" by 1 and put the other mode in the list (m[pos-1] = values[index]), but it kept giving me the same number multiple times. For instance, if the data set was {1,5,10,4,7,3,8,3,9,10} (the 3 and 10 occur twice) then i would...

  • DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...

    DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted.    Task 1 – Reverse array (3 pts) Given an integer array like: const...

  • Need done in C++ Can not get my code to properly display output. Instructions below. The...

    Need done in C++ Can not get my code to properly display output. Instructions below. The code compiles but output is very off. Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs, it should ask the user...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

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