counting loop

This is a discussion on counting loop within the C++ Programming forums, part of the General Programming Boards category; i am trying to write a loop that counts how many times the number 3 appears in a one-dimensional array ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    counting loop

    i am trying to write a loop that counts how many times the number 3 appears in a one-dimensional array called array.



    Code:
    int count = 0;
    for (int x = 0; x < Array.length; x++)
    	if (Array[x] == 3)
    	count++;
    is this correct?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Looks ok to me.
    Also,
    Why not try the code instead of coming here to see if it is ok. It is ok to test code
    Woop?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    No. ....Maybe. It depends on what Array really is, I can't tell from what you posted. If your array is actually a class, that would depend on whether or not there is a member called length. If it's just an array of ints or something
    PHP Code:
    sizeof Array / sizeof *Array 
    might work better.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    17
    thanks, also i do check the code but sometimes i have noticed that you can get the code to work but it might not be the proper way to write it. so what i am trying to do is make sure that i am writing it properly to make it work.

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Counting the number of times a loop has run
    By CConfusion in forum C Programming
    Replies: 1
    Last Post: 04-07-2006, 10:23 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  4. optimizing loop (frequency counting)... HELP
    By skeptik in forum C Programming
    Replies: 22
    Last Post: 05-24-2004, 09:11 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21