Table of Contents

cprog Keywords

Scope (Block, Local, Global, File)
Pointers (address of, assignment, dereferencing)
Type Casting
Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)
File Access (Read, Write, Append)

Structures (Declaration, Accessing Elements, Pointers to)
typedef, enum, union
Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)
I/O Streams (cin, cout, cerr, stream operators) [C++]
Namespaces [C++]
Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]
Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]
Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]
Overloading (Functions, Operators) [C++]
Exception Handing (throw, try, catch) [C++]
Templates, STL (Standard Template Library) [C++]

cprog Keyword 9

Pointers (address of, assignment, dereferencing)

Definition

Pointers are used to represent a specific address or memory. This is commonly used to store values into variables. Also, pointers can be used as arrays. When a pointer is used, it can allow the program to point to a specific space of memory that can then be used to continue the program for whatever specific purpose it has.

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:

#include<stdio.h>

/* In this file, I will be working with pointers and
learning how to use them, if any edits are necesary, they
will be added. Bare with me! */

int main()
{
        int v=17; // Initializing and assigning a value
        int *p1=NULL;
        printf("v is %u\n", v);
        p1=&v;
        printf("*p1 is %u\n", *p1); //Prints *p1
        *p1=53; //Assigns 53 to what p1 points to
        printf("v is %u\n", v);
        printf("*p1 is %u\n", *p1);
        v=7;
        printf("v is %u\n", v);
        printf("*p1 is %u\n", *p1);
        return(0);
}
lab46:~/src/cprog$ gcc -o pointer pointer.c
lab46:~/src/cprog$ ./pointer
v is 17
*p1 is 17
v is 53
*p1 is 53
v is 7
*p1 is 7
lab46:~/src/cprog$ 

cprog Keyword 10

Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)

Definition

Arrays have the capability of storing multiple values of the same type. Arrays that are made without pointers are created as follows:

float array[5];

float: the data type array: the name of the array [5]: size of the array (when assigning values to the array, never assign to the array value [5], only values [0] through [4] exist in the array)

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:

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
        int array[5];
        int count;
        int fives = 0;
        printf("{ ");
        for(count = 0; count < 5; count++)
        {
                array[count] = fives;
                fives = fives + 5;
                printf("%d", array[count]);
                if( count < 4 )
                        printf(", ");
                else
                        printf(" }\n");
        }
        return(0);
}
lab46:~/src/cprog/Opus/Opus2$ ./arraykeyword
{ 0, 5, 10, 15, 20 }
lab46:~/src/cprog/Opus/Opus2$ 

cprog Keyword 11

File Access (Read, Write, Append)

Definition

The concept of File Access is what allows programs to read, write, or adjust files. This is used for things like reading a file to a program, writing to a file from a program, or adjusting something in a program. Those are just simple examples, there are many more examples of File Access.

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:

#include<stdio.h>
#include<stdlib.h>

int main()
{
        FILE *in, *out;
        char value=0;
        in=fopen("file.txt", "r");
        out=fopen("out.txt", "w");
        if(in == NULL)
        {
                printf("ERROR!\n");
                exit(1);
        }
        fscanf(in, "%hhd", &value);
        while(value!=-1)
        {
                value*=2;
                fprintf(out, "%hhd\n", value);
                fscanf(in, "%hhd", &value);
        }
        fclose(in);
        fclose(out);
        return(0);
}

cprog Keyword 12

I/O Streams (cin, cout, cerr, stream operators) [C++]

Definition

I/O Streams serve the same purpose as Standard I/O. The exception is that I/O Streams are used only in C++. A programmer can still use Standard I/O, only by manipulating the header that you are calling it with.

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:

#include <iostream>
 
using namespace std;
 
int main()
{
        int value = 0;
        cout << "Listen to me! You have to get out of here!\n\nPlease enter a number:\n";
        cin >> value;
        cout << endl << value << endl;
        return(0);
}

Results:

lab46:~/src/cprog/Opus/Opus2$ g++ -o keyword12 keyword12.cc 
lab46:~/src/cprog/Opus/Opus2$ ./keyword12 
Listen to me! You have to get out of here!

Please enter a number:
15

15
lab46:~/src/cprog/Opus/Opus2$

cprog Keyword 13

typedef, enum, union

Definition

 * Typedef allows the programmer to assign a new data type name to variables, usually to make variables name more convenient and organized.
 * Enum is a constant known as an enumeration constant, which represents a list of constant integer values.
 * A union is used to store multiple values of different data types and keep track of them and what is being assigned to them.

Demonstration

A good example of typedefs and unions exists in the program that was created in class called union.c:

#include<stdio.h>
 
int main()
{
        int i;
        union var {
                int x;
                float f;
        }; //This union holds two values of different data types that will be used accordingly.
        typedef union var Uif;
        //Without ^, we would have had to type "union var value;"
        Uif value;
        for(i = 0; i < 24; i++)
        {
                value.x = value.x + rand()%51 + 1;
        }
        printf("total is : %d\n", value.x);
        value.f = 0.0;
        for(i = 0; i < 73; i++)
        {
                value.f = value.f + rand()%27 + 0.1;
        }
        printf("total is: %f\n", value.f);
        return(0);
}

cprog Keyword 14

Type Casting

Definition

Type casting allows the programmer to use a variable, like int, as another type, like short int or char, for one operation.

Demonstration

The following is an example of type casting:

#include<stdio.h>
#include<stdlib.h>
 
void main()
{
        int num;
        num = 67;
        printf("%c\n", num);
}

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

lab46:~/src/cprog/Opus/Opus2$ ./keyword14
C
lab46:~/src/cprog/Opus/Opus2$ 

cprog Keyword 15

Scope (Block, Local, Global, File)

Definition

The scope of a variable determines how the variable can be used. The block scope means that a variable is used in one specific block or statement. A local scope means a variable can be used in a block as well as any other blocks inside of that first one. A global scope means a variable can be used outside of any block. A file scope means a variable is taken from a file or given to a file.

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:

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

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

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

cprog Keyword 16

Overloading (functions, operators) [C++]

Definition

Overloading is when a name is used for multiple functions or operators in a scope. Function overloading means that there are multiple functions with the same name; what makes them different is that each has a different amount or data types of parameters.

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:

#include<iostream>
using namespace std;
 
void print( double valuef ) // The function that prints the float value
{
        cout << "Printing the float value: " << valuef << "\n";
}
 
void print( int valuei ) // The function that prints the integer value
{
        cout << "Printing the integer value: " << valuei << "\n";
}
 
int main()
{
        print(15); // Both functions are called print, just with different para$
        print(15.23);
        return(0);
}

This is the result:

lab46:~/src/cprog/Opus/Opus2$ ./keyword16
Printing the integer value: 15
Printing the float value: 15.23
lab46:~/src/cprog/Opus/Opus2$ 

cprog Objective

cprog Objective

One of the course objectives is to be able to break down and separate code into functions and illustrate the use of parameter passing.

Definition

The main purpose of doing this is to reduce the amount of coding in the main function, to make things neater, and to reduce the chances of error in the program. This is done by taking parts of programs that are repeated often within the main function, and remove this code and put it into a function. Then, the programmer will create a function prototype for the function, and replace the repeated code with the function call.

Method

A good method of showing this is shown in project two. A lot of the program that could be used in the main are, instead, put into separate functions, and then called in the main, making the main function much cleaner, and allowing the user to debug with more ease.

Measurement

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
 
void printary( int, char* );
char* mkArray( int );
char* mkZero( int, char* );
char* Addition( int, char*, char*, char* );
//char* Subtraction( int, char*, char*, char* );
 
int main()
{
        char *array1, *array2, *carry, *result, menuchoice;
        int size, size1, size2, count, i;
        fprintf(stdout, "When you input the numbers, you must add an extra zero at the beginning of the larger number, \
or else the program will not run correctly.\n");
        fprintf(stdout, "Also, you must add in enough zeros at the beginning of the smaller numbers to be the same amount of digits as the \
larger, then add one more.\n");
        fprintf(stdout, "Example: If the first number is 12, and the second is 2634, then the first number must have three zeros in front \
of the actual number: 00012.\nThe second number must then have one zero put in front of it: 02634.\n\n\n");
        fprintf(stdout, "The first number must be the smaller number.\n");
 
        fprintf(stdout, "How many digits is this number: ");
        fscanf(stdin, "%hhu", &size1);
        fprintf(stdout, "\nHow many digits is the second number: ");
        fscanf(stdin, "%hhu", &size2);
 
        if( size1 > size2 )
                size = size1;
        else
                size = size2;
 
        size = size++;
 
// Creating the arrays
 
        array1 = mkArray( size );
        array2 = mkArray( size );
        carry = mkArray( size );
        result = mkArray( size );
 
// Making the array values zero
 
        array1 = mkZero( size, array1 );
        array2 = mkZero( size, array2 );
        carry = mkZero( size, carry );
        result = mkZero( size, result );
 
        printary( size, result );
        printary( size, array1 );
        printary( size, array2 );
        printary( size, carry );
 
        fprintf(stdout, "Please enter the number that you want to be stored: ");
 
        fgetc(stdin);
 
        for(i = 0; i <= size; i++)
        {
                *(array1 + i) = fgetc(stdin) - 48;
        }
        printary( size, array1 );
 
        fprintf(stdout, "\n\n");
 
        fprintf(stdout, "Please enter the number that you want to be stored: ");
 
        for(i = 0; i < size; i++)
        {
                *(array2 + i) = fgetc(stdin) - 48;
        }
        printary( size, array2 );
 
        fprintf(stdout, "\n\n");
 
        fprintf(stdout, "What would you like to do with these numbers? ");
        printf("Choose:\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Divison\nEnter now: ");
 
        fgetc(stdin);
        menuchoice = fgetc(stdin);
        switch( menuchoice )
        {
                case'1':
                        fprintf(stdout, "You have chosen Addition.\n");
                        result = Addition( size, array1, array2, carry );
                        fprintf(stdout, "The result is: ");
                        printary( size, result );
                        fprintf(stdout, "\n");
                        break;
                case'2':
                        fprintf(stdout, "You have chosen Subtraction.\n");
                        break;
        }
        return(0);
}
 
void printary( int size, char *array )
{
        char count;
        for( count = 0; count < size; count++ )
        {
                printf("%hhu", *(array + count));
        }
        printf("\n");
}
 
char * mkArray( int size )
{
        char *array;
        array = (char *) malloc (sizeof(char) * size);
        return array;
}
 
char * mkZero( int size, char *array )
{
        char count = 0;
        while( count <= size )
        {
                *( array + count ) = 0;
        }
        return array;
}
<
char * Addition( int size, char *array1, char *array2, char *carry )
{
        int count;
 
        int answer = 0;
        char *addition;
        addition = (char *) malloc (sizeof(char) * ( size - 1 ) );
 
        for( count = size; count >= 0; count-- )
        {
                answer = (*(array1 + count) + *(array2 + count) + *(carry + count));
                if(answer > 9)
                {
                        answer = answer - 10;
                        *(carry + (count - 1)) = 1;
                }
                *(addition + count) = answer;
        }
        return addition;
}

Analysis

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