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; Help i need array+loop and count C++ i need code for count the same value in array Ex. ____________________________________________ Enter ...

  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,675
    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,675
    • 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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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,675
    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
    laserlight likes this.

  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
    21,310
    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.
    Salem 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

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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
    Name:  Screenshot_1.png
Views: 116
Size:  1.5 KB




    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
    21,310
    What is your current code?
    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

  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

Page 1 of 2 12 LastLast
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