Like Tree3Likes

Help i need array+loop and count C++

This is a discussion on Help i need array+loop and count C++ within the C Programming forums, part of the General Programming Boards category; You should print the answer after the search .So you should move the printf out of the loop,because you want ...

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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
    21,394
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    2,690
    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
    21,394
    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.
    jimblumberg likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 08:21 AM
  2. count bit loop
    By aromash in forum C Programming
    Replies: 2
    Last Post: 03-08-2011, 08: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, 07: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, 05:05 PM

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