By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

simple question??

P: n/a
sj
Hi,
I am new to C and learning it. could someone help me to understand the
following code's output? I am having hard time understanding how n2
values like that?. To me all n2 values are equal to 1. What am i
missing here?
thanks in advance.

output:

n1 = 8
n1 = 4
n1 = 2
n2 = 2
n2 = 4
n2 = 8
code:

#include <stdio.h>
#define MAX 8

void msort(int list[], int n)
{
int half1;
int arr1[MAX/2+1];
int count = 0;
if(n > 1)
{
count++;
half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(arr1, half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
int array[] = { 8, 3, 2, 9, 7, 1, 5, 4 };
msort(array, n);
return 0;
}
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sj wrote:
Hi,
I am new to C and learning it. could someone help me to understand the
following code's output? I am having hard time understanding how n2
values like that?. To me all n2 values are equal to 1. What am i
missing here?
Well, for one thing, since you don't enter the printing code when n ==
1, the `n2' value can *never* be 1!

It would be more instructive, however, for you to follow the flow of
execution in the code (remember, you'er making recursive use of the
function `msort'.

output:

n1 = 8
n1 = 4
n1 = 2
n2 = 2
n2 = 4
n2 = 8
code:

#include <stdio.h>
#define MAX 8

void msort(int list[], int n)
{
int half1;
int arr1[MAX/2+1];
int count = 0; printf("msort called with n = %d\n", n); /* this might help */ if(n > 1)
{
count++;
half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");
printf("about to make recursive call of msort() with n = %d\n",

half1); msort(arr1, half1); printf("returned from recursive call of msort() with n = %d\n"
half1); printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
int array[] = { 8, 3, 2, 9, 7, 1, 5, 4 };
msort(array, n);
return 0;
}


That should help you visualize what's going on. IF you still don't see
it, by all means repost.

HTH,
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Nov 14 '05 #2

P: n/a
sj wrote:
Hi,
I am new to C and learning it. could someone help me to understand the
following code's output? I am having hard time understanding how n2
values like that?. To me all n2 values are equal to 1. What am i
missing here?
thanks in advance.

output:

n1 = 8
n1 = 4
n1 = 2
n2 = 2
n2 = 4
n2 = 8
code:

#include <stdio.h>
#define MAX 8

void msort(int list[], int n)
{
int half1;
int arr1[MAX/2+1];
int count = 0;
if(n > 1)
{
count++;
half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(arr1, half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
int array[] = { 8, 3, 2, 9, 7, 1, 5, 4 };
msort(array, n);
return 0;
}


I am a novice but hay there is no out put for arr1
with int array[], int list[], int arr1[] deleted
you will get the same answer altered code below:
then change printf("n2 = %d ", n); to printf("n2 = %d ", half1);
and see what you get
#include<stdio.h>
#define MAX 8

void msort(int n)
{
int half1;
int count = 0;
if(n > 1)
{
count++; half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
msort( n);
return 0;
}

Nov 14 '05 #3

P: n/a
In article <1c**************************@posting.google.com >,
kp******@yahoo.com (sj) wrote:
Hi,
I am new to C and learning it. could someone help me to understand the
following code's output? I am having hard time understanding how n2
values like that?. To me all n2 values are equal to 1. What am i
missing here?
thanks in advance.

output:

n1 = 8
n1 = 4
n1 = 2
n2 = 2
n2 = 4
n2 = 8
Each invocation of msort() has it's own independent version of the 'n'
variable. So you have a call graph like:

main
+---->
msort (n == 8)
+---->
msort (n == 4)
+---->
msort(n == 2)
+---->
msort(n == 1)
<---+
<---+
<---+
<---+

Each function also has it's own copy of "count", so for each msort()
but the innermost one, count will always end up with the value "1".

Cheers,
- jonathan

code:

#include <stdio.h>
#define MAX 8

void msort(int list[], int n)
{
int half1;
int arr1[MAX/2+1];
int count = 0;
if(n > 1)
{
count++;
half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(arr1, half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
int array[] = { 8, 3, 2, 9, 7, 1, 5, 4 };
msort(array, n);
return 0;
}

Nov 14 '05 #4

P: n/a
sj
Darklight <ng******@netscape.net> wrote in message news:<cl**********@titan.btinternet.com>...
sj wrote:
Hi,
I am new to C and learning it. could someone help me to understand the
following code's output? I am having hard time understanding how n2
values like that?. To me all n2 values are equal to 1. What am i
missing here?
thanks in advance.

output:

n1 = 8
n1 = 4
n1 = 2
n2 = 2
n2 = 4
n2 = 8
code:

#include <stdio.h>
#define MAX 8

void msort(int list[], int n)
{
int half1;
int arr1[MAX/2+1];
int count = 0;
if(n > 1)
{
count++;
half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(arr1, half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
int array[] = { 8, 3, 2, 9, 7, 1, 5, 4 };
msort(array, n);
return 0;
}


I am a novice but hay there is no out put for arr1
with int array[], int list[], int arr1[] deleted
you will get the same answer altered code below:
then change printf("n2 = %d ", n); to printf("n2 = %d ", half1);
and see what you get
#include<stdio.h>
#define MAX 8

void msort(int n)
{
int half1;
int count = 0;
if(n > 1)
{
count++; half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
msort( n);
return 0;
}


thanks. i understand that part and could have deleted array things.
that was not a problem of arrays. How does n2 go like 2 4 8
Nov 14 '05 #5

P: n/a
kp******@yahoo.com (sj) wrote in message news:<1c**************************@posting.google. com>...
Hi,
I am new to C and learning it. could someone help me to understand the
following code's output? I am having hard time understanding how n2
values like that?. To me all n2 values are equal to 1. What am i
missing here?
thanks in advance.

output:

n1 = 8
n1 = 4
n1 = 2
n2 = 2
n2 = 4
n2 = 8
code:

#include <stdio.h>
#define MAX 8

void msort(int list[], int n)
{
int half1;
int arr1[MAX/2+1];
int count = 0;
if(n > 1)
{
count++;
half1 = n / 2;
printf("n1 = %d ", n);
printf(" \n");

msort(arr1, half1);
printf("n2 = %d ", n);
printf(" \n");

}
}

int main()
{
int n = 8;
int array[] = { 8, 3, 2, 9, 7, 1, 5, 4 };
msort(array, n);
return 0;
}


This is what is known a recursion. Each isntance of msort is
independent and has its own variables. A function record and anly
local variables (the function activation record) are pushed onto the
stack. but each time msort is called, there is other instances of
these variables.

In languages like fortran things were static and you could not do
this. In C you are allowed recursion
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.