[Help] C Programming

Status
Not open for further replies.

lamrinnirmal

Journeyman
tuxfan said:
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

bullzeye tuxfan!..... couldnt disagree with you on that!...

@prasadmultiplex- hope you understood the mod operator as tuxfan explained it.... it is really important to get the mod concept right as it simplifies hell lot of problems in C...
in a nutshell: a%b (read % as mod) will return the remainder when a is divided by b. hence if a%b is 0 then b completely divides a...
hope you got the concept wrt to finding even nos.
 

hafees

In the zone
Hi :)
i 've added C puzzles topic . pls try it out. and provide ur own c puzzles.

*www.thinkdigit.com/forum/viewtopic.php?t=16416&start=0&postdays=0&postorder=asc&highlight=
 
OP
P

prasadzmultiplex

Broken In
tuxfan said:
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 :)

I Agree Tuxfan. But lemme assure you that I'm not Copying the code,you helpful guys
are giving,but am grasping the underlying idea and am writing on my own.
Rest assured that I am not blatantly using the code u guys provide.

And yes,I know abt the Modulus operator.Checking for Even/Odd nos. is always
done using the % operator. In case of the array program,I am able to enter
data first into a temporary array (As Ujjwal said abt "Data") and then check for
it being Even/Odd.But I want a prog which Checks a number whether it is even or odd
at the time of entry itself and then Insert it into the respective array.

I thot of using the getch(); operator but what i am abl eto accomplish is that, when I enter an odd number,it gets checked & then scanned into a[1],
next,if i enter another odd number,it gets overwritten onto a[1] but does'nt get into a[3].Same is the case with Even nos.So at the end I have only
a[0] & a[1] in my array.

The Loop within which I'm using the getch(); operator is not able to make the control move onto
the next variables......
 

lamrinnirmal

Journeyman
heres a very rough algorithm you could use for the way you want to enter the variables

:arrow: keep a counter variable to count the no of values entered in the array. also keep two more variables to monitor the odd indices and even indices. and INITIALIZE em!!

:arrow: start the loop ,monitoring the counter variable

:arrow: take the variables , more preferrably using getche so that it "echoes" on your screen

:arrow: check for its oddness/evenness and enter it into the array while also monitoring the odd/even index so that it doesnt exceed the array limit(lack of bounds checking is a pain in the A$$). increment the odd/even variable to keep it ready for the next input.

@hafees- good thread started dude.....thanks!
 
OP
P

prasadzmultiplex

Broken In
lamrinnirmal said:
heres a very rough algorithm you could use for the way you want to enter the variables

:arrow: keep a counter variable to count the no of values entered in the array. also keep two more variables to monitor the odd indices and even indices. and INITIALIZE em!!

:arrow: start the loop ,monitoring the counter variable

:arrow: take the variables , more preferrably using getche so that it "echoes" on your screen

:arrow: check for its oddness/evenness and enter it into the array while also monitoring the odd/even index so that it doesnt exceed the array limit(lack of bounds checking is a pain in the A$$). increment the odd/even variable to keep it ready for the next input.

@hafees- good thread started dude.....thanks!

I tried that before....Not working....Here's the code:

Code:
main()
{
int a[20],i,j,temp;
printf("Enter a Number Please\n");
for(i=0,j=1;i<20,j<20;i=i+2,j=j+2)
{
temp=getch();
if(temp%2=='0')
{
a[i]=temp;
}
else
{
a[j]=temp;
}

This way I'm able to fill only a[0] & a[1].Itz not moving ahead....
 
OP
P

prasadzmultiplex

Broken In
Thanks to all who have Helped me in this Endeavour.I am glad to say that
I have almost Finished Learning C.(If Files are the Last part,then I am Finished :p )But I've heard that Data Structures & Databases ar an Essential
part of C.Which Books are the best for them?
Also I have studied Structures in C.Are they Diff. frm Data Structures?


Now that I have completed C,am thinking of moving towards C++.
Regarding the current market Scenario,Which Course would be best
suitable:.Net or Java? All the Experienced Guys Plz Advise.I have a keen
interest in web Designing,so which one would be more suitable?
 

hafees

In the zone
Congrats !

Now try these puzzles also.
*www.thinkdigit.com/forum/viewtopic.php?t=16416&start=0&postdays=0&postorder=asc&highlight=

Structures are the essential building blocks of data structure.
eg:
struct Stack{
....
..
}
struct LinkedList{
...
} etc.
i recommend these books

i) Data Structures using C & C++ by Tenenbaum and others (published by PHI )
ii) book by Yashwant Kanitkar (similar name)
and this book comes with a nice CD with flash animated illustration.
 

tuxfan

Technomancer
prasadzmultiplex said:
I am glad to say that
I have almost Finished Learning C.

Are you sure? :shock: C & C++ are too vast to be finished like that. Don't sit on your laurels. There are lots of things that you still wouldn't know. Be hungry for more. Learn this from Sachin Tendulkar. The man has scored tone and tons of runs but is still hungry for more and that is the reason for his success :)

Have a look at the link given by hafees. Solve the puzzles and increase your knowledge.
 
OP
P

prasadzmultiplex

Broken In
tuxfan said:
prasadzmultiplex said:
I am glad to say that
I have almost Finished Learning C.

Are you sure? :shock: C & C++ are too vast to be finished like that. Don't sit on your laurels. There are lots of things that you still wouldn't know. Be hungry for more. Learn this from Sachin Tendulkar. The man has scored tone and tons of runs but is still hungry for more and that is the reason for his success :)

Have a look at the link given by hafees. Solve the puzzles and increase your knowledge.

Yeah...I did'n mean that I have Become a Thorough C professional but for a Beginner,
I have learned most of the concepts.i.e. Upto Files & Structures.Other Topics
I shall study as & when Needed.I am still in my intermediate,and would like
to know which topics should be covered further to become a Good Coder.

If Sunny Gavaskar (Hafees) & Kapil Dev (You) will provide me with some
needful tips,I too shall try 2 bcome Sachin :lol:
 

tuxfan

Technomancer
prasadzmultiplex said:
If Sunny Gavaskar (Hafees) & Kapil Dev (You) will provide me with some needful tips, I too shall try 2 bcome Sachin :lol:

:D But we are not yet retired!! :) I feel, you must master the pointers and classes if you want to get good with C/C++. Leave pointers aside if Java is what you want to learn finally.
 

vasanth_12345

Journeyman
Help

Actually i am a little interested in c programming(know a little).One of my friends did a project by which numslock key would be turned on.now i inferred that he had used pointers to change the valu from either 0 to 1 or 1 to 0(binary).Now where will this value be located in the ram or the keyboards buffer.Further can we access the buffer of a device using c
 
OP
P

prasadzmultiplex

Broken In
tuxfan said:
prasadzmultiplex said:
If Sunny Gavaskar (Hafees) & Kapil Dev (You) will provide me with some needful tips, I too shall try 2 bcome Sachin :lol:

:D But we are not yet retired!! :) I feel, you must master the pointers and classes if you want to get good with C/C++. Leave pointers aside if Java is what you want to learn finally.

Sure sure....

On the Puzzles Tread ther was talk abt VDU ,Classes n all Hi-fi Jargon.....
How come I did'nt come across them in C?
 

hafees

In the zone
One of my friends did a project by which numslock key would be turned on.
yes! he should 've used pointers or the library function pokeb();
the memory location 0x417(segment:0000h offset:0417h) stores the value of num lock, caps lock, scroll lock, etc. by changing the appropriate value u can alter the present status of num lock, caps lock etc.
Now i think it can be added in the puzzles thread. some one try it.
 

tuxfan

Technomancer
Re: Help

vasanth_12345 said:
Actually i am a little interested in c programming(know a little).One of my friends did a project by which numslock key would be turned on.now i inferred that he had used pointers to change the valu from either 0 to 1 or 1 to 0(binary).Now where will this value be located in the ram or the keyboards buffer.Further can we access the buffer of a device using c

Here's you go :D use these functions to manipulate caps lock, num lock, scroll lock & insert state. These functions that I post the code here are part of my function library :)

There are some more functions to find out if Alt, Ctrl, Shift, Caps, etc. were pressed. But that code, after a short break ;)

CAPS LOCK
Code:
int caps(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *capstat = 0x417;
     int state;
     state = *capstat & 64;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 64 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 64 )
               *capstat = *capstat - 64;
          return(0);
     }
     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 64 )
               *capstat = *capstat + 64;
          return(1);
     }
}

NUM LOCK
Code:
int num(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *numstat = 0x417;
     int state;
     state = *numstat & 32;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 32 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 32 )
               *numstat = *numstat - 32;
          return(0);
     }
     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 32 )
               *numstat = *numstat + 32;
          return(1);
     }
}

SCROLL LOCK
Code:
int scrolock(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *scrolstat = 0x417;
     int state;
     state = *scrolstat & 16;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 16 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 16 )
               *scrolstat = *scrolstat - 16;
          return(0);
     }

     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 16 )
               *scrolstat = *scrolstat + 16;
          return(1);
     }
}

INSERT
Code:
int insert(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *insertstat = 0x417;
     int state;
     state = *insertstat & 128;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 128 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 128 )
               *insertstat = *insertstat - 128;
          return(0);
     }
     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 128 )
               *insertstat = *insertstat + 128;
          return(1);
     }
}
 

tuxfan

Technomancer
prasadzmultiplex said:
On the Puzzles Tread ther was talk abt VDU, Classes n all Hi-fi Jargon.....
How come I did'nt come across them in C?

That is why I said, you are still not through with C :)

VDU stands for Visual Display Unit. IF you find out which part of the memory stores what is displayed on screen, you can easily manipulate what gets displayed and have a greater control. But you must be comfortable with pointers to do that. So master the pointers ;)

As for classes, they don't exist in C. At max structures come near it. For example see how you have a user defined variable type. There you will have to use structure and a . operator to access member variables.

You will get loads of classes in C++ and Java and many other programming languages including VB, C#, etc. :)
 

vasanth_12345

Journeyman
coldn't undestand it tuxfan "int far *capstat" far is a variable right.Couldnt uderstand his properly "state = *capstat & 64" what does the state function do.Further "0x417" denotes the ram value right then the keyboards buffer rests in the ram and not in the keyboard right.pls correct me if i am wrong further all the keys can be accessed like this or what.Pls help
 
Status
Not open for further replies.
Top Bottom