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