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ɐſ
2009-11-06 21:39:38
he said c as tag, not java
Roalt
2009-11-06 21:41:53
What is `println` in C?
Sinan Ünür
2009-11-06 21:42:09
And even so, how do you think giving somebody the answer to a homework question is going to help them?
GMan
2009-11-06 21:43:13
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
2009-11-06 21:44:13
Just kidding HollerTrain, you're not hopeless.
uosɐſ
2009-11-06 21:45:20
That's very kind of you, Jason. (Hint: sarcasm. That's very rude.) Also, you clearly don't actually know C.
GMan
2009-11-06 21:46:21
Aw, come on, what's that for? I gave a nice clean implementation!
uosɐſ
2009-11-06 21:46:24
GMan is too harsh
uosɐſ
2009-11-06 21:47:53
i guess Jason is right. I shouldn't be asking for guidance.
HollerTrain
2009-11-06 21:48:46
"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
2009-11-06 21:48:55
@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
2009-11-06 21:49:40
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
2009-11-06 21:49:54
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ɐſ
2009-11-06 21:51:23
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
2009-11-06 21:51:43
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ɐſ
2009-11-06 21:54:15
+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
2009-11-06 21:40:03
+1 for showing a generalized version. But please change `int pSize` to `size_t pSize`.
Chris Lutz
2009-11-06 21:45:39
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
2009-11-06 21:41:00
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
2009-11-06 21:41:12
The terminating condition in the for loop should be `i < sizeof(arr) / sizeof(int)`
Charles Salvia
2009-11-06 21:42:49
I don't think he will get his master thesis by my answer, but maybe he learns something of it.
Roalt
2009-11-06 21:48:39
+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
2009-11-06 21:41:30