Table of Contents

Part 2

Entries

Entry 5: March 26, 2012

The fact that I can not figure out how to use atoi has led me to use this opus entry on Type Conversions.

The C Programming Language 2nd edition by Kernighan and Ritchie says

“When an operator has operands of different types, they are converted to a common type according to a small number of rules. In general the only automatic conversions are those that convert a “narrower” operand into a “wider” one without loosing any information… A char is just a small integer, so chars may be freely used in arithmetic expressions. This permits considerable flexibility in certain kinds of character transformations. One is exemplified by this naive implementation of the function atoi, which converts a string of digits into its numeric equivalent.”

/* atoi: convert s to integer */
int atoi(char s[])
{
    int i, n;
 
    n = 0;
    for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
        n = 10 + n + (s[i] - '0');
    return n;
}    

That is all I really read and found in the book/index that specifically talked about the atoi function.

C Pocket Reference by Prinz & Kirch-Prinz

Conversion Between Strings and Numbers

“A variety of functions are declared in the header file stdlib.h to obtain numerical interpretations of the initial digit characters in a string. The resulting number is the return value of the function.”

int atoi( const char *s );
    /*Interprets the contents of the string s as a number with type int.  The analogous functions atol(), atoll()(*),
    and atof() are used to convert a string into a number with type long, long long(*), or double.*/

And that is pretty much what that book had to say about my problem so I guess I will go ahead and try to work on implementing this into my project bignum.c

Entry 6: March 30, 2012

for(x = digit1; x >= 0; x--)
{
    *(result+x) = *(value1+x) + *(value2+x) + *(result+x);
    if(*(result+x) > 9)
    {
        *(result+x) = *(result+x) - 10;
        *(result+x - 1) = *(result+x - 1) + 1;
    }
}

For this opus entry I am going to try and re-break down what Matt went over with me in the last class session.

The first line of the for loop in the the code block above is stating:

for the instance that the variable x equals the int digit1 and also the variable x is greater than or equal to zero decrement x by 1.

*(result+x) = *(value1+x) + *(value2+x) + *(result+x);

This line is saying that the pointer of result+x is equal to the pointer of value1+x plus the pointer of value2+x plus the pointer of result+x which has been zeroed out for the program.

if(*(result+x) > 9)

The if statement is creating a condition that if the pointer of result+x is greater than the number 9 to go on and do what other instructions are in the if statement.

    {
        *(result+x) = *(result+x) - 10;
        *(result+x - 1) = *(result+x - 1) + 1;
    }

Inside of the if statement as the code shown above is saying process this code when the if statement is true.

Take the current result x variable you are working with and subtract 10 and take the next result x variable to the left and add 1 to it.

Entry 7: April 3, 2012

Inheritance in object-oriented programming is a way to use code that you have previously included in a class, usually refered to as parent classes, base classes, or a superclasses. The new classes that are inheriting the base class are commonly refered to as child classes or subclasses and or derived classes.

#ifndef _RECTANGLE_H
#define _RECTANGLE_H
#include "shape.h"
 
class rectangle: public shape{
        public:
                virtual int perimeter();
                virtual int area();
};
#endif

As shown in the code block example above this file called rectangle.h would be inheriting the public classes code from the file shape.h making the parent file shape.h and the child file rectangle.h

Class inheritance has three different types of data:

Public:

Any other files in your inheritance tree that include the header files needed to use those public variables of the class you are trying to access can.

Protected:

Only a child file of the parent can use protected files, if a child is not directly pointed to the parent file they are trying to access protected data from that child file will not be able to access the protected portion of that code.

Private:

Private variables are only accessable by the class they are defined in, although they can be accessed through a call to a protected or public variable in the same class that has access.

Entry 8: April 5, 2012

The GNU Compiler Collection or (GCC) is a programming code compiler, a compiler is an application that uses source code written in a programming language and creates object code to have an executable program from the source code.

The languages the compiler can compile that we deal with in our class are C (gcc) and C++ (g++)

The compiler consists of a front-end and back-end

The front-end of the compiler handles the programming language setting up the information to be handled by the back-end

The back-end deals with converting the language into the specific target architecture

Keywords

cprog Keywords

cprog Keyword 9

Scope (Block, Local, Global, File)

Definition

Global Scope is defined outside of specific functions in the head section of a program and is usually accessible by the entire program.

File Scopes act about the same as Global scopes with the exception that the global scope is accessible by the whole program and the file scope is just accessible in that particular FILE.

Local Scope is defined within a specific function and is usually just relevant to that function and would not be recognized by another function it was not part of.

Block Scope is defined within a specific code block

{
Block Scope
}
Demonstration

Demonstration of the chosen keyword.

int x = 1; // global variable
12
13  int main()
14  {
15     int x = 5; // local variable to main
16
17     cout << "local x in main's outer scope is " << x << endl;
18
19     { // start new scope                                        
20        int x = 7; // hides x in outer scope                     
21                                                                 
22        cout << "local x in main's inner scope is " << x << endl;
23     } // end new scope                                          

cprog Keyword 10

Type Casting

Definition

Type Casting happens when you have a given expression and you convert or assign that expression another type.

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:

short a=2000;
int b;
b=a;

cprog Keyword 11

Selection Structures (if, case/switch)

Definition

An If selection structure is dependent on a condition statement, for example if something is true do this OR if something is false do this other thing.

A switch case selection structure is a shorter substitute to long if statements, a value variable is compared and or switched in each of the following cases for the programs structure.

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:

switch ( <variable> ) {
case this-value:
  Code to execute if <variable> == this-value
  break;
case that-value:
  Code to execute if <variable> == that-value
  break;
...
default:
  Code to execute if <variable> does not equal the value following any of the cases
  break;
}

cprog Keyword 12

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

Definition

The repetition structure of a while loop allows for a statements execution to run repeatedly until the condition of that loop has been met.

The do while loop has the same structure as the while loop with the exception that do while loop tests the condition at the end of the loop structure so that the statement will be executed at least once.

The for loop handles all ( variable initialisation; loop condition; variable update ) in the same set of paraenteses.

Demonstration

Demonstration of the chosen keyword.

while LOOP

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    int count = 0;
    while(count < 10)
    {
    printf("%d\n", count);
    ++count;
    }
    return 0;
}

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

Sample output:

0
1
2
3
4
5
6
7
8
9

cprog Keyword 13

Structures (Declaration, Accessing Elements, Pointers to)

Definition

Structure Declarations are declared by the preset struct then followed by a variable name in c programming.

When accessing elements of a structure you need to call both the name of the struct and the variable name of the struct to access the individual elements.

You can also your Pointers to access elements inside of structs by declaring the pointer and implementing it to a global variable.

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

cprog Keyword 14

typedef, enum, union

Definition

While structures define several fields data types a UNION is only used to define one location that may be addressed by different names.

type def gives you the ability to name and define your own variables with the use of a typedef statement.

enumerated data - used for variables that may only contain a set number of values, the variables are then referenced by the name or (tag).

Demonstration

typedef example

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    typedef int day_of_the_week; // Define the type for days of the week
    const int SUNDAY = 0;
    const int MONDAY = 1;
    const int TUESDAY = 2;
    const int WEDNESDAY = 3;
    const int THURSDAY = 4;
    const int FRIDAY = 5;
    const int SATURDAY = 6;
    /* Now to use it */
    day_of_the_week today = TUESDAY;
    return(0);
}

enum example

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    enum day_of_the_week {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY};
    /* Now use it */
    enum day_of_the_week today = TUESDAY;
    return(0);
}

cprog Keyword 15

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

Definition

Functions are sets of statements that contains three main parts a function header, function body and function prototypes functions can also be called on by a program from outside of that function.

Parameters are values within the functions.

When you pass a parameter by Reference you are changing the value of the parameter when you pass it.

When you pass the parameter by Value you are not changing the parameter just passing it along.

Command-Line Arguments are used for retrieving parameters entered by the user when using your program.

The Return Type is the syntax and definitions of values returned from a function.

Recursion is when a function can call on itself with out an acting outside call.

Demonstration

Functions

int main()
{
   ...
}

Recursion

int factorial(int n)
{
   if(n == 1 || n == 0)
   {
      return 1;
   }
   else
   {
      return n*factorial(n-1);
   }
}

Parameters

int main
{
   fctn(num1, 12);
}
 
void fctn(int arg1, int arg2)
{ ... }

cprog Keyword 16

Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)

Definition

The Compiler is the actual piece of software that turns your code into an executable.

The Preprocessor allows for your programs to include header files, macro expansions and conditional compilation.

Flags are used to optimize how you want to compile the code for example -wall -o etc…

An assembler takes source code to produce machine code.

A Linker takes the compiler's object files and puts them all together so the computer recognizes the files and runs them together as a program.

cprog Objective

cprog Objective

to learn how to become and effective programmer in the languages of c and c++

Definition

syntax

indenting format

compiling

functions

classes

Libraries

Header Files

Pointers

Arrays

etc…

Method

Well from knowing zero day 1 I now know more than zero.

Measurement

followed.

Analysis

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

  • How did you do?

ok.

  • Is there room for improvement?

of course.

  • Could the measurement process be enhanced to be more effective?

not sure.

  • Do you think this enhancement would be efficient to employ?

n/a

  • Could the course objective be altered to be more applicable? How would you alter it?

more beginner focus.

Experiments

Experiment 4

Question

What will happen if you do not include the \n at the end of a printf statement.

Resources

lab46

Hypothesis

I am thinking that without the \n the printf statement will not display.

State your rationale. I am pretty sure \n is a terminator for the printf statement.

Experiment

I am going to test my hypothesis on lab46 with my hello.c program.

Data

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

Analysis

Based on the data collected:

No.

it was applicable.

No.

I guessed wrong.

None.

Conclusions

The program still complied and ran without the \n at the end of my printf statement but as you can see the command prompt ended up on the same line that the program executed the printf on.

So the \n is a line terminator and not a printf function terminator if that makes sense in the best of my ability to explain what I am experimenting.

Experiment 5

Question

For this experiment I will be seeing what will happen if the program does not include a main function and just includes statements.

Like this.

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

Resources

Lab46

Hypothesis

I am not sure what will happen I am assuming the program will not compile or function properly.

State your rationale.

I am thinking that the program needs a Function structure of some sort to run properly.

Experiment

going to test this on lab46

Data

lab46:~$ gcc -o hello hello.c
hello.c:3: error: expected identifier or '(' before '{' token

Analysis

Based on the data collected:

Yeah I believe this time it was I did get an error for an expected identifier.

it was applicable.

No I think the identifier the program is looking for is the main function.

None.

None.

Conclusions

The program did not compile when the line of

int main()

was removed from the program I am not 100% sure if it was asking for a function container, okay well i just did this experiment to test it further.

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

and the program ran and compiled so the int was not necessary it was looking for a main() function or a function container in general I am guessing. Not sure if it has to named main() but that sounds like another experiment.

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.

Resources

Evaluate their resources and commentary. Answer the following questions:

Hypothesis

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

Experiment

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

Data

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

Analysis

Answer the following:

Conclusions

Answer the following: