GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Feb-2005, 10:16
matthewbarr matthewbarr is offline
New Member
 
Join Date: Feb 2005
Posts: 4
matthewbarr is on a distinguished road
Question

Help with a simple program


Hi there, I'm a newbie!

I'm writing a very small program as follows, to count even and odd numbers. The loop and so on seems to work, but after I enter all ten integers, I get 0 for both the even and the odd counts. I guess I'm getting confused with pointers or something, like maybe I'm incrementing a memory address rather than an actual variable (I'm a little confused about all these * and & things!).

Any help is greatly appreciated!

Cheers,

Matt

CPP / C++ / C Code:
/*Find the number of even integers and the number of odd integers in a
list of 10 integers which are typed on the computer keyboard */
#include <stdio.h>
void even_odd(int *even, int *odd);
int main()
{int even,odd;
even = 0;
 odd = 0;
even_odd(&even,&odd);
printf("Even: %d Odd: %d\n",even,odd);
}
void even_odd(int *even, int *odd)
{int n,i;
for(i=1;i<=10;i++)
	{
	printf("Please enter an integer:\n");
	scanf("%d",n);
	if((n % 2) == 0)
	      {
	      *even++;
	      }
	else
	      {
	      *odd++;
	      }
	}
}
Last edited by LuciWiz : 11-Feb-2005 at 07:54. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 10-Feb-2005, 10:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by matthewbarr
Hi there, I'm a newbie!

I'm writing a very small program as follows, to count even and odd numbers. The loop and so on seems to work, but after I enter all ten integers, I get 0 for both the even and the odd counts. I guess I'm getting confused with pointers or something, like maybe I'm incrementing a memory address rather than an actual variable (I'm a little confused about all these * and & things!).

Any help is greatly appreciated!

Cheers,

Matt

/

1. Change this:
CPP / C++ / C Code:
	scanf("%d",n);

to this
CPP / C++ / C Code:
	scanf("%d",&n);

The function scanf() takes an address as an argument (so that it can change the value of whatever variable you are scanning into).

2.

Due to operator precedence, this

CPP / C++ / C Code:
	      *even++;

is the same as

CPP / C++ / C Code:
	      *(even++);


That is, it fetches the int in memory pointed to by "even", then it increments the function's local copy of the pointer. (It doesn't do anything with the value from memory, and some compilers give a message something like "Code has no effect".)

Now, if you want to increment the value in memory pointed to by "even", you must override the operator precedence. Here is a way to do it:

CPP / C++ / C Code:
      (*even)++;

This says: fetch the int in the memory location pointed to by "even". Increment the int and store the new value back into the same place.

Regards,

Dave
  #3  
Old 10-Feb-2005, 10:51
matthewbarr matthewbarr is offline
New Member
 
Join Date: Feb 2005
Posts: 4
matthewbarr is on a distinguished road
Thank you - that's excellent, it works.

Thanks also for the explanation of what is actually going on, too. That will really help in the future!

Cheers,

Matt :-D
  #4  
Old 10-Feb-2005, 11:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by matthewbarr
Thank you - that's excellent, it works.

Thanks also for the explanation of what is actually going on, too. That will really help in the future!

Cheers,

Matt :-D

As programming languages go, C is rather a simple language. One of the first things that typically confuses people (not just beginners) is the somewhat bewildering number of operators. Instead of memorizing precedence tables, I usually recommend that people put in extra parentheses in all but the simplest cases (multiplication and addition, for example). Real "studly" C programmers think it's a sissy thing to do, and in fact some think it's a macho thing to try to put an entire program in one incredibly complex expression statement (with no parentheses).

By the way, I misspoke slightly in my description. I said, "due to operator precedence." I should have said, "due to operator precedence and associativity rules." The pointer dereference "*" and the postincrement "++" actually have the same precedence, but the associativity (order in which the operators are bound) is right-to-left.

Now do you see why I said that people sometimes get confused? Parentheses are less confusing to me than "precedence and associativity rules."

Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
Help with a simple program. bluedragon27 C Programming Language 1 16-Nov-2004 16:40
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06
How to write simple program like ........? laputa9000 C++ Forum 3 29-Oct-2003 13:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.