Trying to understand...

I am analyzing a C++ console based program but I am very newbie to C++ and I want to learn it. Main reason, why I am analyzing this code, becouse I need to remake it by myself but in form of MFC dialog.Okay, I have couple of questions and I hope, someone will help me...1) Does this one makes an array? If so, then, all the array values from 1..32 will be empty, if I call set[1] or any other?[code]int set[32][/code]2) What will I get, when I will call just: "set", not "set[n]" ?[code][/code]3) What will I get, when I will say "set+k" if:[code]int k=0;[/code]4) What is doing this loop? I know that "EOF" is EndOfFile that is CTRL+Z, but what this loop does?[code]while (k<32 && scanf("%d",set+k)!=EOF) k++;[/code]5) What is doing here:[code]for (i=0;i<k-1;i++) printf("%d,",set[i]);[/code]6) What this will "set[k-1]" print out?[code]if (k) printf("%d",set[k-1]);[/code]

printf("enter the set elements separated by blanks and ctrl-z at the " "end");

while (k<32 && scanf("%d",set+k)!=EOF) k++; printf("your set:{"); for (i=0;i<k-1;i++) printf("%d,",set[i]); if (k) printf("%d",set[k-1]); printf("}"); printf("its subsets:"); subsets(set,k); return 0;}[/code]I will have a couple of more questions later on...

Comments

: 1) Does this one makes an array? If so, then, all the array values : from 1..32 will be empty, if I call set[1] or any other?: [code]: int set[32][/code]: Yes, that declares an array with 32 uninitialized values, i.e. these values can be anything.

: 2) What will I get, when I will call just "set", not "set[n]"?You get the address of the array. 'set' is also equal to '&set[0]'. '*set' is equal to 'set[0]'.

Cool, I understod almoast everything... Just few more questions in this step:7) What is meaning of "%d" and "set+k" at "scanf" function? How can I use this in future?[code]scanf("%d",set+k)[/code]8) What is meaning of "%d" and "set[i]" at "printf" funtion? How can I use this in future?[code]printf("%d,",set[i])printf("%d",set[k-1])[/code]9) How to make ending of program just like: "Enter any key to exit..." and only after pushing any key, console window closes?

: 8) What is meaning of "%d" and "set[i]" at "printf" funtion? How can : I use this in future?: [code]: printf("%d,",set[i]): printf("%d",set[k-1])[/code]: '%d' is the placeholder for a value. '%d' denotes that this value is a signed decimal. This value is 'set[i]'

: Cool, I understod almoast everything... Just few more questions in : this step:: 7) What is meaning of "%d" and "set+k" at "scanf" function? How can : I use this in future?: [code]: scanf("%d",set+k)[/code]

[color=Blue]If you write only the array name, in this case "set", it will equal a pointer to the first element in the array. set+k is pointer arithmetics, it means "take the addres of the first element + k elements".This is the very same as writing &set[k].

In this case, they wanted to assign the value from scanf() to element set[k].[/color][code][/code]

: 8) What is meaning of "%d" and "set[i]" at "printf" funtion? How can : I use this in future?: [code]: printf("%d,",set[i]): printf("%d",set[k-1])[/code]

[color=Blue]It means "print element i of the array set". Heh, and it probably also means that you won't understand my reply to 7), to understand pointer arithmetics, you first need to understand arrays.[/color]

: 9) How to make ending of program just like: "Enter any key to : exit..." and only after pushing any key, console window closes?: [color=Blue]#include

: Cool, I understod almoast everything... Just few more questions in : this step:: 7) What is meaning of "%d" and "set+k" at "scanf" function? How can : I use this in future?: [code]: scanf("%d",set+k)[/code]:

[color=Blue]%d in both cases means the formatting specifier. It tells that scanned value from user input (console) will be translated using decimal format, like "1234". set+k mean the address of 'k' element of array 'set'. Note: the address - not the value.[/color][code] [/code]: 8) What is meaning of "%d" and "set[i]" at "printf" funtion? How can : I use this in future?: [code]: printf("%d,",set[i]): printf("%d",set[k-1])[/code]:

[color=Blue]See above for %d - it again tells that value from set[i] must be printed using decimal formatting. set[i] is a value of element indexed by 'i' in array 'set'. Indexes are starting from 0, so the first element of the array is set[0], second - set[1] and so on...[/color][code] [/code]: 9) How to make ending of program just like: "Enter any key to : exit..." and only after pushing any key, console window closes?: [color=Green][code]printf ("

10) What is doing this loop?[code]while (k<32 && scanf("%d",set+k)!=EOF) k++;[/code]It is reading an input 32 times searching for EOF? Or what? Every time input is scanned, it is added to set with each loop, increasing "k" by 1? Or what?

: 10) What is doing this loop?: [code]: while (k<32 && scanf("%d",set+k)!=EOF) k++;[/code]: : It is reading an input 32 times searching for EOF? Or what? Every : time input is scanned, it is added to set with each loop, increasing : "k" by 1? Or what?: : PS: Continuing work later...: It puts a value into set[k], then increments k to get to the next value when the while loop is run again. The !=EOF is to end the while statement midway if the file ends midway.

It is also a very typical example of why you should use for() loops whenever possible, since the code is hard to read. Here is an improved version that ensures that k has been intialized and ensures integrity of the data stored in "set":

[code]for(k=0; k<32; k++){ int input;

if(scanf("%d", &input) == EOF) break; else set[k] = input;}[/code]

Note that this example is equally efficient as the original while-loop, both in terms of speed and memory used.

12) What is a main meaning of that "k" (look for program code in earlier posts)? Is it equal to number of set elements? Ok, at first, there is declaration that "[color=Orange]k = 0[/color]". Okay. Second, there is loop "[color=Orange]while (k<32 && scanf("%d",set+k)!=EOF) k++;[/color]". If I analyze this loop:1> If user is wise, he will enter set correctly that is, for example, set of three chars, seperated with blanks "A B C".2> Loop is asking if "k" is less than "32". Yes, it is "0". continuing.3> Loop is setting "set+k" equal to user input. That means, loop is setting "set+k = set[0];" and... "set[0] = A B C;"4> Loop is asking if that "set[0]" is not equal to "EOF" char. Yes, it is, it is just "A B C". SO, loop continues and adds one to "k" so now "k = 1"5> Again, we think that user is not a noob and will add correctly "EOF" sign "CTRL+Z" at next line and press enter...6> Loop is asking if "k" is less than "32". Yes, it is "1". continuing.7> Loop is setting "set+k" equal to user input. That means, loop is setting "set+k = set[1];" and... "set[1] = EOF;"8> Loop is asking if that "set[1]" is not equal to "EOF" char. No, it isn't, it is "EOF" char. SO, loop stops and continues to next program line...

What this K is for? I think, if I remake this program in dialog way, this "k" will be unnessesary, cause, it will be all the time "k = 0". Am I right?[/color]

13) How to convert "int" to "CString"? Is there any other conversition commands?For example: I have CString called "word" with value "something special" and int called "number" with value "12345". I want to combine "word" with "number" together to get "something special 12345".