Originally Posted by
rstanley
@userxbw
Let's take look at your code:
I count two for() loops, not one!
Your array was initialized to 5,0,0,0,... What if the first random number was '0'?
What is 'var2'? How is this better than 'count'?
You have not corrected the things I mentioned. How does this version improve on what I presented, and recommended?
How is this "Backwards" and why should it be done "Backwards"?
I and most programmers, prefer the "KISS" (Keep It Simple Stupid) method. (No offense intended to anyone!) In addition, clarity in code is also important especially for someone else who might have to maintain the code later on. The OP's version was very clear except for the third variable he didn't need, and the corrections that I presented.
mmmm your code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int count = 0;
int myArray[100];
int i;
srand(time(0)); // Seeds Random Numbers
// for(i=99; i>0; i--)
// one for loop not two
// I see no other decloration of for ( .... )
for(i=0; i<100; i++)
{
// putting in all of the values first 0,1
myArray[i] = rand()%2;
printf("%d\n", myArray[i]);
// then just going to that very same element next in one for loop.
// then checking to see what it was that was just put in there.
if(myArray[i] == 1)
{
printf("%d - Yes\n", myArray[i]);
count++; // keeping count of how many times a one happened.
}
}
printf("Number of times one occurs is: %d\n", count);
}
my code
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int myRandomNumberArray[100] = { 5 };
srand((int)time(0)); // Seeds Random Numbers
int var1,var2 = 0;
// one for loop
for (var1=0; var1<100; var1++)
{ // instead of doing it your way in one loop
// backwards or opposite of
// check for the one first, then place
// it in the array
if( ( rand()%2) == 1)
{
myRandomNumberArray[var2] = 1;
printf("got a 1 \n");
var2++;
}
}
printf("Number of times one occurs is: %d\n", var2);
for ( var1 = 0; var1 < var2; var1++)
printf("%d: values capturded %d\n",var1+1, myRandomNumberArray[var1]);
return 0;
}
the usage of what name is given to whatever variables is arbitrary.
he said he can only use two vars, so it is a way of showing that only two vars are being used, point blank.
then I gave him why would someone do it that way, impactions , if one is seeking through data and only needing to keep some of it depending on what that is, then it can be checked first if it matches the criteria then save it in the array, else let it go by, then you now have an array full of usable data stored in the array for later use.
that was ALL done with one for loop, the second for loop is not part of that first equation, it merely shows how that first for loop worked, or what it actually did, it is all done to elevate his knowledge in what and why's. not to say what you did was wrong.
but if you look at what is being done it still equates to the same thing. looking for 1 in a random sequence between 0,1 using one loop . thereby nullifying the need for an array.
Furthermore, I bet you a small duck the teacher is trying to get them to learn how to fill an array first with values without over running the array, then check for a certain value next then make note of how many times that took place. then print that out to the screen last.
this here
Code:
Your array was initialized to 5,0,0,0,... What if the first random number was '0'?
that also is arbitrary, because it is changed during the filling of the array.
your logic , then I could say what if you set the elements to zero , and
What if the first random number was '1'? would that not also conflict with your line of thought?
Code:
int myRandomNumberArray[100] = { 5 };
with that and the loop i put in one of them I posted to print out what is inside of it, shows him what?
that the rest still get zero,
Code:
int myRandomNumberArray[100] = { };
would then do what?
it only got put into that last one because I reused the file, and again it is of no consequence.
backwards, instead of filling the array with everything then looking for what is usable, just look for what is usable first then store it in the array.