some simple questions

hi guys,

i have some questions, i did most of them but here are a few questions that i couldn't do it, so i want you help me to understand how to solve them

Q1- i want to write programme to count the even and oss numbers

example: the user input is 12345
so: even= 2 , odd = 3

this is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int even[5]={0,2,4,6,8};
	int odd[5]={1,3,5,7,9};
	int n[10];
	int count_even = 0 ,count_odd = 0;
		
		for(int j=0; j < 10; j++)
		{
			cin>>n[j];
			
			for(int i=0; i < 5; i++)
			{
				
				if(n[j] == even[i]) count_even++;
				if(n[j] == odd[i])  count_odd++;
				
			}
						
		}
		cout << count_even << " " << count_odd;


can you show me what was my mistake?





Q2- ask uesr to input a number then display this

example: input = 5

output:
*****
****
***
**
*

this is my code
1
2
3
4
5
6
7
8
9
10
11
int n;
		cin >> n;
		for(int i = 0; i <= n; i++)
		{
			for(int j=0; i <=n; j++){
			cout << "*";
			n--;
			}
			
			
		}



Q3-ask user for ten numbers and then dsiplay the average

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int numbers[10];
	int sum = 0;
	double average = 0;
	
	cout << "Enter 10 numbers one by one: " << endl;
	
	
	for(int i = 0; i > 10; i++){
	cin >> numbers[i];
	}
	int i = 0;
	while (i < 10)
	{
		sum += numbers[i];
		

		i++;
	}
	
	// Calculate the average of the 10 numbers.
	average = sum / 10.0;
	
	cout << "The average is " << average << endl;


i really don't know what is the wrong in my code? especially the to get ten numbers

i can do it by using while loop but i want to use for loop

A1 - Don't really see anything wrong with the code itself. Note though that 12345 is one number. You want to enter 1 2 3 4 5. On the other hand, your approach is a little wrong here. A number is even if it is divisible by 2 = the remainder of division by two equals 0. That is:if(number % 2 == 0) cout << "even"; else cout << "odd";. What you do here is silly.

A2 - First loop i from 0 to n, then you have to loop j from 0 to something that gradually decreases. I wonder if that could be i (that is 5-i)?

A3 - line 8 : i > 10. That condition is never met, so you get no input.
thanks for your replay

this is my code for Q1
1
2
3
4
5
6
7
8
9
10
11
int n;
	int count_even = 0 ,count_odd = 0;
	
		cin>>n;
				if(n % 2 == 0){ 
				count_even++;
				}else{
				count_odd++;
				}
			
		cout << count_even << " " << count_odd;


but i should consider: 123456 as one number, so this code not works fine, how to do that


for Q2, i can't understand , can you explain more

for Q3, opps you right, thanks it works now
hi, for A1-- try to solve in this way:

int i;
do {
cout << "Type a number: ";
cin >> i;
if (i%2 != 0)
cout << "The number is odd\n";
else
cout << "The number is even\n";
} while (i != 0) ;

and worked on it
hi there,

thanks for the code but i should consider: 123456 as spreate numbers, so this code not works fine, how to do that

example: the user input is 12345
so: even= 2 , odd = 3
closed account (EzwRko23)
Code for A1:

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
using namespace std;

int main() {
  string number;
  cin >> number;
  int even = 0;
  for (string::iterator it = number.begin(); it != number.end(); ++it)
    if (((*it - '0') & 1) == 0)
      ++even;
  cout << "Even: " << even << "\n";
  cout << "Odd: " << (number.size() - even) << "\n";
}



Scala:
1
2
3
4
val number = readLine;
val even = number.count(x => (x.toInt & 1) == 0)
println("Even: " + even)
println("Odd: " + (number.length - even))

Do you consider the number 0 to be even or odd? Do you allow leading zeroes?
(eg, should the user input 0123456 generate the same answer as 123456?)

xorebxebx gave you a quite overblown implementation in C++ so that he could
provide a comparison with other languages (an unfair comparison anyway, since the
scala implementation uses a different algorithm); I would ignore that post, it won't
be useful to you anyway, as your instructor is probably not looking for you to do
string processing.

When you read in an integer, say 123456, you want to process one digit at a time.
If you divide the integer by 10 and take the remainder, you get the last digit of the
integer. If you then take that quotient, you are left with the rest of the digits. So
for example, 123456 % 10 = 6 (the last digit) and 123456 / 10 = 12345 (all of
the remaining digits). Now you can check the last digit for evenness, and repeat
the algorithm on the remaining digits (12345).

So obviously I'm suggesting a loop when I say "repeat the algorithm...". The only
trick here, which I'll let you think about, is to figure out when the loop should
end.

@empror9,
Yo could change cin >> i;//which takes all chars up to the first non digit and converts them to int to i = cin.get()-'0';//which takes one char and converts it to integer . You'd have to handle all non digits then. For example, check if i is between 0 and 9 before checking if it's odd or even. If it is not in this range, ignore it.

@xorebxebx,
I see you find pleasure in writing long c++ apps and comparing them with short scala apps. Well, there programs approach the problem differently, so you can't really compare them. A more equivalent c++ code would use count_if (I'm not implying that it would be any shorter though since c++ doesn't have lambda functions yet).
You're missing the point here though. He is not writing an app to count even digits. He is writing this app because he needs to learn a bunch of things about how loops, arrays and streams work and your code doesn't say much about that.
closed account (EzwRko23)
Who the hell told you they use a different algorithm?
The algorithm is exactly the same. Both use linear scan to count. Both count only even numbers. They are only expressed differently - one is idiomatic C++, the second one is idiomatic Scala. If you feel so bad about my "overblown" C++ implementation, please provide a better one. I doubt it will be any shorter or simpler.
Last edited on
So obviously I'm suggesting a loop when I say "repeat the algorithm...". The only
trick here, which I'll let you think about, is to figure out when the loop should
end.


Hmm.. above is a good chance to ask the student to produce an iteration and a recursive version to understand recursion sometimes can be very "beautiful" and concise :P
@xorebxebx
The equivalent c++ code would be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>

bool is_odd(char x){
    return (x-'0') % 2;
}

int main(){
    std::string s;
    std::getline(std::cin, s);
    int odd = std::count_if(s.begin(), s.end(), is_odd);
    std::cout << "Even : " << s.length()-odd << ", odd : " << odd;
    std::cin.get();
    return 0;
}
And yes, it is not any shorter. As for simplicity, I find a loop + comparison + incrementing much more obvious than a function that somebody else wrote that does most of the job for you + 'x & 1 == 0'. Also, if I were a professor, I wouldn't be glad to see that kind of answer. As I said, it's missing the point.

You know, this isn't really the right example that shows how much scala is better than c++. It is way too trivial for that. This is more about how scala has a bunch of functions in it's standard lib. To prove your point you should probably find a thread dealing with something more complex..
closed account (EzwRko23)
So you have shown you can use functional style in C++ for this trivial example.
I agree it is cleaner (and from my other posts you would also know I'm generally a proponent of the functional paradigm), but it is also a little longer. I tried to write the shortest C++ code, so that you cannot claim I deliberately used longer constructs to make C++ look more verbose.

Anyway, the functional style and C++ simply don't play well together. Functional style is not idiomatic C++, because C++ is not a functional language (having some limited support for functional programming doesn't make it a functional language). But, this example is far too simple to show that.


To prove your point you should probably find a thread dealing with something more complex..


I was not proving anything. I even haven't posted any comment. Just pure code. The readers can judge on their own. Finding a different thread will take some time, because most of the threads in this forum concern C++ language problems (like: why this does not compile or I get segfault or what library to use, etc.), not domain problems.
Last edited on
So you have shown you can use functional style in C++ for this trivial example.
It was not my intention to use functional paradigm, but to translate the scala code you posted. That's irrelevant though.
I agree it is cleaner
I don't. I believe, that is simply because you've spent more time with functional paradigm while I got used to imperative.
Topic archived. No new replies allowed.