[Help] C Programming

Status
Not open for further replies.

hafees

In the zone
Hi Klux :))

which book u r using?? Let us C?

There is no nested functions in C as u 've mentioned.

u may be mentioning something like this.

main()
{
a();
}


a()
{
b();
}

b
{
c();
}

c()
{

}

are u mentioning this type of function call?????
 

hafees

In the zone
if u ve that program - the one with nested functions - pls post it here.

Let Us C is a good book (u may skip some advanced topics which is discussed on that book)

then Pointers in C
then Projects in C
then TSR programming thru C
 

aadipa

Padawan
lamrinnirmal said:
dude KuKlux pm me your email .... i ll host a few ebooks on C on a hosting server where you can download em off........or just search the forum....there are quite a lot of links to ebook sites(all legal!)

about nested functions............hope the below code clears your confusion.

imagine of a traveller moving to the different countries from main and when hes in australia() he doesnt go to anyother place(cause australia() doesnt call any function) and thus to find his way back to main he climbs up the way he came down.........

Code:
void india();
void england();
void usa();
void australia();

void main()
{
printf(" i am in main");
india();
printf("jus got back from india!.......finally here...what a ride");
}

void india()
{
printf("im in india! ");
england();
printf("jus got back from england.....!");
return;
}

void england()
{
printf("im in england.......the queen aint hot!");
usa();
printf("jus got back from usa");
return;
}

void usa()
{
printf("im in usa......so whats so great bout this place?pathetic!");
australia();
printf("got back from australia");
return;
}

void australia()
{printf("lost in the outbacks!.....gotta find myself way back");
return;
}


your output would be.....

i am in main
im in india!
im in england.......the queen aint hot!
im in usa......so whats so great bout this place?pathetic!
lost in the outbacks!.....gotta find myself way back
got back from australia( now you are in usa)
jus got back from usa(now in england)
jus got back from england.....!(now in india)
jus got back from india!.......finally here...what a ride(home sweet main.....program ends

This is not a nested function. Just a simple function calling other function.
 
OP
P

prasadzmultiplex

Broken In
aadipa said:
lamrinnirmal said:
dude KuKlux pm me your email .... i ll host a few ebooks on C on a hosting server where you can download em off........or just search the forum....there are quite a lot of links to ebook sites(all legal!)

about nested functions............hope the below code clears your confusion.

imagine of a traveller moving to the different countries from main and when hes in australia() he doesnt go to anyother place(cause australia() doesnt call any function) and thus to find his way back to main he climbs up the way he came down.........

This is not a nested function. Just a simple function calling other function.

Thanx Nirmal for the prog..I'm sure gonna try it out.
And aadipa a function calling another function is called Nesting of functions.
Atleast I think it is...Please correct me If I'm wrong.

As in case of Loop within a loop leads to various iterations.a function
within another function leads to nesting of functions.I have jus stepped
onto Recursions and they make my head go ga-ga :shock:

And Hafees.....the Book I'm presently referring to is Programming in C
By Venugopal & Prasad a/w the Kanetkar & Balagurusamy ones.....
 

lamrinnirmal

Journeyman
@aadipa- the poster has defined nested functions in his post as:
Nested Functions=Functions Within Functions.....They're a bit confusing...
Particularly the ones with many nestings.....

since every function called from any other function returns back to the point where it was called a nest is formed is'nt it?.........
anyways i made up the program reading his post as he has defined...... if im wrong let me know pal....
 

tuxfan

Technomancer
Ok. So the confusion is cleared about what is nested function. Actually, there is nothing called nested function. There may be nested loop. For example

Code:
while (TRUE)
{
   some code.... ;
  for (x=1; x<=20; x++)
  {
     some code .... ;
  }
}

These functions calling another function is pretty simple. Tell us what you don't understand about them.
 
OP
P

prasadzmultiplex

Broken In
tuxfan said:
Ok. So the confusion is cleared about what is nested function. Actually, there is nothing called nested function. There may be nested loop. For example

Code:
while (TRUE)
{
   some code.... ;
  for (x=1; x<=20; x++)
  {
     some code .... ;
  }
}

These functions calling another function is pretty simple. Tell us what you don't understand about them.

Yeah I pretty much understand what the nesting of loops is all about...
But u ce ethe thing with functions is that they return to where it was called
from after performing the task.That is where I get into a mess....

For eg..I call a function at line 6 of my prog....It goes to its definition
which i have provided at the end,carrieds out the task & returns back
to line 7 and then the prog continues....

WIth nesting of functions,it becomes complicated.....If suppose there is
a function within the definition of the earlier function,where does it go to
after it gets executed? Back to the Definition part or the Earlier function
called part??
Also what do u mean when u say " A Function returns with a value"?
 

hafees

In the zone
Actually y do u use the term nested function like nested if or nested loops. Actually u CAN'T define a function inside another function body.

A function returns to from where it is called. If A calls B and B calls C and C calls D, then D is returned to C and C is returned to B and so on. In no way D can return to A(unless D is called from A).

Return with a value means a function is not void.
By default every function is supposed to return an int

that is

myfunc() is same as int myfunc()
.if u want to make it return nothing then make it void
eg: void my_func(int x) { ... }
A function can return only one value.
 

tuxfan

Technomancer
prasadzmultiplex said:
Yeah I pretty much understand what the nesting of loops is all about...
But u ce ethe thing with functions is that they return to where it was called
from after performing the task.That is where I get into a mess....

If this doesn't happen, you will be in a bigger mess :shock: Where do you think should it go after the function is finished? Just think, what will make it convenient?

prasadzmultiplex said:
For eg..I call a function at line 6 of my prog....It goes to its definition which i have provided at the end,carrieds out the task & returns back to line 7 and then the prog ontinues....

This is called sequential programming as compared to event based programming. Usually (not a rule), under GUI, event based programming is used and under text based environments sequential programming is used.

prasadzmultiplex said:
WIth nesting of functions,it becomes complicated.....If suppose there is a function within the definition of the earlier function,where does it go to after it gets executed? Back to the Definition part or the Earlier function called part??
Huh?? What are you trying to ask? Control is never returned back the statement that defines a function. It is always retuned to the statement that calls it.

prasadzmultiplex said:
Also what do u mean when u say " A Function returns with a value"?
Take a very simple example. You have a function called Add3Numbers(). This is how you call it. Here the function returns a value.

Code:
Sum1 = Add3Numbers(23,12,34);
Sum2 = Add3Numbers(num1,num2,num3);

This function will be coded like this.
Code:
int Add3Numbers (int a, int b, int c)
{
  int total;
  total = a + b + c;
  return (total);
}

Hope you have understood. :)
 
OP
P

prasadzmultiplex

Broken In
Thanxx a lot Tuxfan & hafees.....I seem to have got Functions and their nesting
all taped out.As tuxfan explained,the 'Returning' part was a bit foggy 4 me and
thanx to u & some books/tutorials,i seem to have grasped them.

Now I hate bothering u guys again but I have hit a wall again.This time
its the arrays.I have seen some tutorials as supplied by nirmal and they're good.
But there are no programs on the net are there? Googling is os little use.
I have managed most of the programs given in the exercise but two of them
are eating my head away.

1)A Prog to accept data in2 an array so that Even nos. go into Even
Indexes & odd nos. go into Odd Indexes.

2)A Prog to enter data into a 3X3 matrix so that 1st data goes into 9th
cell,2nd into 8th & so on.....
 

tuxfan

Technomancer
Mr. Racist aka Mr. Facist, what are you trying to ask? I didn't understand the first question :( If its only about odds and evens, why not use a mod operator?

As for second, it can be achieved by 2 for() loops. Instead of incrementing counters, you will have to decrement them.

I hope these hints will help you do it :)
 
hey guys what about using unix for programming. is anybody using. then has nayone tried graphics in c or c++. And i think i shold suggest Harbeld Schild for c++ using unix. The complete refrence. Not sure about the name though. But he has given a very good examples on every concept. The price might be Rs.395.
 
OP
P

prasadzmultiplex

Broken In
tuxfan said:
Mr. Racist aka Mr. Facist, what are you trying to ask? I didn't understand the first question :( If its only about odds and evens, why not use a mod operator?

see, an array a[5] has a[0],a[1],a[2],a[3],& a[4] indexes......
u hafta enter data into the array such that Odd nos go into the odd indexes
and even nos go into the even indexes.
also this should be dome that as one enters,the data,it gets into the respective
index....No storing in a temp array (Say b[5] ) and then re-arrange the inedexes...

Hope i'v emade it clear :oops:

And the second one i got the idea...thanx a ton
 

ujjwal

Padawan
For the first program, you just keep two seperate variables, say i for even numbers (initialised at 0), and k for odd numbers (initialised at 1). Now, if the number is even (check it using the remainder operator), put the number into the i'th element of the array, and then increment i by 2. Similiarly, you can place odd numbers into odd positions :)
 

lamrinnirmal

Journeyman
gave it a lot of thought and i now conclude that you can do it using functions ...... your array then must be declared global and your loop counters must be static.... give it a shot.... if someone posts a better solution here hats off to him!
 

lamrinnirmal

Journeyman
@ujwal - i tried your technique..... there is a flaw....

Code:
for(i=1,j=0;i<6;i=i+2,j=j+2)
{
scanf("%d",&data);
if(data%2!=0)
a[i]=data;
else
a[j]=data;
}

say i enter an even no... then sure a[0] contains the even no but my j is now 3 not 1!!.... and hence a[1] will never get filled!
 

ujjwal

Padawan
@lamrin - the idea was not to put i=i+2 and j=j+2 in the loop update expression, but

if(data%2!=0)
{ a=data;
i=i+2;
}

I think this should work without trouble.
 

lamrinnirmal

Journeyman
hmm......that sounds plausibe ujwal....though i ve not tried it does your program sounds like the below code?:

Code:
i=0;
j=1;
while(i<=20&&j<=19)
{
scanf("%d",&data);
if(data%2!=0)
{
a[j]=data;
j=j+2;
}
else
{a[i]=data;
i=i+2;
}
}

yeah.....looks good.... my logic (previous post) was too roundabout.....

hats off to you man! d :-o
hey get an avatar with that guy removing his hat "off"...it ll look neat!

prasadzmultiplex said:
Now I hate bothering u guys again but I have hit a wall again

dude....we come here to get bothered and to listen to people who run into walls .....and sometimes when we run into a wall we bother the other guys..... this is a community to remove all walls.period.
 
OP
P

prasadzmultiplex

Broken In
Thanx a Ton Ujjwal & nirmal,u guys must be really hardcore Programmers!!!

I shall try thm out & let u know the results....

Can i save the Prog i typed as a *txt file or should I have to type it again in Word?
 

tuxfan

Technomancer
How can you guys directly give code to a learner? Then how will he learn? Don't spoon feed him. Give him hints, not the code. It will be more satisfying even for him if he solves his problems on his own :D

@ prasadzmultiplex, can you see the %2 in their codes? That % is a mod operator. That is what I told you to use :)
 
Status
Not open for further replies.
Top Bottom