Thread: Help i need array+loop and count C++

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You should print the answer after the search .So you should move the printf out of the loop,because you want it to be executed only once

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please fix the indentation of your code and post it again. In doing so, you may discover how to fix this problem that you're facing by yourself
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by laserlight View Post
    Please fix the indentation of your code and post it again. In doing so, you may discover how to fix this problem that you're facing by yourself
    tnx for advice

    tnx for helping me!!! laserlight and std10093

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Will this work for you ?

    Code:
    /* Input 10 numbers in an array and count the number of occurence of an element inside the array */
        
    
       #include<stdio.h>
       #include<conio.h>
    
       void main()
    
       {
    
          clrscr();
    
          int a[10],i,n,count=0;
    
    
          for(i=0;i<10;i++)
    
             {
    
                  printf("\n\t Enter element a[%d] : ",i);
                  scanf("%d",&a[i]);
    
              }
    
           printf("\n\n Enter the element you want to count : ");
    
           scanf("%d",&n);
    
           for(i=0;i<10;i++)
    
             {
                 if(n==a[i])
    
                  count++;
    
               }
    
        printf("\n\n %d occurs %d times in your array ",n,count);
    
        getch();
    
      }
    Last edited by somali.cc; 10-08-2012 at 10:23 AM.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Will this work for you ?
    I would have to say no, your code doesn't work for me. First several members of this forum have been telling the OP to properly format the code, so when you post poorly formatted code you to are not following their advice. I recommend you also learn to properly format your code it will make reading your code much easier. Second you change int main() to void main() which is also wrong main should never be defined to return a void, except in very rare circumstances. Lastly providing a complete solution you rob the student of the joy that comes from actually finding their own problems. Along with the fact that they will learn much less.

    Jim

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hey Marvin,

    You don't want to print "Enter a number" without giving more information. Maybe "Enter 10 numbers that will be searched through". Then "Enter a number to search for: ". Just something to give the user a chance at entering the right info - because otherwise, they won't do it.

    How do you want the display answer?

  7. #22
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    I would have to say no, your code doesn't work for me. First several members of this forum have been telling the OP to properly format the code, so when you post poorly formatted code you to are not following their advice. I recommend you also learn to properly format your code it will make reading your code much easier. Second you change int main() to void main() which is also wrong main should never be defined to return a void, except in very rare circumstances. Lastly providing a complete solution you rob the student of the joy that comes from actually finding their own problems. Along with the fact that they will learn much less.

    And I would have to say pls dont try to discourage ppl who want to share and learn C-programming with such out-of-context comments...I have just provided the code that I had felt might work for the thread-starter...So, allow them to reply back with their opinion instead of showing-off ur ' I am the ultimate' attitude... There r more forums for that. We signed in here to learn and discuss C-programming , not to lambast others..

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by somali.cc
    And I would have to say pls dont try to discourage ppl who want to share and learn C-programming with such out-of-context comments
    Many of jimblumberg's comments critiqued your code. That is not out of context.

    Quote Originally Posted by somali.cc
    I have just provided the code that I had felt might work for the thread-starter.
    As jimblumberg noted, please don't do that until it is clear that the thread starter has a working solution, in which case you would be providing an alternative solution for discussion. Thus, your code example would be excellently posted after Marvin Flores has posted one last code sample that he/she is quite satisfied with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop count
    By slash.hack in forum C Programming
    Replies: 7
    Last Post: 03-06-2012, 09:21 AM
  2. count bit loop
    By aromash in forum C Programming
    Replies: 2
    Last Post: 03-08-2011, 09:11 AM
  3. While loop to count the charecters of a string
    By simpatico_qa in forum C Programming
    Replies: 11
    Last Post: 04-24-2009, 03:51 AM
  4. Help for some loop count problem..
    By jochen in forum C Programming
    Replies: 3
    Last Post: 11-30-2007, 08:24 AM
  5. homework help. cant get for loop to count right
    By bluegoo06 in forum C++ Programming
    Replies: 11
    Last Post: 03-10-2005, 06:05 PM