simple question??

S

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;
}
 
A

Artie Gold

sj said:
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
 
D

Darklight

sj said:
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;
}
 
J

Jonathan Adams

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;
}
 
S

sj

Darklight said:
sj said:
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
 
S

Stuart Gerchick

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top