Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
How to Initialize array with rondom numbers with single statement using C++ ?
Posted 31-Dec-12 8:15am
Rate this: bad
good
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};
  Permalink  
Comments
progahmed at 1-Jan-13 13:56pm
   
thanks thats exactly the required code Thank you so much
Rate this: bad
good
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.
  Permalink  
Comments
progahmed at 31-Dec-12 14:56pm
   
it is not homework, the interviewer asked me this question today morning for junior c++ developer job
PIEBALDconsult at 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 at 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 at 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! —SA
PIEBALDconsult at 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 at 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. —SA
PIEBALDconsult at 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 at 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: http://msdn.microsoft.com/en-us/library/vstudio/5011f09h.aspx 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. —SA
PIEBALDconsult at 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 at 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... —SA
PIEBALDconsult at 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 at 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: http://www.codeproject.com/Answers/517108/howplustoplusmarkplusmypluslocationplusandplusmypl#answer1 On this optimistic note, again, Happy New Year! —SA
progahmed at 31-Dec-12 16:37pm
   
please write the code for my question
PIEBALDconsult at 31-Dec-12 16:39pm
   
Ummm... what?
Sergey Alexandrovich Kryukov at 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. —SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
progahmed at 31-Dec-12 14:34pm
   
dosen't contain solution to my question thank you
Rate this: bad
good
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.
  Permalink  
Comments
Mattias Högström at 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
good
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 ??
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 940
1 OriginalGriff 543
2 Abhinav S 253
3 thatraja 245
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,282
1 OriginalGriff 4,750
2 Peter Leow 3,784
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid