Table of Contents

Part 2

Entries

Entry 5: March , 2012

If the union is named value, and the variables are [float f, int x], then when called, the variables will be value.f for the float variable, and value.x for the int variable.

Entry 6: March 6, 2012

 **//typedef//** int length; 

This would cause the int length to shortened to just length:

 length max, min; 

Entry 7: March 22 (I think), 2012

Entry 8: March 13, 2012

Keywords

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.

  • I still have some errors to sort through, but the program mostly works. I think I handled it fairly well.
  • There is still room for improvement, I had a lot of trouble making the first few functions and getting them to work properly.
  • I could have cleaned up the functions or used one function to make all of the arrays.
  • I don't believe it would have been efficient, it would have made things very complicated and it would have left too much room for error.
  • I think it is pretty applicable.

Experiments

Experiment 4

Question

What is the difference between using the fgetc() function and the fscanf() function?

Resources

According to a wiki on the website http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/, fscanf's purpose is: “Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments.” This means that fscanf takes a stream that is input and stores that stream into a variable with a specified data type. According to a similar wiki on the same website, the function fgetc's purpose is: “Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced by one character to point to the next character.” In other words, fgetc takes a string and returns whatever it is being pointed to, character by character, until the string ends. One can take a variable and assign it these values.

Hypothesis

I think that these two do very similar things, but the main difference is that a programmer can determine a variable data type with fscanf, while fgetc will always return a character. I that they will do the same thing when the data type of the value in the fscanf function is char, but other than that, you will get different results.

Experiment

I will create a program that accepts the same value through both fgetc and fscanf and assigns them to different variables of the same data type. Then, I will use some fprintf functions to print these values, once as a char, and once as an int.

Data

Experiment:

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
        int scanvalue;
        int fgetcvalue;
        fprintf(stdout, "Input one number twice: ");
        fscanf(stdin, "%d", &scanvalue);
        fgetcvalue = fgetc(stdin);
        fprintf(stdout, "FSCANFVALUE being printed as an integer: %d\n", scanvalue);
        fprintf(stdout, "FGETCVALUE being printed as an integer: %d\n", fgetcvalue);
        fprintf(stdout, "FSCANFVALUE being printed as a character: %d\n", scanvalue);
        fprintf(stdout, "FGETCVALUE being printed as a character: %d\n", fgetcvalue);
        return(0);
}

Result:

lab46:~/src/cprog/Opus/Opus2$ ./experiment4
Input one number twice: 10
FSCANFVALUE being printed as an integer: 10
FGETCVALUE being printed as an integer: 10
FSCANFVALUE being printed as a character: 10
FGETCVALUE being printed as a character: 10
lab46:~/src/cprog/Opus/Opus2$ ./experiment4
Input one number twice: 10 10
FSCANFVALUE being printed as an integer: 10
FGETCVALUE being printed as an integer: 32
FSCANFVALUE being printed as a character: 10
FGETCVALUE being printed as a character: 32
lab46:~/src/cprog/Opus/Opus2$ ./experiment4
Input one number twice: 4 
FSCANFVALUE being printed as an integer: 4
FGETCVALUE being printed as an integer: 32
FSCANFVALUE being printed as a character: 4
FGETCVALUE being printed as a character: 32
lab46:~/src/cprog/Opus/Opus2$ 

Analysis

Based on the data collected:

Conclusions

What this experiment helped me understand is that fgetc and fscanf are applicable when accepting input, but I should watch out for any extra input, like enter, space, backspace, etc.

Experiment 5

Question

When given two different arrays with different values in them, what would happen if you set one equal to the other?

Resources

According to what I have learned, an array, when set equal to another array, as a pointer, will only have the array point to the address of the other.

Hypothesis

I think that the array that will be set equal to another array will point to the address of the other, changing what memory it holds to the same memory as the other array. I have used this method a lot, and, so far, all I have seen is this result. Testing it now!

Experiment

I will create code that will take two arrays, with different values stored in them, and set one equal to the other, and then reverse them and set them equal to each other. I will print each of the arrays before and after this switch of values.

Data

DA CODE:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        char *array1, *array2;
        int i;
        array1 = (char *) malloc (sizeof(char) * 4 );
        array2 = (char *) malloc (sizeof(char) * 4 );
 
        printf("Input a 4 letter word: ");
        for( i = 0; i <= 3; i++ )
        {
                *( array1 + i ) = fgetc( stdin );
        }
 
        for( i = 0; i <= 3; i++ )
        {
                printf("%c", *( array1 + i ) );
        }
 
        getchar();
 
        printf("\nInput a 4 letter word: ");
        for( i = 0; i <= 3; i++ )
        {
                *( array2 + i ) = fgetc( stdin );
        }
 
        for( i = 0; i <= 3; i++ )
        {
                printf("%c", *( array2 + i ) );
        }
 
        array1 = array2;
        array2 = array1;
 
        printf("\nArray1: ");
 
        for( i = 0; i <= 3; i++ )
        {
                printf("%c", *( array1 + i ) );
        }
 
        printf("\nArray2: ");
 
        for( i = 0; i <= 3; i++ )
        {
                printf("%c", *( array2 + i ) );
        }
 
        printf("\n");
        return 0;
}

The result:

lab46:~/src/cprog/Opus/Opus2$ ./experiment5 
Input a 4 letter word: Cavo
Cavo
Input a 4 letter word: Josh
Josh
Array1: Josh
Array2: Josh
lab46:~/src/cprog/Opus/Opus2$ ./experiment5
Input a 4 letter word: Josh
Josh
Input a 4 letter word: Cavo
Cavo
Array1: Cavo
Array2: Cavo
lab46:~/src/cprog/Opus/Opus2$ 

Analysis

Based on the data collected:

– My hypothesis was correct, the first time one was assigned to the other, it dropped it's previous memory assignment and pointed to the new one.

– I believe my hypothesis was applicable.

– There doesn't seem to be more than I thought going on.

– There weren't any shortcomings, FOR THE FIRST TIME, I MADE CODE AND IT RAN JUST AS I WANTED IT TO! NO PROBLEMS!

– My data was correct and accounted for!

Conclusions

I can conclude that my knowledge of arrays has increased, and is pretty good, now. Probably due to the great amount of arrays and array passing in projects 2 and 3.

Retest 2

Perform the following steps:

State Experiment

Whose existing experiment are you going to retest? Provide the URL, note the author, and restate their question.]

– I will be retesting Robert Matsch's third experiment, asking what happens if one accidentally puts ' in instead of .

URL: http://lab46.corning-cc.edu/opus/spring2012/rmatsch/start#experiment_3

Resources

Evaluate their resources and commentary. Answer the following questions:

– He stated one resource, the C ANSI book that was purchased for the class. I believe that this would give enough information about the experiment.

– The interwebs!

– I believe he has, it gave him many errors, so I believe that he could understand that it isn't a good thing to do. (I hope)

– I have no objections. Everything seems to be in order!

Hypothesis

State their experiment's hypothesis. Answer the following questions:

– Yeah, I believe that it covers anything they may see.

– I think that he could have added more explanation, but mainly a spellcheck! :x

Experiment

Follow the steps given to recreate the original experiment. Answer the following questions:

– They are correct, there isn't much more one can do when testing to see if it works or not.

– I have no suggestions.

– I would have added the result of the working code as well, and I would have just attempted to run the incorrect code, just to show the reader what happens. That could give the reader more piece of mind on the matter.

Data

Publish the data you have gained from your performing of the experiment here.

The code of the correctly done program:

#include <stdio.h>
 
int main()
{
        printf("Hello, World!\n");
        return 0;
}

the code of the incorrectly done program:

#include <stdio.h>
 
int main()
{
        printf('Hello, World!');
        return 0;
}

The result of the correctly done program, followed by the result of the incorrectly done program:

lab46:~/src/cprog/Opus/Opus2$ gcc -o retest retest.c
lab46:~/src/cprog/Opus/Opus2$ gcc -o retestwrong retestwrong.c
retestwrong.c:5:9: warning: character constant too long for its type
retestwrong.c: In function 'main':
retestwrong.c:5: warning: passing argument 1 of 'printf' makes pointer from integer without a cast
/usr/include/stdio.h:339: note: expected 'const char * __restrict__' but argument is of type 'int'
lab46:~/src/cprog/Opus/Opus2$ ./retest
Hello, World!
lab46:~/src/cprog/Opus/Opus2$ ./retestwrong
Segmentation fault
lab46:~/src/cprog/Opus/Opus2$ 

Analysis

Answer the following:

– The data that I have seems nearly identical to the data that Rob had.

– There were no deviations.

– The only errors were the expected ones that came with only putting ' instead of .

– The stated hypothesis was adequate, he seemed to know what the result would be.

Conclusions

Answer the following:

– I concluded that when putting a ' symbol in for a symbol in printf statements, an error occurs. I wasn't sure if something else would happen, which is why I chose this experiment.

– Absolutely, this experiment helped me to determine the result of my curiosity of a different, but slightly similar, symbol replacing the quotations.

–I believe he has gotten some value out of this experiment.

– I don't think there are any suggestions, seems pretty straight forward.