Thread: C Code help

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    C Code help

    Ok i was looking a questions on a mock exam and i am really stuck with this one.

    Write a program to read a list of integers into an array. Using a WHILE loop, go through the array printing out all elements that satisfy all three conditions:

    a) Element is less than the index.

    b) Element is positive.

    c) Element is an even number.


    I can do some of the code but i cannot figure out how to get it to compare the numbers in the array after inputting them and then print out the ones that meet the requirements of the questions above

    can anyone help me in this please. I know some might think i am just being lazy. I can assure you i have been trying to work this one out for a week now. I am dyslexic so find it more difficult to visualise the code for this one. I am sure if some one put the code up it will be a ohh yeah moment.

    Thanks for any help given
    Yrth

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    PSEUDOCODE...

    Code:
    while (index < arraysize)
    
    
     // part a
     if (array[index] < index)
           print array[index]  is less than the index 
    
    // part b
    if (array[index] > -1)
      print array[index] is not negative
    
    // part c
    if (not array[index] & 1)
      print array[index] is even
    Now lets see if you can write that in C code....

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, yrthilian!

    If you could verbalize what part of the exam question specifically, had you stumped, then we could focus our replies into that specific area.

    Using a loop, and comparing two numbers - you want those basics to be << snap! >> put right into your brain. You want to have to restrain your hands from the nearest keyboard to stop them from keyboarding up the answer, in code.

    I hope you'll take the time to mull this question over, until you can not only give the right answer, but give it quickly, and with confidence.

  4. #4
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Code:
    int _array[] = {4, 6, 2, 7, 3, 9, 10};
    int cnt = 0;
    while (cnt < sizeof(_array)) {
    	if((_array[cnt] < cnt) && (_array[cnt] >= 0) && (_array[cnt] % 2 == 0))
    		printf("%d\n", _array[cnt]);
    cnt++;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codeprada View Post
    Code:
    cnt++;
    Ok... I got chastized for this... now it's your turn.

    Generally the policy here is that we do not hand out code snippets to people who are obviously doing homework or quizes... It defeats the whole purpose of learning.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by codeprada View Post
    Code:
    int _array[] = {4, 6, 2, 7, 3, 9, 10};
    Don't use identifiers that start with an underscore. They're reserved for the implementation.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Thank you all for the responses.
    I got the clarity i needed.

    I do understand the policy of not giving the full answers as yes i agree it does not help when learning. I normal would not have asked only i was badly stuck and my real exam is on Friday.

    anyway here is what i managed to make.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define NUM_ELEMENTS    10
    
    void main(void)
    {
        int c = 0,  /* loop counter    */
            pos_count = 0,
            even_count = 0,
            less_than_index_count = 0,
            list[NUM_ELEMENTS];
    
        do
        {
            printf("Enter in a number: ");
            scanf("%d", &list[c]);
    
            if (list[c] < c)
                ++less_than_index_count;
    
            if (list[c] > 0)
                ++pos_count;
    
            if (list[c] % 2 == 0)
                ++even_count;
        } while (++c < NUM_ELEMENTS);
    
        printf("Of the %d numbers:\n", NUM_ELEMENTS);
        printf("%3d were less than their index\n", less_than_index_count);
        printf("%3d were positive\n", pos_count);
        printf("%3d were even\n", even_count);
    
    	printf("Program ended.\n");
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    I had most of the above in my head at the time of asking the question i just could not get it to fit together at the time.

    Only think is that i cant figure out is
    getting it to print out the numbers entered by a user at the end.

    So a user will put in 10 numbers and the program can tell them how many are even and how many are under the index value. but i would like to be able to get it to show the number too.

    Is that possible in C only code?
    This is all done under C intro code i start C advanced next month after the exams.

    Thanks again for all your help on this.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by yrthilian View Post
    Is that possible in C only code?
    This is all done under C intro code i start C advanced next month after the exams.

    Thanks again for all your help on this.
    As an alternative method... make an array of however many numbers you want. Go through a first loop where the user keys in a list of numbers. Then do your parsing on the array in a second loop...

    Is that possible in C code... Ummm yes, easily so... FYI Windows and Linux were both written in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM