Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Trying to understand...

DeeleDeele Posts: 7Member
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...
«1

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    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?
    : [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
  • DeeleDeele Posts: 7Member
    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?
  • bilderbikkelbilderbikkel Posts: 754Member
    : 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]'


    bilderbikkel
  • LundinLundin Posts: 3,711Member
    : 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

    and then at the end

    system("pause");
    return 0;


    Or as an alternative:

    printf("Press any key to exit");
    getchar();
    return 0;[/color]
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : 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 ("

    Press any key to exit...");
    getch ();[/code][/color]
  • LundinLundin Posts: 3,711Member
    : [color=Green][code]: printf ("

    Press any key to exit...");
    : getch ();[/code]: [/color]


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

    [b]getchar();[/b]

    ;-)
  • DeeleDeele Posts: 7Member
    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...
  • cactus1cactus1 Posts: 41Member
    : 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.
  • LundinLundin Posts: 3,711Member
    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.
  • DeeleDeele Posts: 7Member
    11) If understood right, now, If I want to create "your set" in one variable, I should do this:
    [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]
«1
Sign In or Register to comment.