Click here to Skip to main content
13,597,396 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to Initialize array with rondom numbers with single statement using C++ ?
Posted 31-Dec-12 8:15am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

What's a statement? If you have a for statement that contains an assignment statement, is that one statement or two?

I should think that if you don't initialize the array you'd have fairly random numbers. :shrug:

Sounds like homework or a puzzle or something.
progahmed 31-Dec-12 14:56pm
it is not homework,
the interviewer asked me this question today morning for junior c++ developer job
PIEBALDconsult 31-Dec-12 15:12pm
Ah, that sounds like a typical question from someone who doesn't know programming.
My main responses still apply.
I don't see it being done with standard language, but maybe a common add-in library has a class that will do it.

I do C# (not C++) so I can see a library having a class that allows you to instantiate an array and provide a delegate that provides the values.
progahmed 31-Dec-12 14:58pm
he asked me to write one single statement to initialize array with randome number from 0-5 using rand() function
Sergey Alexandrovich Kryukov 31-Dec-12 15:12pm
Best answer so far, my 5. And yes "for" loop with block is one statement :-)
"Don't initialize array" is a pretty sharp idea, but in many cases the values will be 0 or all 0xFs.., not quite random.
Happy New Year!
PIEBALDconsult 31-Dec-12 15:14pm
" "for" loop with block is one statement "

Not from my point of view. And what if that one statement is a compound statement?
Sergey Alexandrovich Kryukov 31-Dec-12 15:17pm
A "statement" is not a unit of code or something like that. This is a term used in formal description of a language. Yes, one single statement can be composed of several other statements, and so on. Look at some language standards and you will see it.
PIEBALDconsult 31-Dec-12 15:34pm
I've read languages specs. I disagree with your interpretation, because that can reduce an entire program to one statement. Which probably not what the asker had in mind anyway.

As an ad nauseum example: if ( 1==1 ) { millions of "lines of code" } is one statement.

I am of the opinion that any unit of code that the compiler (lexer,parser,whatever) recognizes as a "statement" is a statement and needs to be counted as such, so it counts as two statements. You are welcome to disagree.
Sergey Alexandrovich Kryukov 31-Dec-12 15:49pm
This is exactly what happens: a whole program is one statement; but it contains many statements. Nothing wrong about it. Anyway, this is all formal thing; there can be any number of equivalent descriptions of same language.

To see that what you call "my" interpretation is actually used (and even typical or dominating notation), its enough to see one example:

Pay attention for the comment "// if-else statement" (second code sample). It explicitly calls the whole construct a "statement", and explicitly shows that it has statements inside it. If you look at BNF notation, you will see the same. This is how languages are normally described these days.

PIEBALDconsult 31-Dec-12 16:10pm
I don't think you'll get anyone to say the a then-statement is _not_ a statement. A then_statement _must_ be a statement or you get an error.

"The embedded-statement non-terminal is used for statements that appear within other statements"

"A statement list consists of one or more statements written in sequence."

"explicitly shows that it has statements inside it" Exactly my point -- an if_statement _is_ a statement and its then_statement is _also_ a statement; ergo _two_ (or more) statements.
Sergey Alexandrovich Kryukov 31-Dec-12 16:20pm
So, we come back to the same thing. "If" (or if-then-else is one statement, but the total number of statements is not one). Call it a paradox, but this is a trivial linguistic, not a logical paradox. As OP's "single statement" "requirement" does not strictly define what it "single", you can use any convenient meaning and had ever write to say that the loop is a correct solution. Note that strict BNF definitions do not operate the terms like "single" because it is not needed. BNF is quite formal and uses "let the one having the eyes seeing" principle. It's quite clear what construct is valid or not, without all that "singe or not" casuistic. Honestly, it's getting boring...
PIEBALDconsult 31-Dec-12 16:34pm
I don't call it a paradox. Trivial perhaps, but programming specs should be clear.

Most likely the for-with-one-assignment is the desired response, and a for-with-two-assignments would be frowned upon, yet both could be viewed as "one for statement".

So what does the interviewer mean? Chances are he's just an HR hack or non-programming manager who doesn't realize that he asked an ambiguous question and expects the poster to know what the hell he's talking about. A very bad situation in IT, but business as usual.

Maybe things will be better next year.
Sergey Alexandrovich Kryukov 31-Dec-12 17:31pm
Most interviewers I ever saw were morons, and so were their companies or teams. Ambiguous questions, as well as incorrect ones, are a commonplace. If this is an interview question (I would not be so sure, bu quite possible) — I agree; this is just one of them, really of poor value.

No wonder those companies produce some much of complete trash. On can clearly see it right no on the industrial automation market. Paradoxically, this is result of relatively high demand for solving of programming problems and writing of software. To hire a competent engineers, one needs to be a competent engineers, so clusterization occurs. In my observations, the situation is gradually shifted to the better one, but this is somewhat masked by the current recession in U.S. and other countries. This improvement is just not that apparent, and the effect of "domination of morons" still massively takes place.

I think, nothing bad will happen to the real part of the industry. There are too many real difficult and really important problems, and qualified people will be working at them. Please see my past answer:

On this optimistic note, again, Happy New Year!
progahmed 31-Dec-12 16:37pm
please write the code for my question
PIEBALDconsult 31-Dec-12 16:39pm
Ummm... what?
Sergey Alexandrovich Kryukov 31-Dec-12 17:33pm
How about you? You should be able to do it, too, as you are the one who is more interested. If you face any problem, we will help you.
It's not just because we are lazy; we just don't want to waste time for people who might not be able to really get help.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

How about this:

int values[5] = { rand() % 5, rand() % 5, rand() % 5, rand() % 5, rand() % 5};
progahmed 1-Jan-13 13:56pm
thanks thats exactly the required code
Thank you so much
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

progahmed 31-Dec-12 14:34pm
dosen't contain solution to my question
thank you
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

This page[^] shows an example of using generate to fill a vector with random numbers - replace the vector with an array and you might have the answer you were looking for.
Mattias Högström 1-Jan-13 12:38pm
True. Something like this

int myArray[10];
generate(myArray, &myArray[sizeof(myArray)/sizeof(myArray[0])], rand);

For readability, I prefer using normal for-loops.
Easier to debug too.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

You're being fooled by a stupid question.

Who cares about the number of statements in your source code. Does a less number of source code makes your program faster, more stable ?? Well, not at all. It may sometimes cause you problems, or can reduce the performance of your program.

Every program is built on top of another one. For example, .NET is built on top of the Windows API, the Window API is built on top of the Windows Kernel and the Windows Kernel is built on top of a CPU Architecture. They all are related.

Using Visual Basic.NET you will be able to create a windows program without just a single statement. But if you use the Windows API, it will take at least 100 lines to create a program with a window. So which one's better, faster and more stable ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.180621.3 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100