Thread: please walk me through this code

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    21

    Question please walk me through this code

    Can somebody please walk me through this problem and show me how you get an output of:
    [0]
    [14]
    [28]

    I've been looking at it for the past 30 minutes and I still can't figure out how this code is supposed to work? Thanks in advance!

    Code:
    What is the output of the following program?
    --------------------------------------------------------
    	#include <stdio.h>
    	
    	int main()
    	{
    		int x[20];
    		int count1, count2;
    
    		for( count1 = 0; count1 < 20; count1++ )
    		{
    			x[count1] = count1 * 2;
    		}
    
    		for( count2 = 0; count2 < 20; count2 += 7 )
    		{
    			printf( "%d\n", x[count2] ); 
    		}
    		return 0;
    	}

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    What's the problem with it?

    count1 is being increased by one each loop, and count two is being increased by seven. the zeroth element of x is 2*0=0. The seventh is 2*7=14 and the 14th is 2*14=28. The next iteration for the second loop gives a value of 21 for count2 which is greater than 20 so the loop terminates and the program completes.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The first loop puts all of the even numbers:
    Code:
    0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38
    into the array. The second loop prints every 7th number after 0, so it prints the red ones before count2 is bigger than 20:
    Code:
    0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Oh I see, I was confused as to what the relationship between count1 and count2 was. So for this code, the first part sets freq[1]-freq[4] equal to zero? Then I'm not sure what the second part is supposed to do. The output should be (3 1 2 0 1)


    main()
    {
    int i;
    int freq[5];
    int a[] = {0,1,4,0,2,2,0};

    for(i=0;i<5;i++) {
    freq[i]=0;
    }

    for(i=0;i<7;i++) {
    freq[a[i]]++;
    }

    for(i=0;i<5;i++) {
    printf("&#37;d ",freq[i]);
    }
    printf("\n");
    }
    Last edited by cboard; 04-11-2007 at 04:00 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > the first part sets freq[1]-freq[4] equal to zero?
    No, both count variables are assigned zero so that we can count indices.
    Something in pseudocode might be more legible:
    Code:
    Let A[] be an array of 20 items
    
    count = 0
    for each item in A[]:
        A[item] = count * 2
        count++
    
    for each seventh item in A[]:
       print A[item]
    Does it make sense now?

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    I forgot to put the second code in my last post sorry! But yeah that makes perfect sense, do you mine writing me a psuedo code for that second code? If you don't mind of course

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Basically
    Code:
    Let A[] be an array of some number of items
    Let B[] be an array of 5 items
    
    for each item in B[]:
       B[item] = 0
    
    for each item in A[]:
        save = A[item]
        access B[save] then increment B[save]
    
    for each item in B[]:
       print B[item]
    Skipping the pseudocode step is reserved for expert programmers.

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I'm being picky here but your pseudocode is misleading. In fact, the for each operation is used incorrectly:

    If you were to write "for each item in B[]", then item already refers to the value, not to the index. You can output all the values of an array like this:

    Code:
    for each item in B[]
        print item
    The correct way of writing is to say:
    Code:
    Let A[] be an array of some number of items
    Let B[] be an array of 5 items
    
    for each index in 0 to length(B[]) -1:
       B[index] = 0
    
    for each item in A[]:
        B[item] = B[item] + 1
    
    for each item in B[]:
       print item
    I know it's not "that" important to write correct pseudocode, but your construction of for each is against every syntax of every language implementing the foreach sugar.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I know that Koni. Thanks for the concern anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  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