A program that generate 10 numbers

Write a program that generates the numbers from 1 to 10 then prints the sum of the even numbers and the sum of odd numbers using “ do while loop”
Tell us what part of the program you are having problems with so we can help you on getting the solution; we are not giving away solutions "as is" to encourage people to learn (and not to become lazy).
Last edited on
hey dear friend,
no body will help you in this because if we do, you will become weak....
this is a very simple question... just refer to some books or just try google... dont put your home work as a whole in here... we will help you in any dificulty you face, but its not good for u that we do ur homework..
please understand muham2k..
cheers..
check this link..
http://www.cplusplus.com/doc/tutorial/
Hi I give you some example but to be challenge for you try to convert this to do-while loop and use this as a hint for your project:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
main() {
    for(int x=1;x<=10;x++) {
        if((x%2) == 0) {
            cout << "this number is an even number: " << x;
        } else {
            cout << "this number is an odd number: " << x;
        }
        cout << "\n";
    }
}


http://codewall.blogspot.com

Last edited on
The only challenge in this assignment was showing initiative by wring the code yourself. There's nothing wrong with postnig codes as long as they abide a certain unwritten, yet common, rule:
The code should only show a concept.
The final code should not be an edit of the given code.

The hint you could have given in this case is:
For integer values (x % 2) == 0 if it is even.

Also, your code is non-standard, and will generate a compiler warning. Prefix main by int like so: int main().
Whatever, since we're posting working codes, here is my attempt using Wrap Around Number Oriented Programming. ( Header file can be found here: http://www.cplusplus.com/forum/lounge/33512/#msg181953 )
1
2
3
4
5
6
7
8
9
10
11
12
#include "WANOP.h"

PROGRAM("528");
unsigned i=11;
unsigned s_e=0;
unsigned s_o=0;
void foo(){if(--i<=0){go=0;std::cout<<"Even sum: "<<s_e<<"\nOdd sum: "<<s_o<<"\n\n";}}
void bar(){if(i%2)s_o+=i;}
void baz(){if(i%2==0)s_e+=i;}
START_ADD_FUNCTIONS
ADD(foo);ADD(baz);ADD(bar);
END_ADD_FUNCTIONS
Last edited on
@kyon some compiler or ide you are using for me I use code blocks will not give you warning if you dont put int in main. Main is a special function, you can put int use void or even without it. You can even not use the return statement.

There are no non-standard code! It up to programmer how he program it as long as its correct, useful, safe and secure and maintainable and approve by user.

http://codewall.blogspot.com
Last edited on
closed account (1yR4jE8b)
@kyon some compiler or ide you are using for me I use code blocks will not give you warning if you dont put int in main. Main is a special function, you can put int use void or even without it. You can even not use the return statement.

There are no non-standard code! It up to programmer how he program it as long as its correct, useful, safe and secure and maintainable and approve by user.


wrong, wrong, and wrong

Anything other than int main() is not standard C++, just because some compilers support other forms of it does not mean that it is correct and is not guaranteed to be portable between different compilers (or even different versions of the same compiler).

And yes there IS 'non-standard' code. If you write code that goes against the C++ standard, but compiles anyway, then it is NON-STANDARD. Just because it compiles doesn't mean it is correct, take variable-sized arrays for example:

1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
#include <ctime>

int main()
{
  srand((unsigned)time(0));
  int x = rand() % 1000;
  int array[x];
  
  return 0;
}


This code compiles using Cygwin g++ 4.3.4, but it goes against the C++ standard that states that non-dynamic array sizes must be a compile-time constant.

In this example, and your very own non-standard main example, if you disable language extensions with the
g++ -pedantic
option, the compiler will generate proper errors for both.

I suggest you stop spewing such nonsense, and shut down your blog until you actually know what you are talking about.
@ darkestfright thanks for your comment but remember that each programmer have it's own perception in regarding creating program. I know that you follow what is written but my perception in programming is, there's nothing important than an actual experience. My blog is the result of my experiences. And also remember Actual is not actually the same as what you read in the books. And what is always important is your users.

http://codewall.blogspot.com
Last edited on
closed account (1yR4jE8b)
lol, what does that even mean?
Programming its a very huge knowledge to study and its expand. When we say it standard, yes its standard but sometimes this standard not even work!. and some programmer may create their own standard.

http://codewall.blogspot.com
Last edited on
closed account (1yR4jE8b)
I take it English isn't your first language...

Anyway, there are so many things wrong with the things you say that I'm not going to bother...I'm just going to get back to working on my darkestfright edition of C++.

Good night.
my friends, you are reminding me of the great music of Beethoven, "Rage over a lost penny".. hehe...
cmon guys.. lets not quarrel over some code..
according to my knowledge, what i know ( and what i am still learning ) is that you cant be a good programmer without knowledge of algorithms,good experience, knowledge about operating system, low level(assembly and machine language) language knowledges and focus on the scope( the purpose) of the problem(like whether it must be efficient or it must be neat and clean)...

for example,
you can implement a function that return the factorial of a given number using Recursion( the original definition of factorial implemented as it is) or with a loop(iteration)..
1st method is neat and 2nd is efficient..

u will decide if you are focusing on neat code or efficient code according to the problem which u are solving..

anyway, standards can be broken wisely, but if you do it foolishly , u can end up in a lot of problems...
one thing for sure, if standard could be decided by a user like me
Then the "standard" would become a nightmare
because your standard may not same as my standard, my standard may not same as your standard
how could we communicate with each other's if there were without an unique standard?

try to imagine one thing, if there a lot of "MPEG2"
MPEG2.0, MPEG2.0_USERA, MPEG2.0_USERC.......MPEG2.0_USERCCC
wow, how many decoders do we need?
even VLSI can't handle it
Last edited on
exactly...

also thats a very good example..

ps.
i have heard this word "best practices" which also mean a little simillar..... there are best practices for most of the things in programming... sticking with them may make things easy... but if u wanna go advanced ( maybe for security or any other reason ) you will create some crazy thing that is greater or more your purpose sepecific..
but i cant find any good example for this rite now... the example i have in mind may confuse the reader... :(
i will try to find some good examples..
thecodewall wrote:
@kyon some compiler or ide you are using for me I use code blocks will not give you warning if you dont put int in main. Main is a special function, you can put int use void or even without it. You can even not use the return statement.

There are no non-standard code! It up to programmer how he program it as long as its correct, useful, safe and secure and maintainable and approve by user.

http://thecodewall.blogspot.com


You are right. Compilers and IDE's won't support it by default, install the library I gave a link to and it will work.

EDIT:
As a side note, the code I posted was mainly - like all posts after the OP doesn't react - to troll in a light-sense.
Light, because with the library added, the program actually does what he wants.
Trolling because it's using simulated recursive function calls by following a functor-vector depending on a wrap around number. ... instead of a do-while loop.

RE-EDIT:
Another note, you mixed up terms, obviously:
There are no non-standard code! It up to programmer how he program it as long as its correct,

The standard defines what is correct. You are talking about good practices (as chathura already pointed out).

RE-RE-EDIT:
This post is getting really long. But I had to re-edit it AGAIN to show you another flaw in your logic.
Main is a special function, you can put int use void or even without it. You can even not use the return statement.

Int is standard; all compilers support it. Void is non-standard, only some compilers support it, but really shouldn't. Return is never needed.
What I'm trying to point out is that you SHOULD return an int, the OS you are running simply wants return-codes. If you don't tell the OS that your program has struck upon an error or if it ran successfully, things can get messy.
Last edited on
Topic archived. No new replies allowed.