Table of Contents

Part 2

Entries

October 5, 2011

October 5, 2011

October 14, 2011

October 31, 2011

cprog Topics

Keyword 1: Selection Structures

Example code:

#include <stdio.h>
 
int main()
{
	int number;
 
	printf("Please enter a number between 1 and 10: ");
	scanf("%d", &number);
 
	//here is the selection statement
	if (number < 5 && number >= 1)
	{
		printf("You entered a number between 1 and 4\n");
	}
	else if (number >=5 && number <=10)
	{
		printf("\nYou entered a number between 5 and 10\n");
	}
	else
	{
		printf("\nYou entered an invalid number.\n");
	}
 
	return 0;
 
}

Keyword 2:Repetition/iteration structures

Example code:

#include <stdio.h>
 
int main()
{
	int number = 10;
	int x;
	printf("\n");
	for(x = 1; x <= number; x++)
	{
		printf("%d\n", x);
	}
 
 
	return 0;
 
}

Keyword 3: Multi-Dimensional arrays

Keyword 4:File access

File access was covered in Sys prog.

Keyword 5:typedef, enum, union

Keyword 6: Functions

Keyword 7: Parameters

Keyword 8: return types

Keyword 9: Recursion

Keyword 10: Command line arguments

Keyword 11: Code stages(source,object, Binary)

Keyword 12: Compiler, preprocessor, flags, assembler, linker

data Topics

Keyword 1: Doubly Linked Lists

Doubled linked lists are not much different than linked lists except now each node not only points to the next node but to the previous as well.

Keyword 2: Stacks(array and linked list based)

A stack is a last in first out data structure, meaning the “top” of the stack is all you have immediate access to. You can store data on top of the stack, remove it, or view it.

Keyword 3: Pushing

Pushing is when you add to the top of a stack or initializes the stack if it is empty

Keyword 4: Popping

Popping simply removes the data stored at the top of the stack.

Keyword 5: Top(stack top, peek, whatever you wish to call it)

Top typically simply returns the value at the top of the stack but does not delete it like pop does.

Keyword 6: Overflow Condition

Overflow is when a stack is full and cannot accept anymore data

Keyword 7: Underflow condition

Underflow is when the stack is empty and there is no data to be removed

Keyword 8: LIFO or FIFO

LIFO means last in first out. Whatever the last thing that was stored will be the first one out. As stated before stacks are LIFO

FIFO means first in first out. Whatever the first thing that was stored will be the first one out. Queues(which will be discusses later) are FIFO

Keyword 9: Queues(Array and linked list based)

A queue is a first in first out data structure. Where with a stack you could only add or remove from the top, with a queue you add to the top(or back, depending on if you like to envision a queue as a sideways stack) but you remove from the bottom(front).

Keyword 10: Enqueuing

Enqueuing is a queues version of pushing. You simply add new data to the “end” of whatever type of queue you are using.

Keyword 11

Dequeuing is the process of removing the FIRST thing that was stored in your queue, unlike a stack where the last thing stored is removed.

Keyword 12:Overrun and Underrun conditions

Overrun(queue overflow) occurs when you try to add data onto a queue that is full Underrun(queue underflow occurs when you trying to remove data from a queue that is empty

hpc1 Topics

Keyword 1

Identification and definition 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);
}

Keyword 2

Identification and definition of the chosen keyword.

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$ 

Keyword 3

Identification and definition of the chosen keyword. Substitute “keyword” with the actual 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);
}

Keyword 4

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

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$ 

Keyword 5

Identification and definition of the chosen keyword. Substitute “keyword” with the actual 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);
}

Keyword 6

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

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$ 

Keyword 7

Identification and definition of the chosen keyword. Substitute “keyword” with the actual 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);
}

Keyword 8

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

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$ 

Keyword 9

Identification and definition of the chosen keyword. Substitute “keyword” with the actual 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);
}

Keyword 10

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

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$ 

Keyword 11

Identification and definition of the chosen keyword. Substitute “keyword” with the actual 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);
}

Keyword 12

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

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$ 

sysprog Topics

Keyword 1: Device files

Everything in unix is a file and that includes any device on the system. Depending on the device, device files can be written to and read from just like any other file. The monitor device file can be written to but not read from. A keyboard can be read from but not written to.

Keyword 2: Race condition

A race condition is when multiple processes are able to access shared data and they try to change it at the same time. It is called a race condition because the result is depending on the timing of events. page 149 of understanding unix/linux programming has a good example.

Keyword 3:Atomic operations

An atomic operation is when the kernel joins two or more system calls into one indivisible unit. Meaning the entire sequence of calls has to be performed or none at all. Page 150 of understanding unix/linux programming talks briefly about it.

Keyword 4: Streams

The STREAMS model of data flow and connection attributes allows for modularity of processing. You can write or remove modules as you see fit.

Keyword 5: Blocking vs. Non-Blocking

Blocking is when a call waits for input. If there is no input then the program waits for it. Non-Blocking is when when blocking is turned off thus if there is no input the program will still continue.

Keyword 6: Signals

A Signal can be defined as a one word message sent by the kernel. Each signal has a numerical code. Requests for signalcs can come from processes, exceptions and user keystrokes. Many signals cause processes to die, but there are other types of signals. You can typically find the list of signals in /usr/include/signal.h

Keyword 7: Event driven programming

Keyword 8: Alarms, interval timers

Keyword 9: reentrant code, critical sections

Keyword 10: Asynchronous i/o

Keyword 11: Programs and Processes

Keyword 12:Parent/Child processes

cprog Objective

Objective: structures vs. classes

What is the difference between a structure and a class?

data Objective

Objective: data structs in memory

How are data structures allocated and used in memory?

Just like any variable type data structures are given a memory address. Data structures typically have nodes and each node has its own address, this is done in C with the malloc() function.

hpc1 Objective

Objective

State the course objective; define what that objective entails.

Method

State the method you will use for measuring successful academic/intellectual achievement of this objective.

Measurement

Follow your method and obtain a measurement. Document the results here.

Analysis

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

sysprog Objective

Objective: signals

Design a program that handles signals.

#include <signal.h>
#include <stdio.h>
 
void intu();
void seg();
 
 
int main()
{
	char *array;
	signal(SIGINT, intu); //interrupt aka ctrl c
	signal(SIGSEGV, seg);// good ol seg fault why is it V instead of F?
	getchar();
	printf("%c\n", *array);// why does this continue to segment?
 
	return 0;
}
 
void intu()
{
	printf("You tried to interrupt me but I won't let you!\n");
}
 
void seg()
{
	printf("I segmented!\n");
 
}

Experiments

Experiment 1 Structure variables

Question

When defining a struct, can you give a value to any variables within the struct? If not, then why not?

Below is the code I attempted to compile:

#include <stdio.h>
 
 
 
struct list
{
	int variable = 57;
};
 
int main()
{
 
	/*struct list fashizzle;
	fashizzle.variable = 10;
	printf("\nThe variable is %d\n", fashizzle.variable);*/
 
	return 0;
}

Here is what happens when attempting to compile:

lab46:~/Desktop/experiments$ gcc -Wall structvar.c 
structvar.c:7: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
lab46:~/Desktop/experiments$ 

So the answer to the question is: No you cannot give value to a variable when defining a struct.

The reason for this is quite simple, as I have stated in previous parts of my opus a variable is simply a human name given to an address. When you are defining a structure, you are not assigning an address yet, that does not happen until you actually declare a structure in the main program. At that point a memory address is given and now you can give it a name and store a value.

Experiment 2 Enum 0 or 1

Question

After learning about enum I was curious as to what value it starts at, is it 0 or 1? Or possibly some other random value?

Here is my code:

#include <stdio.h>
 
int main()
{
	enum days{sunday, monday, tuesday, wednesday, thursday, friday, saturday};
	printf("%d\n", sunday);
	printf("%d\n", monday);
 
	return 0;
}

Here is the output:

lab46:~/Desktop/experiments$ ./a.out 
0
1
lab46:~/Desktop/experiments$

It would appear that the default for enum is to start at 0

Experiment 3: Enum + 1

I was curious if enumerated values would still allow you to math operators. Basically I am going to take an enumerated variable and see if I can add one and get the next enumerated variable.

Using the same code in previous experiment except instead of printing Monday, I will try to print sunday + 1

#include <stdio.h>
 
int main()
{
	enum days{sunday, monday, tuesday, wednesday, thursday, friday, saturday};
	printf("%d\n", sunday);
	printf("%d\n", sunday+1);
 
	return 0;
}

Here is the output:

lab46:~/Desktop/experiments$ ./a.out 
0
1
lab46:~/Desktop/experiments$ 

As we can see it worked the same way as the previous experiment meaning enumerated variables can still have math ops used on them.