Thread: help would be appreciated(newbie at this)

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Talking help would be appreciated(newbie at this)

    Heres the problem

    Write a program that inouts a serie of integer and pass them one at a time to function even which use the remainder operator to determine if an integer is even. The function should be integer arguement and return 1 is the integer is even and 0 otherwise.

    Heres what i come out with so far

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int integer1;
    	int odd;
    	int even;
    	int remainder;
    
    	printf("Enter an integer\n");
    	scanf("%d", &integer1);
    	
    	remainder = (integer1%2);
    
    	if (remainder == 0) {
    		printf("%d", integer1);
    		printf("1\n");
    	}
    	if (remainder != 0) {
    		printf("%d", integer1);
    		printf("0\n");
    	}
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Hope this helps in your quest. From the FAQ board http://cboard.cprogramming.com/showthread.php?t=31212
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    if anyone can correst my program by rewriting he whole program it would be helpful because i dont really get what to so to correct it.

  4. #4
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    Your program doesn't work?
    Code:
    if (remainder )
    {
       printf("You entered %d, it is odd.\n"
                 "Here's your 0.\n\n", integer1);
    }
    else
    {
       printf("You entered %d, it is even.\n"
                 "Here's yor 1.\n\n", integer1);
    }
    Are you supposed to print 1 if even, or return 1. There's a difference.

    dinjas
    straight off the heap

  5. #5
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    ok, read your post a little slower...

    Code:
    int yourfunc( int value )
    {
       //test whether value is even or odd
      ...
    
      if it's odd
          return 0;
      else
          return 1;
    }
    So, just make a function like this, and call it from main with your integer1 .

    dinjas
    straight off the heap

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    It can be done without an if statement.
    Code:
    Function(int Value)
    {
       Value %= 2;
       return !Value;
    }

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    im new at this too.. but did you forget to put your cast in the printf:

    old code
    Code:
    	printf("Enter an integer\n");
    	scanf("%d", &integer1);
    new code
    Code:
    	printf("Enter an integer\n", integer1);
    	scanf("%d", &integer1);
    hehe, maybe

  8. #8
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    Quote Originally Posted by Quantum1024
    It can be done without an if statement.
    good call, I didn't think of that.

    Quote Originally Posted by <3pi
    im new at this too.. but did you forget to put your cast in the printf:
    as mentioned in the other post, you don't need to do anything special with printf unless you're printing the value of a variable.
    ex.
    Code:
    printf("this is a line without a variable\n");
    will display
    Code:
    this is a line without a variable
    and
    Code:
    int number = 1;
    printf("this one prints the value of the variable \"number\"\n"
           "number's value is %d\n", number);
    will display
    Code:
    this one prints the value of the variable "number"
    number's value is 1
    dinjas
    straight off the heap

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by joker209
    if anyone can correst my program by rewriting he whole program it would be helpful because i dont really get what to so to correct it.
    Are you kidding? You've been given enough hints. Here's another one.
    Code:
    #include <stdio.h>
    OO(O0){return !(O0 & 1);}main(_OO,_00)
    int*_00;{int _0O;char _O0[]="How many\
     integers will you enter? \0is\0odd.\n\
    \0even.\n\0Enter an integer: \0";for(
    _00=&_0O,printf("%s",_O0),scanf("%d",
    _00),_00=&_OO;_0O;printf("%s",_O0+51),
    scanf("%d",_00),OO(*_00)?printf("%d %s\
     %s",*_00,_O0+35,_O0+44):printf("%d %s \
    %s",*_00,_O0+35,_O0+38),_0O--);return _0O;}
    Good luck with that class.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    from reading the problem more closely now and testing my program, none of the code in this thread enable someone to enter series of numbers(more than one) and reads them in one at a time and inputs those number out in order. returns a 1 if the 1st number is a even number and 0 if its a odd number, same for the other numbers.

    because the problem saids

    Write a program that inputs a SERIES OF INTEGERS and pass them one at a time to function even which use the remainder operator to determine if an integer is even. The function should be integer arguement and return 1 is the integer if even and 0 otherwise.

    I dont get this sentence at all: pass them one at a time to function even

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Run result should be something like this

    Enter an integer
    8 93 92
    1 0 1
    Press any key to continue

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    It's basically asking you to create a function called 'even' that returns a 0 if odd and 1 if even. So I would start by creating a function, I'm pretty sure there are enough hints in the thread to go about creating it, but since you still seem pretty confused:

    Code:
    int even(int input)
    {
       return (input % 2 == 0);
    }
    Now you should be worrying about how to get the actual series of integers, theres a number of ways of going about doing this - one of the simpler options is to ask the user how many integers that are expecting and then keep reading in integers and storing it in an array or something. Once you read in all the integers (and processed them using even()) simply just print the array out. I'm pretty sure there are more efficient ways though

Popular pages Recent additions subscribe to a feed