Guys I have difficulty with this exercise would you give me a hand

Write a procedure that calculates the sum of the numbers returned by the call to a function which, given an integer, returns the nearest even number (the same number if it is even, otherwise the number increases by one). The function is called 3 times with current parameter given by the user. Example: if the function is called with parameter 5, then 2 and then 7, the procedure calculates 6 + 2 + 8. The main press 16.
If a number is even, use the% function which gives the remainder of the division of two numbers


#include <iostream>
using namespace std;

int paridisp(int n){
cout<< "Inserire un valore ";
cin>>n;
if (n%2==0){
contaggioPari = conteggioPari+1;
}
else {
conteggioPari=conteggioDispari+1;
}

return n;
}

int main(int argc, char** argv) {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int even(int no)
{
	return no + no % 2;
}

int main()
{
	int sum {};

	std::cout << "Enter 3 numbers: ";

	for (int i {}, no {}; i < 3 && (std::cin >> no); sum += even(no), ++i);

	std::cout << "The even sum is: " << sum << '\n';
}



Enter 3 numbers: 5 2 7
The even sum is: 16

1
2
3
int main(int argc, char** argv) {
  return 0;
}

This program does nothing, because it has only one statement: return 0;


1
2
3
4
5
6
7
8
9
10
11
12
13
int paridisp( int n ) {
  cout<< "Inserire un valore ";
  cin >> n; // This discards the value that the function was called with
  // Why require a parameter, if you don't use it?

  if ( n%2==0 ) {
    contaggioPari = conteggioPari+1; // What is contaggioPari? Undeclared variable
  }
  else {
    conteggioPari=conteggioDispari+1; // What is contaggioDispari? Undeclared variable
  }
  return n;
}

I don't know those words, but I bet that part of that code is from program that counts the even and odd numbers.

There is no counting here. (Only) IF number is odd, THEN add one to number.
Calculate sum of numbers. At start sum is 0. Add each number to sum. At end you have the correct sum.
Hello insidethelollo98,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


First you should figure out what is wrong with your code before you try doing something different.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int paridisp(int n)
{
    cout << "Inserire un valore ";  // <--- Enter a value
    cin >> n;

    if (n % 2 == 0)
    {
        contaggioPari = conteggioPari + 1;
    }
    else
    {
        conteggioPari = conteggioDispari + 1;
    }

    return n;
}

int main(int argc, char** argv)
{
    return 0;
}

Just because the function is defined before "main" does not mean that it will be executed. In "main" you have to call the function.

In the definition of "main" if you are not using the "arg" variables you do not need to put them there.

Looking at the function the variables "contaggioPari" and "contaggioDispari" have already been discussed. In the if/else statement look closely at what is on the lhs of the (=). Is this correct?

Your program is a good start, but needs finished.

Andy
Your program is a good start,


?????????
Topic archived. No new replies allowed.