Thread: Can this be reduced to two INT variables?

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    Can this be reduced to two INT variables?

    Instructions: 2. Randomly generate 100 integers in the range [0, 1] using rand() function (remember toseed the rand generator) and save the numbers in an integer array. Write C code to computethe total number of integers in the array which are 1.You may use only TWO integer variables, ONE integer array in the code and no othervariables/arrays of any other type may be used. [10]



    I've got the code to work right now, but I have three int variables (because of the count) and one int array. I'm not sure how I could get it to have the same execution but with two int variables.

    Penny for your thoughts?

    Thank you!





    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    
    
    int count = 1;
    int randNum;
    int myArray[100];
    int i;
    srand((int)time(0)); // Seeds Random Numbers
     
     
      for(i=99; i>0; i--)
      {
    	  randNum = rand()%2;
    	  printf("\n%d", randNum);
    	  myArray[i] = randNum;
    	  
    	  if(myArray[i] == 1)
    	  {
    	  printf("- Yes", myArray[i]);
    	  count++;
    	  }
      }	  
      printf("\nNumber of times one occurs is: %d", count);
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    Instructions: 2. Randomly generate 100 integers in the range [0, 1] using rand() function (remember toseed the rand generator) and save the numbers in an integer array. Write C code to computethe total number of integers in the array which are 1.You may use only TWO integer variables, ONE integer array in the code and no othervariables/arrays of any other type may be used. [10]



    I've got the code to work right now, but I have three int variables (because of the count) and one int array. I'm not sure how I could get it to have the same execution but with two int variables.

    Penny for your thoughts?

    Thank you!
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    
    
    int count = 1;
    int randNum;
    int myArray[100];
    int i;
    srand((int)time(0)); // Seeds Random Numbers
     
     
      for(i=99; i>0; i--)
      {
          randNum = rand()%2;
          printf("\n%d", randNum);
          myArray[i] = randNum;
          
          if(myArray[i] == 1)
          {
          printf("- Yes", myArray[i]);
          count++;
          }
      }      
      printf("\nNumber of times one occurs is: %d", count);
    }
    you're still struggling with this? two major things need to take place, three really.
    1. declare data types,
    2. fill array
    3. process data in array.
    using only one array, and two data types. three things total. simple math.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
     
     
    int main(void)
    {
    
    
    int randNum;
    int myRandomNumberArray[100] = { 5 };
    
    srand((int)time(0)); // Seeds Random Numbers
     
    
    
    int t, g;  // two ints
     int z;
     for ( z = 0; z < 100; z++)
       printf("elemet value is %d\n", myRandomNumberArray[z] );
    
    //for ( t = 99; t >= 0; t--)
    for ( t = 99; t > 0; t--)
    {
      myRandomNumberArray[t] = (rand()%2);
    }
    
    t = 0;
    int size = sizeof(myRandomNumberArray)/sizeof(myRandomNumberArray[0]);
    int occured = 0;
    
    printf("size of array? %d\n", size);
    
    while ( t <= sizeof(myRandomNumberArray)/sizeof(myRandomNumberArray[0]) )
    {
     
     
      if (myRandomNumberArray[t] == 1)
      {
            printf("num %d = Random Number is %d\n",t, myRandomNumberArray[t]);
        g++;
        printf("g = %d\n",g);
      }
      t++;
        occured++;
    }
    printf("1 occured %d times\n",g);
    printf(" I counted how many elements? %d\n", occured);
    
     
    int a = 99;
    
     while(a > 0) {
       // Will iterate the conditions until counter hits 0.
    printf("a = %d\n", a);
    printf("1 occured %d times\n",g);
    a--;
     }
    
     
    }
    you need to figure out which two vars to keep and what needs to be cleaned up and removed, and even perhaps what needs to be removed, because you have not covered that in your classes yet, so use a different method of doing the same thing. ( if that is not a brain teaser I do not know what is)
    Last edited by userxbw; 10-16-2017 at 07:40 AM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    You don't need randNum.
    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--)
    
       for(i=0; i<100; i++)
       {
          myArray[i] = rand()%2;
          printf("%d\n", myArray[i]);
    
          if(myArray[i] == 1)
          {
             printf("%d - Yes\n", myArray[i]);
             count++;
          }
       }
       printf("Number of times one occurs is: %d\n", count);
    }
    Also, newlines are usually placed at the end, not the beginning. Please use consistent indentation. No need to cast the return from time() to an int. And finally, you usually loop from the start of the array to the end, not the reverse.

    EDIT: You also don't need to include math.h! Missed that one! ;^) And your count should be initialized to zero not 1. You are "Off-by-one"!
    Last edited by rstanley; 10-16-2017 at 07:51 AM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    here is a backwards way of doing the same thing by using one loop.
    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;
    
        for (var1=0; var1<100; var1++)
        {
            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;
    }
    implications: only putting what you want to keep in the array to store it for future use.
    Last edited by userxbw; 10-16-2017 at 08:18 AM.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    @userxbw

    Let's take look at your code:

    here is a backwards way of doing the same thing by using one loop.
    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 rcommended?

    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.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by rstanley View Post
    @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.
    Last edited by userxbw; 10-16-2017 at 10:34 AM.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    the usage of what name is given to whatever variables is arbitrary.
    You miss my point. This is a simple program, but small programs many times grow to become large programs. "var2" does not tell the programmer or the maintainer later, what it's function is. 'count" is more clear! Variables names, other than loop variables, should be clear and are not "arbitrary". Poor programming practices now, complicate maintenance of a larger version later.

    he said he can only use two vars, so it is a way of showing that only two vars are being used, point blank.
    Code:
    int count = 0;
    int myArray[100];
    int i;
    It is clear to me any any other programmer that two variables, and one array are present. "var1" and "var2" does not make it more clear, plus it makes the understanding of the code more confusing.

    Bottom line, we are here to answer the OP's questions, and help to improve the code presented, not to rewrite their code with more confusing "backwards" code. Yes, there are many ways of writing a program.

    This language is confusing enough, and we cannot understand how and why the instructor is teaching the language the way the way he/she is. This assignment was better than many I have seen assigned.

    Sorry, but your code does NOT help the OP.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by rstanley View Post
    You miss my point. This is a simple program, but small programs many times grow to become large programs. "var2" does not tell the programmer or the maintainer later, what it's function is. 'count" is more clear! Variables names, other than loop variables, should be clear and are not "arbitrary". Poor programming practices now, complicate maintenance of a larger version later.


    Code:
    int count = 0;
    int myArray[100];
    int i;
    It is clear to me any any other programmer that two variables, and one array are present. "var1" and "var2" does not make it more clear, plus it makes the understanding of the code more confusing.

    Bottom line, we are here to answer the OP's questions, and help to improve the code presented, not to rewrite their code with more confusing "backwards" code. Yes, there are many ways of writing a program.

    This language is confusing enough, and we cannot understand how and why the instructor is teaching the language the way the way he/she is. This assignment was better than many I have seen assigned.

    Sorry, but your code does NOT help the OP.
    side tracking (deflecting) -10 points.

    ( the way it is left, I bet is wrong, it needs two loops, one to fill it first with random 0, and 1, then another loop to go through it and find all of the ones and do the rest, as I have already posted how to do) so now he has both ways to do it, one, the way I did it, then another that makes the array not even needed. me giving a showing of means to further use an array does not conflict with his already gotten answer to his question ,but he now has gained more then he looked for that is a plus 10 points.
    , and we cannot understand how and why the instructor is teaching the language the way the way he/she is. This assignment was better than many I have seen assigned.
    if one cannot understand what the other is trying to convey then how do you expect the person to even give that person what he or she wants?

    you mean to say YOU cannot understand how and why the instructor is teaching the language the way the way he/she is.
    Last edited by userxbw; 10-16-2017 at 11:16 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'll just leave this here, no variables (almost) and no loops (almost)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int foo ( int x ) {
        if ( x ) {
            return rand()%2 + foo(x-1);
        } else {
            return 0;
        }
    }
    
    int main ( ) {
        srand((int)time(0));
        printf("Sum of rand%%2 in 100 iterations=%d\n", foo(100));
        return 0;
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    Sum of rand%2 in 100 iterations=51
    $ ./a.out 
    Sum of rand%2 in 100 iterations=54
    $ ./a.out 
    Sum of rand%2 in 100 iterations=51
    $ ./a.out 
    Sum of rand%2 in 100 iterations=59
    $ ./a.out 
    Sum of rand%2 in 100 iterations=50
    $ ./a.out 
    Sum of rand%2 in 100 iterations=53
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    I'll just leave this here, no variables (almost) and no loops (almost)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int foo ( int x ) {
        if ( x ) {
            return rand()%2 + foo(x-1);
        } else {
            return 0;
        }
    }
    
    int main ( ) {
        srand((int)time(0));
        printf("Sum of rand%%2 in 100 iterations=%d\n", foo(100));
        return 0;
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    Sum of rand%2 in 100 iterations=51
    $ ./a.out 
    Sum of rand%2 in 100 iterations=54
    $ ./a.out 
    Sum of rand%2 in 100 iterations=51
    $ ./a.out 
    Sum of rand%2 in 100 iterations=59
    $ ./a.out 
    Sum of rand%2 in 100 iterations=50
    $ ./a.out 
    Sum of rand%2 in 100 iterations=53
    exactly, but the assignment is to use 1 array, filled to 100 with randoms between 0,1 then find all of the ones and count how many their are then print it to the screen. using two vars. nothing stating what one is to call the vars. so lets look at what is important in the lesson. is the lesson on how to name variables? no, I see no indication of that in the criteria. the only criteria I see is
    Instructions: 2. Randomly generate 100 integers in the range [0, 1] using rand() function (remember to seed the rand generator) and save the numbers in an integer array.
    that requires to do what first?
    Write C code to compute the total number of integers in the array which are 1.
    which requires to do what next?
    You may use only TWO integer variables, ONE integer array in the code and no other variables/arrays of any other type may be used. [10]
    using only three things, 1 int array,and, 2 int vars, names do not matter in this lesson. that is not part of the lesson here.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
     int myRandomNumberArray[100] = {  };
     int var1, var2;
     srand((int)time(0)); // Seeds Random Numbers
     
     // works either way, but the norm is 0 to a greater number, unless you use 1 or a different number,
    //depending on the circumstances
    // for ( var1 = 0; var1 < 100 ;var1++)
      for ( var1 = 100; var1 > 0;var1--)
    {
      myRandomNumberArray[var1] = (rand()%2);
    }
    //can be removed, just to show what is inside of it
     for ( var1 = 0; var1 < 100; var1++)
        printf("%d : %d\n",var1, myRandomNumberArray[var1]);
     
    var1 = 100;
    var2 = 0;
    int count = 0;
         
        while (  var1 > 0 )
        {
          if (myRandomNumberArray[var1] == 1)
          {
            printf("num %d = Random Number is %d\n",var2, myRandomNumberArray[var1]);
            var2++;
          }
         var1--;
         count++;
         }
        printf("1 occured %d times\n",var2);
        printf(" I counted how many elements? %d\n",count );
         
    return 0;
    }
    the count var can be removed, it is just to show a count.
    Code:
    1 occured 45 times
     I counted how many elements? 100
    Last edited by userxbw; 10-16-2017 at 12:17 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw
    exactly, but the assignment is to use 1 array, filled to 100 with randoms between 0,1 then find all of the ones and count how many their are then print it to the screen. using two vars. nothing stating what one is to call the vars. so lets look at what is important in the lesson. is the lesson on how to name variables? no, I see no indication of that in the criteria.
    Descriptive naming is always part of the criteria of any good instructor (yes, I have taught introductory programming methodology classes at university level), except maybe for some silly fun puzzles, but this does not appear to be one of them since the restrictions, though artificial, are actually reasonable: one variable as a loop counter; another variable as the counter for the number of 1s; yet another variable for the array => all these can be descriptively named, with the loop counter possibly named according to the single letter convention for such variables.

    Personally, it looks like rstanley's post #3 was on the mark, though I think the suggestion that one need not cast the return value of time is actually incorrect (I think time_t technically has no implicit conversion to int), though in practice it should be fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    @laserlight
    ... I think the suggestion that one need not cast the return value of time is actually incorrect (I think time_t technically has no implicit conversion to int), though in practice it should be fine.
    I was under the impression that a time_t type was an unsigned value but I was mistaken, however:

    Code:
    srand((int)time(0));
    Under gcc on Linux:

    time() returns a time_t value.
    srand() takes an unsigned int as it's argument

    time_t is of type __time_t
    __time_t is of long int

    (I can't verify any other O/S / compiler)

    So for the original srand() command above:

    "time(0)" retuns a long int
    the long int is then cast to an int
    In the call to srand(), the int is then converted to an unsigned int

    srand() only needs a unique value, and a simple return from time(0) is sufficient as it's argument, without the cast. In real code, I have never seen it done with a cast, Not wrong, but why do it?

    @laserlight
    yes, I have taught introductory programming methodology classes at university level
    I myself have taught C & C++ at a university level along with other organizations.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    Descriptive naming is always part of the criteria of any good instructor (yes, I have taught introductory programming methodology classes at university level), except maybe for some silly fun puzzles, but this does not appear to be one of them since the restrictions, though artificial, are actually reasonable: one variable as a loop counter; another variable as the counter for the number of 1s; yet another variable for the array => all these can be descriptively named, with the loop counter possibly named according to the single letter convention for such variables.

    Personally, it looks like rstanley's post #3 was on the mark, though I think the suggestion that one need not cast the return value of time is actually incorrect (I think time_t technically has no implicit conversion to int), though in practice it should be fine.
    was on the mark about what? naming the vars I do not disagree that they should be descriptively named, my point was it does not in all reality matter, the lesson was more focused on filling an array then searching same said array for the needed data then doing something with it.

    lessons learned are.first,
    1. how to fill an array with data without over running same said array.
    second
    2. how to iterate through an array without over shooting it.
    three
    3. how to look at the data in the array.
    four
    4. how to take notice and keep count needed data in the array
    five
    5. how to print out results of search.

    checking that data prior to putting in that array in the same loop and still putting in the data that is not needed at the same time just to keep it. and for what?

    that is an illogical process . with logic like that might as well toss your dirty underwear in the same basket as the clean underwear are while taking into account how many clean pairs of underwear you have left to put on before needing to do a wash.

    furthermore, var1 and var2 was being descriptively named, showing the teacher look only two are being used.
    I could say more on that matter about knowing when and what to name the variables depending on the circumstances. but why should I?

    was it this one I brought up casting ? it was only because of the level of learning that may not have taken place yet. if it was this post, that is the reason why. not to say casting is bad either, it has it's place. but putting dirty data in an array at the same time as you finding the stuff you need then still storing it all together at the same time .. really? bad memory management?
    Last edited by userxbw; 10-18-2017 at 05:14 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw
    the lesson was more focused on filling an array then searching same said array for the needed data then doing something with it.
    I suspect so myself (refer to my post #9 here), but going from what csmith03 posted, it might not be a hard requirement as compared to this artificial restriction on the number and types of variables.

    Quote Originally Posted by userxbw
    furthermore, var1 and var2 was being descriptively named, showing the teacher look only two are being used.
    Yes, but what are the purpose of these variables? What do they mean? That's what being descriptive in variable naming is about. Using the typical metasyntactic variable names foo and bar would have been as descriptive as var1 and var2 since it is obvious either way that only two int variables were used, and would have provided an equal amount of contextual description, i.e., none. That's great for examples that are intended merely to show syntax or some generic approach with no specific context, but here we have specific context, so if feasible it is better to encourage newbies towards good practices.

    Quote Originally Posted by userxbw
    was it this one I brought up casting ? it was only because of the level of learning that may not have taken place yet. if it was this post, that is the reason why. not to say casting is bad either, it has it's place. but putting dirty data in an array at the same time as you finding the stuff you need then still storing it all together at the same time .. really? bad memory management?
    You should read what rstanley wrote in post #3:
    Quote Originally Posted by rstanley
    No need to cast the return from time() to an int.
    Quote Originally Posted by rstanley
    srand() only needs a unique value, and a simple return from time(0) is sufficient as it's argument, without the cast. In real code, I have never seen it done with a cast, Not wrong, but why do it?
    Have you read Prelude's old article on using rand()? I found her comments on this use of time to seed rather interesting, i.e., she suggested taking a hash of bytes instead of just passing the result of time, whether as-is or with a cast, on the basis that in theory, even a forced conversion to an integer type is not guaranteed for time_t.
    Last edited by laserlight; 10-19-2017 at 03:29 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    I suspect so myself (refer to my post #9 here), but going from what csmith03 posted, it might not be a hard requirement as compared to this artificial restriction on the number and types of variables.


    Yes, but what are the purpose of these variables? What do they mean? That's what being descriptive in variable naming is about. Using the typical meta-syntactic variable names foo and bar would have been as descriptive as var1 and var2 since it is obvious either way that only two int variables were used, and would have provided an equal amount of contextual description, i.e., none. That's great for examples that are intended merely to show syntax or some generic approach with no specific context, but here we have specific context, so if feasible it is better to encourage newbies towards good practices.
    yet we see foo and bar in almost every "professionals" example in how to do something. if they are not useful to tell the one trying to figure out what to do with what that example if trying to show use with the use of foo and bar. then go . start a protest or riot and tell them to stop doing that too.

    if one has a mind they need to be able to use it to its fullest potential. that means they need to be able to know when using foo and bar is applicable. In other words the programmer should know when it is applicable to use the words he uses for the variables to show or give indication of what he is trying to show the one reading the code what it is for or doing.

    If I was his teacher I'd fully accept the use of anything he called them, as long as the lesson I handed out was learned. because properly naming variables is ones own option, therefore left up to the programmer. it has not been standardized by ANSI or IOS

    You should read what rstanley wrote in post #3:

    Have you read Prelude's old article on using rand()? I found her comments on this use of time to seed rather interesting, i.e., she suggested taking a hash of bytes instead of just passing the result of time, whether as-is or with a cast, on the basis that in theory, even a forced conversion to an integer type is not guaranteed for time_t.
    as far as the rest I stand by what I said in the steps wanted to be taken to do the assignment. first fill the array, then loop through it, find the matching data then keep count of it, then print out results. that is a logical flow pattern. what one calls the variables to do this is purely up to the programmer. you are just stating a rule of thumb, not law.

    using ran I am not really putting into question. just casting a data type to a different data type, if the teacher has not yet covered that and the student show they have done so, that raises a red flag to the teacher, where did you get this information? I did not give it to you. did you go else where to get someone else to give you the answer to this or did you find it out on your own.

    Now the teacher has to or should then put that shown knowledge to the test against that student. to see if he or she does have an understanding of casting data types. If they then fail that test, then they are found out that they cheated in one form or another.

    best to play it safe, and show them something that will not raise a red flag or explain that what you show them to them so they will be prepared to answer that why do you know this, and do they have an understanding of it? Because the teacher has not yet taught it to them. that could be an assignment for some time next week.

    I hope you see the points I am trying to make.

    to a student that is reading that, this does not relinquish your responsibility from asking about something you see that you do not yet understand what someone has shown you and not explained it to you that is outside of your knowledge base. the teachers in here, being anyone and everyone that helps you learn something do not always know what you know so they will show you what they know to try and teach you what they think you need to know. while perhaps thinking you already can understand most of what they are showing you.
    Last edited by userxbw; 10-19-2017 at 08:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reduced row-echelon form for Bigger Matrix Using C codes
    By SarasMuthu in forum C Programming
    Replies: 10
    Last Post: 08-11-2017, 06:22 AM
  2. Reduced Echelon form program
    By Heartprogrammer in forum C Programming
    Replies: 3
    Last Post: 11-20-2012, 10:40 AM
  3. help with reduced fractions
    By southpark in forum C Programming
    Replies: 1
    Last Post: 10-12-2010, 02:28 AM
  4. how to get a reduced form
    By silent_eyes in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2005, 10:15 PM

Tags for this Thread