Table of Contents

Part 1

Entries

Entry 1: February 7, 2012

Entry 2: February 10, 2012

Entry 3: February 9, 2012

Entry 4: February 16, 2012

Keywords

cprog Keywords

Standard I/O (STDIO, STDOUT, STDERR)
Header Files (Local and System), C Standard Library (Libc), Libraries
arithmetic (equations, operators)
logic and operators (and, or, not, xor)
Variables (types, ranges, sizes)

Scope (Block, Local, Global, File)
Pointers (address of, assignment, dereferencing)
Type Casting
Selection Structures (if, case/switch)
Repetition/Iteration Structures (for, while, do while)

Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)
File Access (Read, Write, Append)
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++]

cprog Keyword 1

Header Files

Definition

A header file is used to allow the programmer to use specific functions. It is used by preceding it with '#include'. (Ex. #include<stdio.h>)

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()
{
    printf("This is an example of a header file - stdio.h")
 
    return(0);
}

cprog Keyword 2

Standard I/O

Definition

Standard I/O stands for Standard Input/Output. When in a program, it allows the user to input whatever they need to put into the program, and it allows the program to show text, which can say whatever the programmer wants.

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 here.
 */
#include <stdio.h>
 
int main()
{
    unsigned char randomvar;
    printf("This is the output code - printf. It outputs whatever the programmer wants through text.");
    scanf("%hhu", &randomvar); /* Allows the user to input whatever data
                                * that is necessary.
                                * /
    return(0);
}

cprog Keyword 3

Repetition/Iteration Structures

Definition

Repetition/Iteration Structures consist of for, while, and do-while loops. Each of these loops are often used to fill arrays, accept values and keep asking if the value isn't correct, to accept multiple values, to print multiple values, etc.

Demonstration

Demonstration of Repetition/Iteration Structures

/*
 * Sample code block
 */
 
#include<stdio.h>
#include<stdlib.h>
 
int main()
{
        char *value, i; //Declaring two integers: the pointer "value"
                        //and i as a char
        value = (char*)malloc(sizeof(char)*4); //setting aside 4 bits of memory
                                               //and returning a pointer
        *(value+0) = 0; //assigning the pointer for the first space of allocated$
        *(value+1) = 1; //assigning pointer for the second space of allocated me$
        fprintf(stdout, "Please enter a value (-128-+127):");
        fscanf(stdin, "%hhd", &i);
        *(value+2) = i; //assigns pointer for the third space of allocated memory
                        //gives that memory the value that it is assigned
        *(value+3) = (2*i)+3; //assigns pointer for the fourth space of allocated memory
                              //gives that memory the value that is "calculated"
        for(i=3; i>=0; i--)
        {
                printf("%hhd\n", *(value+i));
        }
        return(0);
}
 
/* What the file is doing is showing how arrays work.
 
        The first pointer assigns 0 to the first space (*(value+0) = 0)
        The second pointer assigns 1 to the second space (*(value+1) = 1)
        The third pointer assigns whatever the user input as i to the                 third space etc, etc.
*/
 
    return(0);
}

cprog Keyword 4

Variables (Type, Ranges, and Sizes)

Definition

Variables are the part of the code that take assignments like numbers or memory. There are many types of variables with different sizes.

Demonstration

Demonstration of the chosen keyword.

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
 
#include <stdio.h>
#include <math.h>
 
int main()
{
        // Signed Variables
        signed char sc = 0;
        signed short int ssint = 0;
        signed int sint = 0;
        signed long int slint = 0;
        signed long long int sllint = 0;
 
        // Unsigned Variables
        unsigned char uc = 0;
        unsigned short int usint = 0;
        unsigned int uint = 0;
        unsigned long int ulint = 0;
        unsigned long long int ullint = 0;
 
        // Junk Variable
        unsigned long long int quantity = 0;
 
    return(0);
}

cprog Keyword 5

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

Definition

Logic gates are used in if blocks, loops, switch blocks, and other functions. The purpose of these logic gates is to take two or more input statements and see if the output is true or false, and perform a specific task, in most cases, with that specific output. Operators are the signs that do a specific task, like add “+”, subtract “-”, etc.

Demonstration

* A few examples of operators are greater than “>”, less than “<”, is equal to “==”, etc. * A few examples of logic gates are AND “&&”, OR “||”, NOT “!=”, etc.

/*
 * Sample code block
 */
 
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        char *word, len = 0, x, pos = 0;
        word = (char *) malloc(sizeof(char)*24);
        fprintf(stdout, "Please enter a word: ");
        fscanf(stdin, "%s", word);
        printf("Debug A\n");
        x = *(word + pos);
        while((x!='\0')&&(x != '\n')) // Specifically the "&&" and the "!="
        {
                printf("COME ON! x is %hhd\n", x);
                printf("pos is %hhd\n", pos);
                len++;
                pos++;
                x = *(word + pos);
        }
        printf("Debug B\n");
        for(pos = len; pos > -1; pos--)
        {
                fprintf(stdout, "%c", *(word + pos));
        }
        fprintf(stdout, "\n");
        printf("Debug C\n");
        return(0);
}

cprog Keyword 6

Functions, Parameters

Definition

Functions are specific blocks of code that were either created with the standard library of code or are created separately in each program. The purpose of these functions vary, but the main idea of functions is that they are created to perform a certain operation that can be used to further the process of the program that it is used in. The parameters are the variables that are called in the function, and are brought in through the function call.

Demonstration

Demonstration of the chosen keyword.

/*
 *
 * Sample code block for functions and parameters.
 *
 */
#include<stdio.h>
 
int function( int prntnum ) //I cleverly labeled the function of this program "function" and the parameter "prntnum".
{
        char i;
        for(i=0; i < 10; i++)
        {
                prntnum++;
        }
        return(prntnum);
}
 
int main()
{
        int parameter = 0;
        printf("The value of the original parameter is: %d\n", parameter);
        printf("The new value of the parameter is: %d\n", function( parameter ));
        return(0);
}

Results:

lab46:~/src/cprog/Opus/Opus1$ ./Keyword6
The value of the original parameter is: 0
The new value of the parameter is: 10
lab46:~/src/cprog/Opus/Opus1$ 

cprog Keyword 7

Selection Structures (if, case/switch)

Definition

Selection Structures are used to decide if a program will continue or which path it will take. If blocks are used with '!=' (not equal to), '&&' (and), '||' (or), etc. Case/Switch blocks are used a great amount of the time as “menus” that the user can choose from.

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 for Selection Structures
 */      
#include<stdio.h>
#include<stdlib.h>
 
int main()
{
        int *input, *pick;  input=pick=NULL;
        input=(int *)malloc(sizeof(int)*1);
        pick=(int *)malloc(sizeof(int)*1);
        *input=0;
        *pick=0;
        while(*pick != -1)
        {
                srand(time(NULL));
                *pick=rand()%99+1;
                printf("Guess the computer's pick: ");
                scanf("%d", input);
                if(*input==*pick) //This is the first if block
                {
                        printf("Wow! You got $it right!!!!\n");
                }
                else if(*input > *pick)
                {
                        printf("Too low!!!\n");
                }
                else
                {
                        printf("Too high!!\n");
                }
                printf("Computer had %d\n", *pick);
                printf("0 to go again, -1 to quit\n");
                scanf("%d", pick);
        }
        return(0);
}

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

cprog Keyword 8

Arithmetic (Equations, Operators)

Definition

Arithmetic is used to perform operations that the computer has to run so it can run programs. Equations are solved to find a result that the user is looking for. Operators are used in equations, examples are +, -, /, and *.

Demonstration

Demonstration of the chosen keyword.

/*
 * Sample code block
 */
#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; // This is an equation, which means "value = value * 2", the operator being *
                fprintf(out, "%hhd\n", value);
                fscanf(in, "%hhd", &value);
        }
        fclose(in);
        fclose(out);
        return(0);
}

cprog Objective

cprog Objective

The objective of the course is to learn C/C++ Programming language and use that to learn about computers and programming more. Also, programming in general is important.

Definition

This includes learning about anything from arrays(blocks of memory to store similar variables) to variables. All that will be taught to me is important in taking my next step into the beautiful world of programming and technology!

Method

I will be using the books I have purchased, my professor, and other students as resources to help myself grasp the concept of computer programming and all that it controls.

Measurement

On a scale of Red to Blue, I would say I am at a well… A well of knowledge and understanding! No, but seriously, I understand most of the concepts. The trouble I have is that:

1. I procrastinate 2. Putting together my own code from scratch becomes a little troublesome, especially since I learned mostly C++ and then jumped into this new atmosphere of C, with the compiler that comes with Ubuntu and Linux.

It is all a little confusing at times, but I feel that I will get the hang of it.

Analysis

I'm still quite fuzzy on how I am to fill this out, but I am going to try my best to answer these correctly.

  • I think I am doing alright.
  • There is always room for improvement, but specifically I just need to understand pointers and addresses a little more.
  • I believe that it could have been, but that is a job for the next Opus!
  • I do believe that an enhancement would be very important; it would allow me to further test my abilities in C.
  • It could be altered by including more that I will learn and need to learn to make myself better at programming and understanding computers.

I apologize if this is completely incorrect. I tried to fill it out to the best of my ability. Unfortunately, my abilities are lacking at the moment because it is very late for me and once it gets late, my brain flies out the window, and it takes me a little while (and some quality sleep) to get it back and functional!

Experiments

Experiment 1

Question

What would be printed if I replace the %u with the $d for an unsigned integer?

Resources

According to classes being taken, %d represents a signed integer, while %u represents an unsigned integer. The range for a signed integer data type is -2,147,483,647 to 2,147,483,646, while the range for an unsigned integer data type is 0 to 4,294,967,295.

Hypothesis

I believe that the printed value will be the same ONLY if it is within the range of the signed integer data type. Ex. If the user inputs a value between 0 and 2,147,483,647, then it should print out fine, but if the value is greater than that range, then the value will “roll over” to the next integer in the signed integer data type, which would make the value a negative number.

Due to the data type only covering positive numbers (unsigned integer), it can go to larger numbers in the 4 bytes it is given for memory, but if it is printed as a signed integer, then it will only be able to print out half of the positive integers that it could as an unsigned integer, so it will go back over into the negative numbers until it satisfies the amount of digits it has.

Experiment

To run this experiment, I am going to use the Lab46 terminal and create a program that will accept a value ranging from 0 to 4,294,967,295, which will be accepted as a unsigned integer. From there, the program will print the value back out as a signed integer using the %d command.

Data

This is the code that I used:

#include<stdio.h>

int main()
{
        unsigned int integer;

        printf("Please input any integer between 0 and 4,294,967,295: ");
        scanf("%u\n", &integer);
        printf("The number you put into the program is: %d\n\n", integer);
        return(0);
}

This is the result:

lab46:~/src/cprog$ ./experiment1
Please input any integer between 0 and 4,294,967,295: 3015489756
1
The number you put into the program is: -1279477540

lab46:~/src/cprog$ 

Analysis

Based on the data collected:

Conclusions

When using an unsigned data type, it is possible to print that number as a signed data type, the only problem is that if it is larger than the range given, then it will just “roll over” until the entire number has been stored.

Experiment 2

Question

What would result from manipulating the “firstarray.c” program and replacing the “*4” with a “*3” and nothing else?

Resources

According to what I have learned so far, the purpose of the “*4” along with the function malloc is to allocate memory that the program can then store whatever it needs to in. This is one way to create an array. The “*4” in this program specifically multiplies the size of the char data type, which is one bit, and multiplies that by 4, leaving 4 bits of space that the program can store data in.

Hypothesis

My best guess would be that the array will only have three char-sized “blocks” of memory that are available for storage. This will cause the fourth assignment in the array (*(array + 3)) to not be assigned to any block of memory in the array. This shouldn't cause any problems with the compiler, the program should run fine, still.

Experiment

To perform this experiment, I have used a program from class and manipulated it, replacing the 4 with a 3 when sizing the array. The program then prints out what is left, and I believe that the program will print out a bunch of randomness when it gets to the fourth block of memory that was supposed to be created.

Data

This is the program, minus the documentation:

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

int main()
{
        char *value, i; //Declaring two integers: the pointer "value"
                        //and i as a char
        value = (char*)malloc(sizeof(char)*3);
                                              
        *(value+0) = 0;
        *(value+1) = 1;
        fprintf(stdout, "Please enter a value (-128-+127):");
        fscanf(stdin, "%hhd", &i);
        *(value+2) = i;
                        
        *(value+3) = (2*i)+3;
                              
        for(i=3; i>=0; i--)
        {
                printf("%hhd\n", *(value+i));
        }
        return(0);
}

These are the results of the program:

lab46:~/src/cprog/Opus/Opus1$ ./experiment2
Please enter a value (-128-+127):15
33
15
1
0
lab46:~/src/cprog/Opus/Opus1$ ./experiment2
Please enter a value (-128-+127):458
-105
-54
1
0
lab46:~/src/cprog/Opus/Opus1$ ./experiment2
Please enter a value (-128-+127):12
27
12
1
0
lab46:~/src/cprog/Opus/Opus1$ 

Analysis

Based on the data collected:

Conclusions

Through this experiment, I realized that there is more to arrays and memory allocation than I thought. There is a chance that variables may be stored in buffer memory when making an array, and that is what happened in this experiment. I also learned that I need to read up on my arrays.

Experiment 3

Question

Is it possible to multiply things in an array and store the result in the array?

Resources

From what I have learned about arrays, arrays are capable of a lot of different functions, like storing related variables, and either reading them out or using them for another purpose. So far, arrays have been used to store values and re-arrange them, read them back out, to take variables from one function and use them in another, and many other purposes.

Hypothesis

I believe that this is possible, because arrays store similar variables, even if they are combined in some way and stored back into it.

Experiment

I will make a program that creates an array that will allow the user to store two integer numbers between one and one hundred into the array. From there, the program will call those two numbers from it, and multiply the two numbers together, then storing the third in the array. The array will then be printed.

Data

The program for the experiment:

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

int main()
{
        int storage1 = 0;
        int storage2 = 0;
        int *array;
        array = (int*)malloc(sizeof(int)*3);

        fprintf(stdout, "Please enter two numbers between 1 and 100:\n");
        fscanf(stdin, "%u", &storage1);
        fscanf(stdin, "%u", &storage2);
        *(array) = storage1;
        *(array+1) = storage2;
        *(array+2) = (*(array+1)) * (*(array));
        fprintf(stdout, "These are the numbers stored in the array:\n%u\n%u\n%u$
        return(0);
}

The results:

<cli> lab46:~/src/cprog/Opus/Opus1$ ./experiment3 Please enter two numbers between 1 and 100: 10 15 These are the numbers stored in the array: 10 15 150 lab46:~/src/cprog/Opus/Opus1$ ./experiment3 Please enter two numbers between 1 and 100: 100 100 These are the numbers stored in the array: 100 100 10000 lab46:~/src/cprog/Opus/Opus1$

Analysis

Based on the data collected:

Conclusions

Arrays can be very useful when it comes to storing numbers and variables that you may need later or in other functions. An array could be created to take multiple numbers and use these numbers to show different operations and what the result of these operations would be.