User Tools

Site Tools


opus:spring2012:rmatsch:cprogpart1

Cprog Keywords

Standard I/O (STDIO, STDOUT, STDERR)

■ Header Files (Local and System), C Standard Library (Libc), Libraries……X

■ arithmetic (equations, operators)…..X

■ logic and operators (and, or, not, xor)

■ Variables (types, ranges, sizes)…..X

■ Scope (Block, Local, Global, File)

■ Pointers (address of, assignment, dereferencing)…..X

■ Type Casting

■ Selection Structures (if, case/switch)……X

■ Repetition/Iteration Structures (for, while, do while)….X

■ Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)…..X

■ File Access (Read, Write, Append)…..X

■ Structures (Declaration, Accessing Elements, Pointers to)

■ typedef, enum, union

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

■ 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++]

Variables

Definition

Variables are storage containers which can be permanent but most of the time they vary or change throught a computer program.

Demonstration

Variables have 3 basic properties.First is name, in order to use a variable you must have declare a name for the variable to call it by. secondly you must select a data type such as type “interger” or “char”. Thirdly is the size of this container you want the variable to be. Thankfully this is already determined by the data type you have established so the work is already done. The data type selected can hold a certain amount of data. Below is code for a program to show you the different sizes of a data types there ranges the unique values they can have.I must also mention that upon selection of a data type there are signed and unsigned variables also. Signed variables can handle negative along with positive values and unsigned variables can handle only positive values.

Code :

#include <stdio.h>
#include <math.h>
int main()
{
        // declare variables
        unsigned char uc = 0;
        signed char sc = 0;
        signed short int ssi = 0;
        unsigned short int usi =0;
        signed long int sli = 0;
        unsigned long int uli =0;
        signed long long int slli = 0;
        unsigned long long int ulli = 0;
        signed int si  = 0;
        unsigned int ui = 0;
        unsigned long long int quantity = 0;
 
        //display info for unsigned char data type
        printf("Unsigned char is %u bytes\n", sizeof(uc));
        printf("The range of an unsigned char is %hhu to %hhu\n", uc, (uc-1));
        quantity=(unsigned char)(uc-1) + 1;/* unsigned char -1 = 255 +1 for all unique values*/
         printf("An unsigned char can store %llu unique values\n\n", quantity);
        //display info for signed char data type
        printf("Signed char is %d bytes\n", sizeof(sc));
        quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); /*2 raised to the power of 8 times data type in  bytes */
        printf("The range of a signed char is %hhd to %hhd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
         printf("A unsigned char can store %llu unique values\n\n", quantity);
 
        printf("Unsigned short int is %u bytes\n", sizeof(usi));
        quantity = (unsigned long long int)pow(2, (sizeof(usi)*8));
         printf("The range of a unsigned short int is %u to %llu\n", usi, ((quantity)-1));
         printf("An unsigned short int can store %llu unique values\n\n", quantity);
 
        printf("Signed short int is %d bytes\n", sizeof(ssi));
        quantity = (unsigned long long int)pow(2, (sizeof(ssi)*8));
       printf("The range of a signed short int is %lld to %lld\n", (ssi-(quantity/2)), (ssi+(quantity/2)-1));
        printf("A signed short int can store %llu unique values\n\n", quantity);
 
 printf("Signed int is %d bytes\n", sizeof(si));
        quantity = (unsigned long long int)pow(2, (sizeof(si)*8));
         printf("The range of a signed int is %lld to %lld\n", (si-(quantity/2)), (si+(quantity/2)-1));
        printf("A signed int can store %llu unique values\n\n", quantity);
 
        printf("Unsigned int is %u bytes\n", sizeof(ui));
        quantity = (unsigned long long int)pow(2, (sizeof(ui)*8));
         printf("The range of a unsigned int is %llu to %llu\n", ui,((quantity)-1));
        printf("An unsigned int can store %llu unique values\n\n", quantity);
 
        printf("Signed long int is %d bytes\n", sizeof(sli));
        quantity = (unsigned long long int)pow(2, (sizeof(sli)*8));
         printf("The range of a signed long int is %lld to %lld\n", (sli-(quantity/2)), (sli+(quantity/2)-1));
        printf("A signed long int can store %llu unique values\n\n", quantity);
 
        printf("Unsigned long int is %u bytes\n", sizeof(uli));
        quantity = (unsigned long long int)pow(2, (sizeof(uli)*8));
         printf("The range of a unsigned long long int is %llu to %llu\n", uli,((quantity)-1));
        printf("An unsigned long int can store %llu unique values\n\n", quantity);
 
 
         printf("Signed long long int is %d bytes\n", sizeof(slli));
        quantity = (unsigned long long int)pow(2, (sizeof(slli)*8));
        printf("The range of a signed long long int is %lld to %lld\n", (slli-(quantity/2)), (slli+(quantity/2)-1));
        printf("A signed long long int can store %llu unique values\n\n", quantity);
 
        printf("Unsigned long long int is %u bytes\n", sizeof(ulli));
        quantity = (unsigned long long int)pow(2, (sizeof(ulli)*8));
         printf("The range of a unsigned long long int is %llu to %llu\n", ulli,((quantity)-1));
        printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 
 
return(0);
}

What command line looks like when program is run.

lab46:~/src/cprog$ ./project0
Unsigned char is 1 bytes
The range of an unsigned char is 0 to 255
An unsigned char can store 256 unique values

Signed char is 1 bytes
The range of a signed char is -128 to 127
A unsigned char can store 256 unique values

Unsigned short int is 2 bytes
The range of a unsigned short int is 0 to 65535
An unsigned short int can store 65536 unique values

Signed short int is 2 bytes
The range of a signed short int is -32768 to 32767
A signed short int can store 65536 unique values

Signed int is 4 bytes
The range of a signed int is -2147483648 to 2147483647
A signed int can store 4294967296 unique values

Unsigned int is 4 bytes
The range of a unsigned int is 0 to 4294967295
An unsigned int can store 4294967296 unique values

Signed long int is 8 bytes
The range of a signed long int is -9223372036854775807 to 9223372036854775806
A signed long int can store 18446744073709551615 unique values

Unsigned long int is 8 bytes
The range of a unsigned long long int is 0 to 18446744073709551614
An unsigned long int can store 18446744073709551615 unique values

Signed long long int is 8 bytes
The range of a signed long long int is -9223372036854775807 to 9223372036854775806
A signed long long int can store 18446744073709551615 unique values

Unsigned long long int is 8 bytes
The range of a unsigned long long int is 0 to 18446744073709551614
An unsigned long long int can store 18446744073709551615 unique values

Pointers

Definition

variables are seen as memory cells that can be accessed using their identifiers (aka pointers). Because we did not have to care about the physical location of our data within memory, we can simply used its identifier when we need to refer to the variable.

Demonstration

Below is code to help understand what pointers acually do.

#include <stdio.h>
int main()
{
        // a is initiallly set to zero
        int a = 0;
        int * b; /* b is declared as a pointer variable by star b */
        b = &a; /* b is assigned the adress of a */
        *b = 12; /* what ever b is pointing at is now set equal to 12 not b itself */
 
        printf("a contains %u \n", a );
 
        printf("a's address is 0x%x\n", &a );
 
        printf("b contains %u \n", *b);
      // b contains the value stored at what b is pointing to. (the container or another variable)
        printf("b points to 0x%x\n", b);
 
        printf("b's addres is 0x%x\n", &b);
 
        return(0);
}
lab46:~/src/cprog$ ./var2
a contains 12
a's address is 0xe5a3f33c
b contains 12
b points to 0xe5a3f33c
b's addres is 0xe5a3f330

arithmetic

Definition

+ Adding binary operator, adds two operands to get a sum, (addend +adend = sum
- Subtract binary operator, inverse of adding, (minuend -subtrahend = difference)
% Modulus takesremainder after an integer division
++ means +1
-- means -1
* Multiply binary operator (multiplicand times multiplier = product)
/ Divide binary operator(dividen / divisior = quotient)

Demonstration

c=5
b=3
p=5
q=5

c-b=a = 5-3=2 a =2 
c+b=a = 5+3=8 a =8
c*b=a = 5*3=15 a =15
x=p/q = 5/5=1 a =1

c++ means c= to 5 plus one =6

cprog Keyword 4

Header files

Definition

Header Files are files that are incoporated in c code to allow you added funtionality such as use of funtions or expressions.

Demonstration

#include <stdio.h> #include <stdlib.h> code to demonstrate

#include <stdlib.h>
#include <stdio.h>
 
int main()
{
    return(0);
}

cprog Keyword 5

Arrays

Definition

Arrays can be thought of as sequentialy ordered containers. The ability to take a big piece of data such as string and break it up into individual pieces to be manipulted as needed. because arrays store data in “numbered containers” arrays are a good thing to know.

Demonstration

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
        unsigned char i;
 
        if (argc<2)
        {
                printf("%8s must be run with 1 or \
        more arguments, you only provide %hhu\n", *(argv+0),(argc-1));
                exit(1);
        }
        printf("pleas enter a message to be ciphered")
         for(i=0;i<argc; i++)
        {
                printf("argv[%hhu]: %s \n",i,*(argv+i));
        }
        return(0);
}
./acs h e y t h i s i s a n a r r a y
you ran this program with 17 arguments, they are:
argv[0]: ./acs
argv[1]: h
argv[2]: e
argv[3]: y
argv[4]: t
argv[5]: h
argv[6]: i
argv[7]: s
argv[8]: i
argv[9]: s
argv[10]: a
argv[11]: n
argv[12]: a
argv[13]: r
argv[14]: r
argv[15]: a
argv[16]: y

multidemensional array would look similar to this

   0 1 2 3
0  y r e d
1  a b h o 
2  f k z p 
3  t u m l

argv[1][2]:k

cprog Keyword 6

Selection Structures (if, case/switch)

Definition

selection structure is a order of sequence of check statments (conditional statements) that are true or false. If the stament is true then do somthing.

Demonstration

 if (in == NULL){
                fputs("please enter a message to cipher:\n", stdout);
                fgets(msg, sizeof msg, stdin);
                fprintf(nf, "%s", msg);
                fclose(nf);
                nf = fopen("nofile.txt", "r");
                letter1 = fscanf(nf,"%c", &letter);
        }else{
                 letter1 = fscanf(in, "%c", &letter);
 
        }if (key == NULL){
                printf("please enter a key to encipher with :\n");
                scanf("%d", &keyvalue);
        }else{
                fscanf(key,"%d",&keyvalue);

cprog Keyword 7

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

Definition

Repetitive/iteration structures or loops are a statement which allows code to be repeatedly executed until a condition is meet or can be instructed to “do” something while this condition is true.

Demonstration

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
        unsigned char i;
 
        if (argc<2)
        {
                printf("%8s must be run with 1 or \
        more arguments, you only provide %hhu\n", *(argv+0),(argc-1));
                exit(1);
        }
        printf("you ran this program with %hhu arguments, they are:\n", argc);
         for(i=0;i<argc; i++)/* Condition to be checked */
        {
                printf("argv[%hhu]: %s \n",i,*(argv+i));
        }
        return(0);
}

another example

 while (letter1 != EOF)
        {
                if((letter >= 'A') && (letter <= 'Z'))
                        letter = (((abs(letter - 65) + keyvalue)%26)+65);
                else if((letter >= 'a') && (letter <= 'z'))
                        letter = (((abs(letter - 97) + keyvalue)%26)+97);

                fprintf(out,"%c",letter);
                if (in == NULL){
                        letter1 = fscanf(nf, "%c", &letter);
                }else{
                        letter1 = fscanf(in, "%c", &letter);
                }

cprog Keyword 8

File access

Definition

File Access is when you read from, write to, or append to a file. the program can read write or append 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:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main(){
        char letter1=0;
        char msg[250];
        FILE *in, *out, *key,*nf;
        char letter = 0;
        int keyvalue;
 
        nf = fopen("nofile.txt", "w");
        in = fopen("message.txt", "r");
        out = fopen("cipher.txt", "w");
        key = fopen("key.txt","r");
 
        if (in == NULL){
                fputs("please enter a message to cipher:\n", stdout);
                fgets(msg, sizeof msg, stdin);
                fprintf(nf, "%s", msg);
                fclose(nf);
                nf = fopen("nofile.txt", "r");
                letter1 = fscanf(nf,"%c", &letter);
        }else{
                 letter1 = fscanf(in, "%c", &letter);
 
        }if (key == NULL){
                printf("please enter a key to encipher with :\n");
                scanf("%d", &keyvalue);
        }else{
                fscanf(key,"%d",&keyvalue);
        }
        while (letter1 != EOF)
        {
                if((letter >= 'A') && (letter <= 'Z'))
                        letter = (((abs(letter - 65) + keyvalue)%26)+65);
                else if((letter >= 'a') && (letter <= 'z'))
                        letter = (((abs(letter - 97) + keyvalue)%26)+97);
  fprintf(out,"%c",letter);
                if (in == NULL){
                        letter1 = fscanf(nf, "%c", &letter);
                }else{
                        letter1 = fscanf(in, "%c", &letter);
                }
 
        }if (in == NULL){
                fclose(nf);
        }else{
                fclose(in);
        }
        if (key != NULL){
                fclose(key);
        }
 
 
        //fclose(in)
        fclose(out);
        //fclose(nf);
        return(0);
}
lab46:~/src/cprog$ cat cipher.txt
jpa
lab46:~/src/cprog$

lab46:~/src/cprog$ cat plain.txt
hey
lab46:~/src/cprog$

cprog Objective

cprog Objective

The course objective is for students to be able to write, compile code, use pointers and variables effectively and efficiently. Also this course is designed for students to gain a basic comprehension of memory such as various data types. Also obtain valuable debugging skills and demonstrate the ability to use logic and structure to solve problems. Develop solutions shown in c++ program language.

Method

To measure successful academic/intellectual achievement of this objective. students should be given a final project that incorporates various lessons learned through the course because an important part of intellectual achievement is applying what you leaned. Students should also be asked to write various programs on the spot and should be completed given sufficient time.

Measurement

Correctness - are things displayed when they are supposed to, were variables initialized etc. Design/efficiency- there is many ways to design a program but there may be better ways or more efficient ways. (Are the right size variables used?)

Logic- has a basic concept of logic (uses loop instead of 100 if statements) Other- Does the program do what it was designed to do and accurately.

Analysis

Reflecting upon my results of the measurement to ascertain achievement of the course objective.

* How did you do? I did not do to bad but still have a lot to learn. * Is there room for improvement?yes * Could the measurement process be enhanced to be more effective? Yes * Do you think this enhancement would be efficient to employ? Yes * Could the course objective be altered to be more applicable? No

Reflecting on the measurements I did not do too bad, but of course have a lot to learn so there is much room for improvement.

opus/spring2012/rmatsch/cprogpart1.txt · Last modified: 2012/04/01 21:57 by rmatsch