tags:

views:

112

answers:

5

If I have an array and I need to display how many times the number '12' is created. I'm using a function to go about this. What resources should I look into to find how to exactly tease out this one number and display how many times it is in the array/list? Any help would be greatly appreciated.

A: 

Iterate over the array with a loop, incrementing a counter as you go.

int counter = 0;

for(int index = 0; index < myArray.length; index++)
{
    if (myArray[index] == 12)
        counter++;
}

printf(counter);
uosɐſ
he said c as tag, not java
Roalt
What is `println` in C?
Sinan Ünür
And even so, how do you think giving somebody the answer to a homework question is going to help them?
GMan
thanks GMan. I don't want the answer, just trying to figure it out in my head so I don't have to bug you later :) I appreciate the help.
HollerTrain
Just kidding HollerTrain, you're not hopeless.
uosɐſ
That's very kind of you, Jason. (Hint: sarcasm. That's very rude.) Also, you clearly don't actually know C.
GMan
Aw, come on, what's that for? I gave a nice clean implementation!
uosɐſ
GMan is too harsh
uosɐſ
i guess Jason is right. I shouldn't be asking for guidance.
HollerTrain
"I gave a nice clean implementation!" First off, like we've said, don't go around giving implementations to homework questions. Secondly, this isn't even an implementation. It's in some fictional language.
GMan
@Jason: Because you shouldn't have. It's a homework question - by giving the answer, you made it so HollerTrain doesn't learn anything. By HollerTrain not learning anything, no skills are attained. A programmer with no skills means more garbage code people get stuck trying to fix or maintain. Don't answer homework questions with coded solutions; you're hurting more people than you know (including the person asking the original question).
Ken White
No, you should be asking for guidance. Guidance is completely normal, nobody knows everything. What do you think teachers are? They're just professional guiders.
GMan
Don't be sad, friend. You'll get through. I really do encourage you to learn this stuff. It will be very rewarding if you find a passion
uosɐſ
Your code is some bizarre hybrid of C and Java. You have a printf function (which incorrectly takes an integer), along with a .length member of the array.
Charles Salvia
Well now who's looking for an exact answer? Psuedo-code is a great teaching tool! And the printf was just a smart-ass edit because somebody complained about my java code.
uosɐſ
+3  A: 

You can do it by walking through the array, while keeping a tally.

The tally starts at 0, and every time you reach the number you want to track, add one to it. When you're done, the tally contains the number of times the number appeared.

Your function definition would probably look something like this:

int count_elements(int pElement, int pArray[], size_t pSize);
GMan
+1 for showing a generalized version. But please change `int pSize` to `size_t pSize`.
Chris Lutz
Oops, indeed​​​.
GMan
A: 

If you have a plain C-array, you have to iterate over all elements in a loop and count yourself with a variable.

dwo
A: 
int arr[20];
int twelves = 0;
int i;

/* fill here your array */


/* I assume your array is fully filled, otherwise change the sizeof to the real length */
for(i = 0; i < sizeof(arr)/sizeof(int);++i) {
  if(arr[i] == 12) ++twelves;
}

After this, the variable twelves will contain the number of twelves in the array.

Roalt
Don't give him the answer, people don't learn that way.
GMan
The terminating condition in the for loop should be `i < sizeof(arr) / sizeof(int)`
Charles Salvia
Thanks, changed accordingly
Roalt
I don't think he will get his master thesis by my answer, but maybe he learns something of it.
Roalt
+2  A: 

Simply create a counter variable, and examine each element in the array in a loop, incrementing the counter variable every time an element is equal to 12.

Charles Salvia