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

  1. #1
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9

    Question Help i need array+loop and count C++

    Help i need array+loop and count C++

    i need code for count the same value in array



    Ex.
    ____________________________________________
    Enter 10 #:1 2 3 4 4 4 4 4 4 5<---------------for array
    Enter a Number: 4 <------------------entering the same for counting
    Answer :6 <---------------------------- the Result
    ____________________________________________

    this is my code:

    #include <stdio.h>
    int main()
    {
    int a[10],x,z;
    printf("enter a number");
    for(x=0;x<=9;x++)
    {
    scanf("%d" ,&a[x]);
    }
    for(x=0;x<=9;x++)
    {
    }
    printf("enter a number");
    scanf("%d" ,&z);
    if(z==a[x])
    z++;
    printf("answer is %d" ,z);
    {
    }
    }





    plsss help me
    i hope you can help Me !!!!!!!
    Last edited by Marvin Flores; 10-06-2012 at 07:42 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    We can But first post your attempt please (in other words the code you have so far )

  3. #3
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by std10093 View Post
    We can But first post your attempt please (in other words the code you have so far )
    Code:
     #include <stdio.h>
    int main()
    {
    int a[10],x,z;
    printf("enter a number");
    for(x=0;x<=9;x++)
    {
    scanf("%d" ,&a[x]);
    }
    for(x=0;x<=9;x++)
    {
    }
    printf("enter a number");
    scanf("%d" ,&z);
    if(z==a[x])
    z++;
    printf("answer is %d" ,z);
    {
    }
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    • The check if the element of the array is the same as z has to be in the loop that goes through the whole array.
    • Also z is not initialized to zero,as it ought to be.
    • you should add a line return 0; at the end of main (this you should do it in general)
    • tidy up your code style a bit.The bodies of the if and for must be one tab to the right


    hope this helps

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sigh. This is not C++. This is C. Learn to know the language you study, please.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    Sigh. This is not C++. This is C. Learn to know the language you study, please.
    Take it in stride instead of being frustrated. Different people make the same mistake, and have no idea and don't care who else has made the mistake.

    As for the question, std10093 needs an extra bullet.
    • x < 10 is the conventional form...

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe in enlightening those who are in error. They have a right to know the difference between two entirely different languages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9

    Unhappy i can`t run program perfectly

    i can`t run program perfectly help me!!!

    enter # 1 2 3 4 4 4 4 4 4 4
    enter # 4
    answer 5

    i want count the same value in array
    like this

    enter # 1 2 3 4 4 4 4 4 4 4
    enter # 4
    answer 7
    help me!!!

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    can you please post your updated code ?
    careful not to re-edit the old one,but to create a new post with the updated one

  10. #10
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by std10093 View Post
    can you please post your updated code ?
    careful not to re-edit the old one,but to create a new post with the updated one
    Code:
    #include <stdio.h>int main()
    {
    int a[10],x,z;
    printf("enter a number");
    for(x=0;x<=9;x++)
    {
    scanf("%d" ,&a[x]);
    }
    for(x=0;x<=9;x++)
    {
    }
    printf("enter a number");
    scanf("%d" ,&z);
    z=0;
    z++;
    printf("answer is %d" ,z);
    return 0;
    }
    but now chage value to 1
    enter # 1 2 3 4 5 6 7 8 8 8
    enter # 8
    answer 1

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    int main()
    {
        int a[10], x, z;
    
        printf("enter a number");
        for (x = 0; x <= 9; x++)
        {
            scanf("%d" , &a[x]);
        }
    
        for (x = 0; x <= 9; x++)
        {
        }
    
        printf("enter a number");
        scanf("%d", &z);
        z = 0;
        z++;
        printf("answer is %d", z);
    
        return 0;
    }
    Now, it is obvious that your second for loop has no net effect: it might as well not exist. Then, you read a number from the user and store it in z, but immediately assign 0 to z and increment it, hence the "answer is 1", always.
    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

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Let's take it step by step because i think you are a bit of confused
    The goal is to produce code for count the same value in array (copy paste from your post)
    So i need to declare an array and fill it with values.You did excellent here
    Code:
    int a[10],x,z;
    printf("enter a number");
    for(x=0;x<=9;x++)
    {
          scanf("%d" ,&a[x]);
    }
    Then i need to read a number.This number is what i have to search for in the array.We need to declare a variable in order to remember this number.You declared z and then you have this to read the input
    Code:
    printf("enter a number");
    scanf("%d" ,&z);
    So far so good.Now we have to count how many times this z is inside the array!So...we need an extra variable in order to count.As a result let's call it counter
    From where should we start counting?From zero of course!But how can our code knows that?We will inform the code!How?Just by initializing the counter to zero
    Code:
    int counter = 0;
    Now you have to start searching
    We need to go through the whole array,so we have to use a loop.In this loop we are going to check again and again if z is equal (remember, we use two signs of equal for comparison ) to the element of the array we check at a time,we should increase the counter by one.Something like this
    Code:
    for( x = 0 ; x < 10 ; x++)
    {
            if(z == a[x])
            {
                   counter++;/*This is equivalent to this counter = counter +1; and this counter+=1;*/
             }
    }
    Now try to put everything together.Why i do not did this for you?Because you will miss all the fun then

    Also remember from previous posts this
    • tidy up your code style a bit.The bodies of the if and for must be one tab to the right
    • x < 10 is the conventional form...


    thanks to whiteflag for the second bullet

  13. #13
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9
    Tnx for the code std10093
    now im try to remove this
    Help i need array+loop and count C++-screenshot_1-png




    Quote Originally Posted by std10093 View Post
    Let's take it step by step because i think you are a bit of confused
    The goal is to produce code for count the same value in array (copy paste from your post)
    So i need to declare an array and fill it with values.You did excellent here
    Code:
    int a[10],x,z;
    printf("enter a number");
    for(x=0;x<=9;x++)
    {
          scanf("%d" ,&a[x]);
    }
    Then i need to read a number.This number is what i have to search for in the array.We need to declare a variable in order to remember this number.You declared z and then you have this to read the input
    Code:
    printf("enter a number");
    scanf("%d" ,&z);
    So far so good.Now we have to count how many times this z is inside the array!So...we need an extra variable in order to count.As a result let's call it counter
    From where should we start counting?From zero of course!But how can our code knows that?We will inform the code!How?Just by initializing the counter to zero
    Code:
    int counter = 0;
    Now you have to start searching
    We need to go through the whole array,so we have to use a loop.In this loop we are going to check again and again if z is equal (remember, we use two signs of equal for comparison ) to the element of the array we check at a time,we should increase the counter by one.Something like this
    Code:
    for( x = 0 ; x < 10 ; x++)
    {
            if(z == a[x])
            {
                   counter++;/*This is equivalent to this counter = counter +1; and this counter+=1;*/
             }
    }
    Now try to put everything together.Why i do not did this for you?Because you will miss all the fun then

    Also remember from previous posts this
    • tidy up your code style a bit.The bodies of the if and for must be one tab to the right
    • x < 10 is the conventional form...


    thanks to whiteflag for the second bullet

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    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

  15. #15
    Registered User Marvin Flores's Avatar
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by laserlight View Post
    What is your current code?
    Code:
    #include <stdio.h>main()
    {
    int a[10],x,z;
    printf("enter a number");
    for(x=0;x<=9;x++)
    {
    		scanf("%d" ,&a[x]);
    }
    printf("enter a number");
    scanf("%d" ,&z);
    int counter = 0;
    for( x = 0 ; x < 10 ; x++)
    {
    		  if(z == a[x])
    		  {
    					counter++;
    			printf("answer %d" ,counter);
    			}
    }
    }
    im trying to fix display answer in my own tnx a lot for
    advice and this code

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