User Tools

Site Tools


opus:spring2012:sswimle1:cprogpart2

cprog Keywords

cprog Keyword 9

Scope (Block, Local, Global, File)

Definition

Global Scope is defined outside of specific functions in the head section of a program and is usually accessible by the entire program.

File Scopes act about the same as Global scopes with the exception that the global scope is accessible by the whole program and the file scope is just accessible in that particular FILE.

Local Scope is defined within a specific function and is usually just relevant to that function and would not be recognized by another function it was not part of.

Block Scope is defined within a specific code block

{
Block Scope
}

Demonstration

Demonstration of the chosen keyword.

int x = 1; // global variable
12
13  int main()
14  {
15     int x = 5; // local variable to main
16
17     cout << "local x in main's outer scope is " << x << endl;
18
19     { // start new scope                                        
20        int x = 7; // hides x in outer scope                     
21                                                                 
22        cout << "local x in main's inner scope is " << x << endl;
23     } // end new scope                                          

cprog Keyword 10

Type Casting

Definition

Type Casting happens when you have a given expression and you convert or assign that expression another type.

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

short a=2000;
int b;
b=a;

cprog Keyword 11

Selection Structures (if, case/switch)

Definition

An If selection structure is dependent on a condition statement, for example if something is true do this OR if something is false do this other thing.

A switch case selection structure is a shorter substitute to long if statements, a value variable is compared and or switched in each of the following cases for the programs structure.

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

switch ( <variable> ) {
case this-value:
  Code to execute if <variable> == this-value
  break;
case that-value:
  Code to execute if <variable> == that-value
  break;
...
default:
  Code to execute if <variable> does not equal the value following any of the cases
  break;
}

cprog Keyword 12

Repetition/Iteration Structures (for, while, do while)

Definition

The repetition structure of a while loop allows for a statements execution to run repeatedly until the condition of that loop has been met.

The do while loop has the same structure as the while loop with the exception that do while loop tests the condition at the end of the loop structure so that the statement will be executed at least once.

The for loop handles all ( variable initialisation; loop condition; variable update ) in the same set of paraenteses.

Demonstration

Demonstration of the chosen keyword.

while LOOP

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    int count = 0;
    while(count < 10)
    {
    printf("%d\n", count);
    ++count;
    }
    return 0;
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

Sample output:

0
1
2
3
4
5
6
7
8
9

cprog Keyword 13

Structures (Declaration, Accessing Elements, Pointers to)

Definition

Structure Declarations are declared by the preset struct then followed by a variable name in c programming.

When accessing elements of a structure you need to call both the name of the struct and the variable name of the struct to access the individual elements.

You can also your Pointers to access elements inside of structs by declaring the pointer and implementing it to a global variable.

Demonstration

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    struct thisclass{
        int something
    }
    return(0);
}

cprog Keyword 14

typedef, enum, union

Definition

While structures define several fields data types a UNION is only used to define one location that may be addressed by different names.

type def gives you the ability to name and define your own variables with the use of a typedef statement.

enumerated data - used for variables that may only contain a set number of values, the variables are then referenced by the name or (tag).

Demonstration

typedef example

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    typedef int day_of_the_week; // Define the type for days of the week
    const int SUNDAY = 0;
    const int MONDAY = 1;
    const int TUESDAY = 2;
    const int WEDNESDAY = 3;
    const int THURSDAY = 4;
    const int FRIDAY = 5;
    const int SATURDAY = 6;
    /* Now to use it */
    day_of_the_week today = TUESDAY;
    return(0);
}

enum example

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    enum day_of_the_week {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY};
    /* Now use it */
    enum day_of_the_week today = TUESDAY;
    return(0);
}

cprog Keyword 15

Functions, Parameters (Pass by: Value, Address, Reference), Return Types, Recursion, Command-line arguments

Definition

Functions are sets of statements that contains three main parts a function header, function body and function prototypes functions can also be called on by a program from outside of that function.

Parameters are values within the functions.

When you pass a parameter by Reference you are changing the value of the parameter when you pass it.

When you pass the parameter by Value you are not changing the parameter just passing it along.

Command-Line Arguments are used for retrieving parameters entered by the user when using your program.

The Return Type is the syntax and definitions of values returned from a function.

Recursion is when a function can call on itself with out an acting outside call.

Demonstration

Functions

int main()
{
   ...
}

Recursion

int factorial(int n)
{
   if(n == 1 || n == 0)
   {
      return 1;
   }
   else
   {
      return n*factorial(n-1);
   }
}

Parameters

int main
{
   fctn(num1, 12);
}
 
void fctn(int arg1, int arg2)
{ ... }

cprog Keyword 16

Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)

Definition

The Compiler is the actual piece of software that turns your code into an executable.

The Preprocessor allows for your programs to include header files, macro expansions and conditional compilation.

Flags are used to optimize how you want to compile the code for example -wall -o etc…

An assembler takes source code to produce machine code.

A Linker takes the compiler's object files and puts them all together so the computer recognizes the files and runs them together as a program.

cprog Objective

cprog Objective

to learn how to become and effective programmer in the languages of c and c++

Definition

syntax

indenting format

compiling

functions

classes

Libraries

Header Files

Pointers

Arrays

etc…

Method

Well from knowing zero day 1 I now know more than zero.

Measurement

followed.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

  • How did you do?

ok.

  • Is there room for improvement?

of course.

  • Could the measurement process be enhanced to be more effective?

not sure.

  • Do you think this enhancement would be efficient to employ?

n/a

  • Could the course objective be altered to be more applicable? How would you alter it?

more beginner focus.

opus/spring2012/sswimle1/cprogpart2.txt · Last modified: 2012/05/04 20:55 by sswimle1