lost in processing .

Write a program in C++ that reads integer values continuously until zero or negative value is entered. Then program should displays information about each value as follows:
v1 ==> Avg. Of Even Digits: d.dd, Largest Digit: d
v2 ==> Avg. Of Even Digits: d.dd, Largest Digit: d ... etc.

The program should use the following functions:
getAvgEvenDigits( ): a function that takes one value and returns the average of even digits.getMaxDigit( ): a function that takes one value and returns the maximum digit of the value.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include<iostream >
using namespace std;

int GetAvgEvenDigit(int );
int GetMaxDigit(int );

int main()

 {

int n,x,y,m,v;

do{

cout<<"Enter only (+ve) numbers until zero or negative number is entered \n";
   cin>>n;

}while(n>0);

v=GetAvgEvenDigit(x);

m=GetMaxDigit(y);


cout<<"avg of even digit "<<x<<" is = "<<v;
cout<<" , largest digit  of "<<y<<" is = "<<m;


return 0;
}

int GetAvgEvenDigit(int x )
{
int i,count,v;

for(i=0;i<=x;i++)
   if(x%2==0)
   {
    count++;
   }
   v=x/count;

return v;

}

int GetMaxDigit(int y )

{
int m;
    if(y>m)
    y=m;

   return m;

} 

Last edited on
Do you have a question?
Slow down!
Start learning to write and test as you go.

Step 1.
1
2
3
4
5
int main ( ) {
    int x = 12345;
    int v = GetAvgEvenDigit(x);
    cout<<"avg of even digit "<<x<<" is = "<<v;
}


Step 2, add GetMaxDigit() function.

Step 3, add a prompt for x in main.

Step 4, put the prompt in a loop, until x <= 0.
@helios yes the question is how to operate each function.
Salim c : I didn’t understand but what i know is that x is my input and it is not only one input i should have group of different x values .
But my sequence is right this is the prototype function Style and this what i’ve learned.

See the display it is v1
V2
Actually i have a pic of the display but idk how to upload it here .. can anyone tell me if the website Allows this somehow.
Last edited on
What we're saying is you need to start small and grow the program from there.

You are trying to get the digits of a number, and then do further processing on those digits. That is not what either of your existing functions are doing.

Given a number, 12345,
• 12345 % 10 = 5
• 12345 / 10 = 1234

You can see that using % 10 gets the the least-significant digit, and doing / 10 removes that least-significant digit. From this, you can write a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using std::cout;

void print_digits(int n)
{
    while (n > 0)
    {
        cout << n % 10 << '\n';   
        n = n / 10;
    }
}

int main()
{
    print_digits(12345);
}


Now that you can separate each of the digits of a number, you should able to determine whether the digit is even, or find the max digit, etc.
Last edited on
this may be easier if you read the numbers as strings.
you don't have to, but that lets you see each digit a little easier. this is possibly not the intent, but it says 'value' not 'integer' in the spec, and a string can have a value.
Last edited on
thanks guys I will try to rewrite the code .
Last edited on
Topic archived. No new replies allowed.