User Tools

Site Tools


opus:fall2012:jcavalu3:discretepart2

discrete Keyword 2

cartesian product

Definition

Cartesian Product is making one set that takes one from a set and pairs it with one value from another set. This is done for each of the values in each set. Example:

  • {1, 2} * {up, down} = {(1, up), (1, down), (2, up), (2, down)}
  • {1, 2, left} * {left, right, up, down} = {(1, left), (1, right), (1, up), (1, down), (2, left), (2, right), (2, up), (2, down), (left, left), (left, right), (left, up), (left, down)}

References

List any sites, books, or sources utilized when researching information on this topic. (Remove any filler text).

  • Reference 1
  • Reference 2
  • Reference 3

discrete Keyword 2 Phase 2

variables (environment/local)

Definition

A local variable is only known in the function that you created it in. It isn't known in any other functions and can't be used in any other function. Each time you use the function is starts at whatever value you assigned it to. Global variables are known and can be used by any function in the program. So if you make a variable outside of main() it is a global variable because it can be used in any function. If you make a variable inside main() it can only be used in main() so it is a local variable.

References

Demonstration

  1 #include <stdio.h>
  2 
  3 int glovar = 20; // Global Variable
  4 
  5 int local( int );
  6 
  7 int main()
  8 {
  9     int localvarmain = 0;
 10 
 11     printf("global variable: %d\n\n", glovar);
 12     printf("Local variable cannot be demonstrated the same way because if the program tries\n");
 13     printf("to access a variable that doesn't exist, the program will not compile correctly\n");
 14     printf("and result in the program not running correctly\n\n");
 15 
 16 // Running local function to add the local variable (to the function) to the global variable
 17 
 18     glovar = local( glovar );
 19 
 20     printf("New global variable: %d\n", glovar);
 21 
 22     return 0;
 23 }
 24 
 25 int local( int glovar )
 26 {
 27     int localvar = 10;
 28 
 29     glovar = glovar + localvar;
 30     return glovar;
 31 }

The printed result:

lab46:~/src/opus/opus2$ ./unixdemo 
global variable: 20

Local variable cannot be demonstrated the same way because if the program tries
to access a variable that doesn't exist, the program will not compile correctly
and result in the program not running correctly

New global variable: 30
opus/fall2012/jcavalu3/discretepart2.txt · Last modified: 2012/10/30 14:25 by jcavalu3