Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Trying to understand... Posted by Deele on 31 May 2007 at 2:55 AM
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?
int set[32]

2) What will I get, when I will call just: "set", not "set[n]" ?

3) What will I get, when I will say "set+k" if:
int k=0;

4) What is doing this loop? I know that "EOF" is EndOfFile that is CTRL+Z, but what this loop does?
while (k<32 && scanf("%d",set+k)!=EOF) k++;

5) What is doing here:
for (i=0;i<k-1;i++) printf("%d,",set[i]);

6) What this will "set[k-1]" print out?
if (k) printf("%d",set[k-1]);




Okay, (not a)full code, if you ask, is here:
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\n");

	while (k<32 && scanf("%d",set+k)!=EOF) k++;
	printf("your set:\n{");
	for (i=0;i<k-1;i++) printf("%d,",set[i]);
	if (k) printf("%d",set[k-1]);
	printf("}\n");
	printf("its subsets:\n");
	subsets(set,k);
	return 0;
}

I will have a couple of more questions later on...
Report
Re: Trying to understand... Posted by bilderbikkel on 31 May 2007 at 3:00 AM
Some quick answers that time permits 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?
:
: int set[32]
:
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:
:
: int k=0;
:
The adress 'k' spaces beyond 'set', i.e. '&set[0]+k' (is equal to '&set[k]').

: 5) What is doing here:
:
: for (i=0;i<k-1;i++) printf("%d,",set[i]);
:
Prints the array 'set'

See ya,
bilderbikkel
Report
Re: Trying to understand... Posted by Deele on 31 May 2007 at 3:29 AM
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?
scanf("%d",set+k)

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

9) How to make ending of program just like: "Enter any key to exit..." and only after pushing any key, console window closes?
Report
Re: Trying to understand... Posted by bilderbikkel on 31 May 2007 at 3:40 AM
: 8) What is meaning of "%d" and "set[i]" at "printf" funtion? How can
: I use this in future?
:
: printf("%d,",set[i])
: printf("%d",set[k-1])
:
'%d' is the placeholder for a value. '%d' denotes that this value is a signed decimal. This value is 'set[i]'


bilderbikkel
Report
Re: Trying to understand... Posted by Lundin on 31 May 2007 at 3:45 AM
: 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?
:
: scanf("%d",set+k)


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].



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


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.


: 9) How to make ending of program just like: "Enter any key to
: exit..." and only after pushing any key, console window closes?
:

#include <stdlib.h>

and then at the end

system("pause");
return 0;


Or as an alternative:

printf("Press any key to exit");
getchar();
return 0;
Report
Re: Trying to understand... Posted by AsmGuru62 on 31 May 2007 at 3:46 AM
: 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?
:
: scanf("%d",set+k)
:

%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.
 

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

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...
 

: 9) How to make ending of program just like: "Enter any key to
: exit..." and only after pushing any key, console window closes?
:
printf ("\n\nPress any key to exit...");
getch ();
Report
Re: Trying to understand... Posted by Lundin on 31 May 2007 at 3:58 AM
:
: printf ("\n\nPress any key to exit...");
: getch ();
:



Noo... don't teach non-standard functions!

getchar();

Report
Re: Trying to understand... Posted by Deele on 31 May 2007 at 4:06 AM
10) What is doing this loop?
while (k<32 && scanf("%d",set+k)!=EOF) k++;

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...
Report
Re: Trying to understand... Posted by cactus1 on 31 May 2007 at 5:57 AM
: 10) What is doing this loop?
:
: while (k<32 && scanf("%d",set+k)!=EOF) k++;
:
: 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.
Report
Re: Trying to understand... Posted by Lundin on 31 May 2007 at 6:06 AM
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":

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

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


Note that this example is equally efficient as the original while-loop, both in terms of speed and memory used.
Report
Re: Trying to understand... Posted by Deele on 31 May 2007 at 11:24 AM
11) If understood right, now, If I want to create "your set" in one variable, I should do this:
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 + "}";

and it will be the same as printed to window:
printf("your set:\n{");
for (i=0;i<k-1;i++) printf("%d,",set[i]);
if (k) printf("%d",set[k-1]);
printf("}\n");

Report
Re: Trying to understand... Posted by Deele on 1 Jun 2007 at 5:00 PM
Where does everybody gone? Noone cannot help me? 0_o
Report
Re: Trying to understand... Posted by BitByBit_Thor on 2 Jun 2007 at 3:33 AM
: 11) If understood right, now, If I want to create "your set" in one
: 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.

m_Set = "{";
for (i=0; i<k-1; i++)
{
	m_Set = m_Set + set[i] + ",";
}
if (k > 0) m_Set = m_Set + set[k - 1];
m_Set = m_Set + "}";

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Trying to understand... Posted by Deele on 2 Jun 2007 at 7:30 AM
: : 11) If understood right, now, If I want to create "your set" in one
: : 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.
:
:
: 
: m_Set = "{";
: for (i=0; i<k-1; i++)
: {
: 	m_Set = m_Set + set[i] + ",";
: }
: if (k > 0) m_Set = m_Set + set[k - 1];
: m_Set = m_Set + "}";
: 
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry



I got this error (error C2666: '+' : 4 overloads have similar conversions) for line:
m_Set = m_Set + set[i] + ",";

and the same error on line:
if (k > 0) m_Set = m_Set + set[k - 1];


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 "k = 0". Okay. Second, there is loop "while (k<32 && scanf("%d",set+k)!=EOF) k++;". 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?


Report
Re: Trying to understand... Posted by BitByBit_Thor on 2 Jun 2007 at 9:41 AM
: I got this error (error C2666: '+' : 4 overloads have
: similar conversions) for line:
:
: m_Set = m_Set + set[i] + ",";
:
: and the same error on line:
:
: if (k > 0) m_Set = m_Set + set[k - 1];
:
:

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
Report
Re: Trying to understand... Posted by Deele on 2 Jun 2007 at 11:51 AM
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".



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.