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;
} 5 1371
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."
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;
}
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; }
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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Patchwork |
last post by:
Hi Everyone,
Please take a look at the following (simple and fun) program:
////////////////////////////////////////////////////////////////////////////
/////////////
// Monster Munch, example...
|
by: Proteus |
last post by:
Any help appreciated on a small perl project I need to write for
educator/teaching purposes. I have not programmed perl for some time, need
to get up to speed, maybe some kind souls hrere will help...
|
by: Raskolnikow |
last post by:
Hi!
I have a very simple problem with itoa() or the localtime(...).
Sorry, if it is too simple, I don't have a proper example.
Please have a look at the comments.
struct tm *systime;
time_t...
|
by: Peter |
last post by:
Hello
Thanks for reviewing my question. I would like to know how can I programmatically select a node
Thanks in Advanc
Peter
|
by: abcd |
last post by:
I am trying to set up client machine and investigatging which .net
components are missing to run aspx page.
I have a simple aspx page which just has "hello world" printed....
When I request...
|
by: dba_222 |
last post by:
Dear Experts,
Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.
In Sql server, how do I simply change a...
|
by: Giancarlo Berenz |
last post by:
Hi:
Recently i write this code:
class Simple
{
private:
int value;
public:
int GiveMeARandom(void);
|
by: galiorenye |
last post by:
Hi,
Given this code:
A** ppA = new A*;
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA;
//do something with pA
|
by: Phillip Taylor |
last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm
having some trivial issues. In Visual Studio I create a web services
project and change the asmx.vb file to this:
Imports...
|
by: Chris M. Thomasson |
last post by:
I use the following technique in all of my C++ projects; here is the example
code with error checking omitted for brevity:
_________________________________________________________________
/*...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |