C/C++ Even summation with recursive function in c++

AI Thread Summary
The discussion focuses on a beginner's attempt to write a C++ program that calculates the sum of even numbers from user-inputted values. The user is tasked with entering a specified number of integers, and the program should utilize a recursive function to compute the sum of even numbers only. The initial code provided has several issues, including an infinite recursion problem due to the lack of a stopping condition and the incorrect handling of the `sumEven` variable, which is declared locally and thus does not retain its value across recursive calls. Key suggestions for improvement include decrementing the argument in the recursive function to avoid infinite loops and passing `sumEven` as an argument to maintain its value throughout the recursion.
NewDesigner
Messages
1
Reaction score
0
Hi,

I'm a beginner in C++.
I wan't to write this program:

Write a program that asks the user to enter n numbers –where n entered by the user- and calculates the sum of even numbers only. main function asks the user to enter n and then calls the recursive function Sum to read the values entered by the user. The function Sum returns the summation of even numbers only. At the end, main prints the summation that is returned by the recursive function.

Sample run:

How many numbers do you have:? 6
Enter number:3
Enter number:2
Enter number:9
Enter number:5
Enter number:6
Enter number:8
Sum of odd numbers = 16

I solve this question in that way, and i know it's wrong. can anybody help me please?

PHP:
#include<iostream>

using namespace std;

double sum(int);

int main()
{
	int n;

	cout << "How many numbers do you have? ";
	cin >> n;

	sum(n);

	cout << "Sum of even numbers = " << sumEven << endl;

	return 0;
}

double sum(int a)
{
	int num;
	double sumEven=0;
	double sumOdd=0;

	cout << "Enter number: ";
	cin >> num;

	if(num%2==0)
	{
		sumEven += num;
		sum(num);
	}

	else if(num%2==1)
	{
		sumOdd += num;
		sum(num);
	}
		
	return sumEven;
}
 
Technology news on Phys.org
Few hints

1) Decrement your argument 'a' each time you get a number , otherwise the recursion executes infinitely. you must have a stopping condition.

2)Use sumEven (sumOdd also if you require sum of odd nos) as an argument in your function, rather than declaring it as a local variable. This way you will be able to use the same variable in each function (retaining previous value). Local declaration creates a new variable each time the function is called.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top