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]
Okay, (not a)full code, if you ask, is here:
[code]int main(void) /*demo*/
{
int set[32],i;
int k=0;
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]'.
: 3) What will I get, when I will say "set+k" if:
: [code]: int k=0;[/code]:
The adress 'k' spaces beyond 'set', i.e. '&set[0]+k' (is equal to '&set[k]').
: 5) What is doing here:
: [code]: for (i=0;i<k-1;i++) printf("%d,",set[i]);[/code]:
Prints the array 'set'
See ya,
bilderbikkel
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?
: 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]'
bilderbikkel
: 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
and then at the end
system("pause");
return 0;
Or as an alternative:
printf("Press any key to exit");
getchar();
return 0;[/color]
: 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 ("
Press any key to exit...");
getch ();[/code][/color]
Press any key to exit...");
: getch ();[/code]: [/color]
Noo... don't teach non-standard functions!
[b]getchar();[/b]
;-)
[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...
: [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.
[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.
[code]
int set[32],i;
int k=0;
m_Set = "{";
for (i=0; i<k-1; i++)
{
m_Set = m_Set + "," + set[i];
}
m_Set = m_Set + "}";
[/code]
and it will be the same as printed to window:
[code]
printf("your set:
{");
for (i=0;i<k-1;i++) printf("%d,",set[i]);
if (k) printf("%d",set[k-1]);
printf("}
");
[/code]
: variable, I should do this:
Well... almost. I assume m_Set is defined as CString?
The problem with your code currently is:
1) The first item will look like: "{,1, ...}", and
2) The last item won't be printed.
[code]
m_Set = "{";
for (i=0; i 0) m_Set = m_Set + set[k - 1];[/color]
m_Set = m_Set + "}";
[/code]
Best Regards,
Richard
The way I see it... Well, it's all pretty blurry
: : variable, I should do this:
:
: Well... almost. I assume m_Set is defined as CString?
: The problem with your code currently is:
: 1) The first item will look like: "{,1, ...}", and
: 2) The last item won't be printed.
:
: [code]:
: m_Set = "{";
: for (i=0; i 0) m_Set = m_Set + set[k - 1];[/color]
: m_Set = m_Set + "}";
: [/code]:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry
[hr]
[color=Blue]I got this error (error C2666: '+' : 4 overloads have similar conversions) for line:
[code]m_Set = m_Set + set[i] + ",";[/code]
and the same error on line:
[code]if (k > 0) m_Set = m_Set + set[k - 1];[/code]
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
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]
: similar conversions) for line:
: [code]: m_Set = m_Set + set[i] + ",";[/code]:
: and the same error on line:
: [code]: if (k > 0) m_Set = m_Set + set[k - 1];[/code]:
:
The problem is that the CString class can not convert integer into string.
You'll need to use something like _itoa (integer to ansi) (defined in stdlib), or an equivalent function.
:
: 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?
In the current code the k was used for determining how many elements of set were in use. You will need something equivalent in your program.
Best Regards,
Richard
The way I see it... Well, it's all pretty blurry
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".