User Tools

Site Tools


opus:spring2012:smalik2:part1

Part 1

Entries

Entry 1: January 26, 2012 - C/C++ Programming

Today, for C/C++ in-class we learned a good deal about pointers. Beforehand, from learncpp.com I had read about pointers and even messed around with them in visual studio express but they always manage to confuse me or trip me up somehow. We also did a little on binary, and if knowing binary well becomes a necessity I will have to google and read up on it.

Regarding pointers and whatnot, we made a .c file in class with two variables, with b pointing towards a. We would assign pointer b a value, and that value would then be assigned to a. It's almost like the point is a soft shortcut to whatever address it's pointing to. What threw me off was that we would then fstream the b value, and de-reference it, and then b would suddenly be equal to 12. I'm guessing repetition, and messing with the code, will help me understand it. For the most part, it all did make sense. Along with the binary, and signed and unsigned variables.

Overall, pointers and referencing addresses of variables makes sense to me. When it comes to a bunch of things with pointers and address references going on at once, I find it a little challenging to try to follow what exactly is happening. As for binary, I can understand enough to understand how signed and unsigned works.

Entry 2: February 1, 2012 - Unix/Linux Fundamentals

I am supposed to make a tree of the UNIX file system, with my home directory in it along with a couple other things.

                        root
                          |
                        home
                          |
                      smalik2
                      /  |   \
              archives  bin  src
                             /  \
                           unix  cprog
                             

Entry 3: February 9, 2012 - Unix/Linux Fundamentals

All of the things that we learned in the first two weeks for Linux has actually helped me understand more other things outside of class. Going through directories, moving files, copying files, and other commands we've learned so far I've been using.

Over the past couple weeks I've been modding and messing around with my phone, and since I try to primarily use Ubuntu, I end up using terminal a lot. When I run into a guide on a website and they just have lists of commands to type in terminal to make something happen, I actually know what they're talking about as opposed to just following directions. This level of understanding really helps when it comes to troubleshooting problems, and with not blindly following instructions just hoping that it will magically work.

Since Android phones are based on a Linux kernel, when it comes to navigating my phones contents through the terminal on the computer, I actually know how to do it. I can really appreciate how nicely my need to know this material has coincided with this class. I also appreciate how widely used Linux appears to be, although many people probably do not realize it.

Entry 4: February 12, 2012 - C/C++ Programming

This past Friday in Object Oriented Problem Solving, we were creating an algorithm to read a user entered number. The ultimate goal will be to read the number and convert it to base ten, but right now the objective is to simply read in the number, each digit separately (without the user actually entering each digit separately).

I attempted to do this using C, but kept on hitting various errors. Segmentation faults, syntax, logical errors, etc. The end result was that there was SOMETHING wrong with the line containing the modulus function, where it would take a mod of the number entered by powers of ten (basically knocking off and storing the last digit in the number), and the digit would not be stored in the Array.

I am not sure if this is a problem with the array (I am still not comfortable with C, I'm much more used to C++) or the mathematical functions being performed. I briefly talked to Professor Oppenheim about it after class, but unfortunately I was in a rush so could not check the code with him. We will look over it next time we have class hopefully.

We figured it out, the problem was that one of the variables that was being changed had not been passed by reference. Adding an & fixed this, and the program started running.

Entry 5: February 27, 2012 - UNIX/Linux Fundamentals

This is somewhat random, but I since I have been running Ubuntu 99% exclusively on my laptop now and it is Linux based, it is part of my everyday life pretty much. So much so that I recommend it over Windows to my friends. I was helping somebody (who is not taking UNIX) install it on their laptop, and then when they were trying to use it they had to ask me how to create just a text file - thanks to the UNIX class I could immediately just tell them 'just use terminal, and then nano [filename].txt'. I would have recommended vim, but they had tried it before and it didn't end well.

Additionally, I am contemplating attempting the Linux from Scratch project which is basically building a linux based OS from scratch – teaching you a ton about the whole thing and how it works in the process. Perhaps I will attempt this when I have more free time on my hands.

Keywords

cprog Keywords

LIST FOR C/C++ KEYWORDS

The following is a list of the major topics being covered in this course:

DONE: Standard I/O (STDIO, STDOUT, STDERR)
DONE: Header Files (Local and System), C Standard Library (Libc), Libraries
DONE: arithmetic (equations, operators)
DONE: logic and operators (and, or, not, xor)
DONE: Variables (types, ranges, sizes)
DONE: Scope (Block, Local, Global, File)
DONE: Pointers (address of, assignment, dereferencing)
DONE: Type Casting
DONE: Selection Structures (if, case/switch)
DONE: Repetition/Iteration Structures (for, while, do while)
DONE: Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)
DONE: File Access (Read, Write, Append)
Structures (Declaration, Accessing Elements, Pointers to)
typedef, enum, union
DONE: Functions, Parameters (Pass by: Value, Address, Reference), Return Types, Recursion, Command-line arguments
DONE: Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)
DONE: I/O Streams (cin, cout, cerr, stream operators) [C++]
DONE: Namespaces [C++]
DONE: Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]
DONE: Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]
DONE: Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]
DONE: Overloading (Functions, Operators) [C++]
Exception Handing (throw, try, catch) [C++]
DONE: Templates, STL (Standard Template Library) [C++]

1: Repetition/Iteration Structures

Definition

Repetition/Iteration Structures are basically the coding that is necessary if you want to repeat a process many times, or to count. If you wanted to input 500 values into an array, you'd have to use a repetitive structure (the alternative being typing 500 lines of code). Or let's say you want to run a process multiple times, but each time something is slightly different, you'd use a repetitive 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:

Using a for loop

 
//THIS PROGRAM WILL COUNT FROM 0-20
 
#include <stdio.h>
#include <stdlib.h>
 
 
int main()
{//START MAIN
 
        int count = 0;          //DECLARING AND INITIALIZING A VARIABLE FOR COUNTING
 
 
        for(count; count<=20; ++count)          //FOR LOOP TO COUNT FROM 0-20
        {
                printf("%u sheep.\n", count);   //FSTREAM # AND SHEEP
        }       //END FOR LOOP
 
        return (0);     //RETURN 0
}//END MAIN

Using a do while loop

#include <stdio.h>
 
int main()
{
 
        int count=0;
        do
        {
                printf("%u sheep.\n", count);
                ++count;
        }
        while(count<=20);
 
        return (0);
}

Using a while loop

#include <stdio.h>
 
int main()
{
        int count = 0;
 
        while(count<=20)
        {
                printf("%u sheep.\n", count);
                ++count;
        }
 
        return 0;
}

The outputs of all of these programs lead to the SAME THING, so I'm only putting it once.

lab46:~/src/cprog/MessingAround$ nano RepetitiveDoWhile.c
lab46:~/src/cprog/MessingAround$ gcc -o RepetitiveDoWhile RepetitiveDoWhile.c
lab46:~/src/cprog/MessingAround$ ./RepetitiveDoWhile
0 sheep.
1 sheep.
2 sheep.
3 sheep.
4 sheep.
5 sheep.
6 sheep.
7 sheep.
8 sheep.
9 sheep.
10 sheep.
11 sheep.
12 sheep.
13 sheep.
14 sheep.
15 sheep.
16 sheep.
17 sheep.
18 sheep.
19 sheep.
20 sheep.

2: Variables (types, ranges, sizes)

Definition

Variables are structures that can hold types of data that can be manipulated.

Demonstration

To define a variable you have to make a statement in the code.

[type][identifier] = [value];

The type is the type of variable - Char short int int long int long long int float double long double

All of these variables can also be signed or unsigned (whether or not half of the memory allocated to each variable will be used for negative values, or further positive values.)

The sizes and ranges of these variables can differ, as shown by the output of a program (Project 0) that prints it all.

An unsigned char is 1 bytes
The range of an unsigned char is 0 to 255
An unsigned char can store 255 unique values

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

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

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

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

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

A 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

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

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

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

3: Pointers (address of, assignment, dereferencing)

Definition

A pointer is basically a variable, that contains the address/location of another variable.

Demonstration

To assign a pointer, you denote it with;

*[variable declaration];

The asterisk denotes pointer. And you can have two, or even three to designate a double or triple pointer.

To dereference a pointer, you use an asterisk again.

I think of a pointer as 'going higher' and then dereferencing as 'going back down'.

So if you have a variable called pointer, which is a single pointer, that variable is one level up. To put something in it, you have to bring it down a level.

The plain variable is just the address of where the actual value is being stored.

So, given the following code and output, you can see that the normal variable you can just printf it normally. But when it comes to storing and printing to the pointer, you need to dereference it to take the pointer back down to the storage unit that it specifies.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int *pointer, variable;
 
        *pointer = 9001;        //need  to dereference to store it
 
        variable = 1111;        //not a pointer so it's fine
 
        printf("\nVariable is %d\n", variable);
 
        printf("\nPointer is %d\n", pointer);   
//Since it is not dereferenced, it will print an address (garbage).
 
 
        printf("\n*Pointer (dereferenced) is %d\n", *pointer);
//Since it has been dereferenced, it will properly display the value.
 
        return (0);
 
}
lab46:~/src/cprog/MessingAround$ ./pointers

Variable is 1111

Pointer is -1584842064

*Pointer (dereferenced) is 9001

4: File Access (Read, Write, Append)

Definition

File Access is basically the term for when you read from, write to, or append a file. Basically the program can open a .txt file or a .data file or whatever, and then either read information from it (such as a list of numbers or something) or write information to it (such as results of some calculation), or append (just add onto what is already in the file).

Demonstration

The following program will take an input file, and then add 1 to every number in the input file, and write those results into a different file.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
        int number;
 
        FILE *in, *out;
//Defines two file variable pointers called in and out.
 
        in = fopen("numbers.txt", "r");
//Opening the input file (known as numbers.txt). The r denotes reading.
 
        out = fopen("output.txt", "w");
//Opening the output file (naming it output.txt). The w denotes writing.
 
        if(in == NULL)
        {
                printf("Error, need an input file numbers.txt");
                exit(1);
        }
//This block checks to make sure that a file gets thrown into the in variable
 
        fscanf(in, "%hhd", &number);
//That in at the beginning of the statement indicates that input file
 
        while(number != 17)
        {
                int newnumber=0;
 
                newnumber = number+1;
 
                fprintf(out, "%hhd\n", newnumber);
 
                fscanf(in, "%hhd", &number);
        }
 
        return (0);
}

5: Standard I/O (STDIO, STDOUT, STDERR)

Definition

Standard I/O is the Default Input, Output, and Error Output. Normally these are the keyboard, and the monitor.

Demonstration

When we use scanf(“%d”, &number); it inputs an integer value from the standard input, which is the keyboard.

If we use fscanf(in, “%d”, &number); it inputs an integer value from whatever is defined as 'in' (like a text file), so we changed the input.

When we use printf(“Hello”); it prints Hello to the standard output, which is the monitor.

If we use fprintf(out, “Hello”); it prints Hello to whatever output is defined by 'out' (for instance, a text file). So we changed the output.

The errors are outputted to the STDOUT typically, and we haven't covered redirecting them in C yet.

6. Arrays (standard notation, pointer arithmetic, single-dimensional, multi-dimensional)

Definition

An array is basically a bunch of different data values (of the same type) that fall under the same identifier. The identifier is basically a gigantic data block, that has been sliced into smaller blocks, each of which holds the different data values.

Demonstration

This program will use two 1-d arrays, and 1 2-d array. It will store 0-4 in the 1-d arrays, and in the 2-d array it shows examples of counting by increasing numbers in sets of 5. So, in the first one it counts forward by zeros. In the second set it counts forward by 1s. Third by 2s. Etc.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int Array[5];
 //This defines an array that can hold 5 values
 
 
        int D_Array[5][5];
//This defines a 2-d array, so it holds 5 values, each of which have five values underneath (like a table of sorts).
 
        int *P_Array;
        P_Array = (int*)malloc(sizeof(int)*5);
//This defines a 5 slot array, but using pointer style arithmetic.
 
        int count = 0;
        int count2 = 0;
        int number = 0;
 
        for(count = 0; count < 5; ++count)
        {
                int number2 = 0;
 
                Array[count] = number;
 
                *(P_Array + count) = number;
 
                for(count2 = 0; count2 < 5; ++count2)
                {
 
                        D_Array[count][count2] = number*number2;
                        ++number2;
                }
                ++number;
        }
 
 
        printf("\nThe values in Array\n");
 
        for(count = 0; count < 5; ++count)
        {
                printf("%d ", Array[count]);
        }
 
        printf("\nThe values in P_Array\n");
 
        for(count = 0; count < 5; ++count)
        {
                printf("%d ", *(P_Array + count));
        }
 
        printf("\nThe values in D_Array\n");
 
        for(count = 0; count <5; ++count)
        {
                for(count2 = 0; count2 < 5; ++count2)
                {
                        printf("%d ", D_Array[count][count2]);
                }
 
                printf("\n");
        }
 
        printf("\n");
 
        return (0);
}
lab46:~/src/cprog/MessingAround$ vim arrays.c
lab46:~/src/cprog/MessingAround$ gcc -o arrays arrays.c
lab46:~/src/cprog/MessingAround$ ./arrays

The values in Array
0 1 2 3 4
The values in P_Array
0 1 2 3 4
The values in D_Array
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16

lab46:~/src/cprog/MessingAround$

7: Selection Structures (if, case/switch)

Definition

Selection structures are basically ways of checking. They allow you to check things within the program, and decide what to do based on the check. With more complex logic operating behind the checks, the more interactive and 'intelligent' the program becomes.

Demonstration
IF statements

The following program will take a number, and using if blocks determine IF the number is even or odd.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int number = 0;
        int x;
 
        printf("Enter a number: ");
 
        scanf("%d", &number);
 
        x = number%2;
 
        if(x == 0)
        {
                printf("\n%d is even\n", number);
        }
        else
        {
                printf("\n%d is odd\n", number);
        }
        return (0);
}
lab46:~/src/cprog/MessingAround$ vim Selection.c
lab46:~/src/cprog/MessingAround$ gcc -o Selection Selection.c
lab46:~/src/cprog/MessingAround$ ./Selection
Enter a number: 2

2 is even
lab46:~/src/cprog/MessingAround$ ./Selection
Enter a number: 3

3 is odd
Switch Case
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int number;
 
        printf("Enter a number: ");
        scanf("%d", &number);
 
        int x;
        x = number%2;
 
        switch (x)
        {
        case 0:
                printf("The number is even");
                break;
        case 1:
                printf("The number is odd");
                break;
        default:
                printf("The number is neither even nor odd");
                break;
        }
 
        printf("\n");
 
        return (0);
}
lab46:~/src/cprog/MessingAround$ gcc -o SwitchCase SwitchCase.c
lab46:~/src/cprog/MessingAround$ ./SwitchCase
Enter a number: 6
The number is even
lab46:~/src/cprog/MessingAround$ ./SwitchCase
Enter a number: 9
The number is odd
lab46:~/src/cprog/MessingAround$ 

8: Logic and Operators

Definition

Logic and operators allow you to make a program much more interactive and capable.

Demonstration

And can be used to ensure two values are both true.

Or can be used to ensure at least one value is true.

Not returns the opposite of a value , from true to false and vice versa.

XOR will return true if JUST ONE value is true.

cprog Objective

cprog Objective

Use pointers and variables to discover the indirect properties of data storage

Definition

Basically this means that we will figure out, and be able to utilize, pointers and variables for the sake of different methods and 'tricks' behind data storage.

Method

Probably paying attention when Matt Haas is talking about those things would be a great start. Defining these as keywords would help as well.

Measurement

I defined them both as keywords, and it did help me understand a lot more. I already knew variables for the most part, but all the stuff to do with pointers I was sketchy on and now I feel much more confident about it.

Analysis

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

I think I did great. Of course there is room for improvement, but I am no longer clueless. Not really, being forced to define them and give working examples of them (for me) works very well. Perhaps the course objective would make a little more sense if it went into a little bit more detail in what it means by 'indirect properties data storage'.

unix Keywords

THIS IS THE LIST –

The following is a list of the major topics being covered in this course:

DONE: Local host
DONE: Remote host
DONE: Home directory
DONE: Current working directory
DONE: Types of files (regular, directory, special)
DONE: File manipulation
DONE: Text Processing
DONE: The VI editor
DONE: The UNIX Shell
DONE: Environment variable
DONE: $PATH
DONE: wildcards
DONE: tab completion
DONE: Job Control
DONE: The UNIX Programming Environment
DONE: Compiler, Assembler, Linker, Loader
DONE: Source Code, Object Code, Binary Code, Library
DONE: Pattern Matching
DONE: Regular Expressions
DONE: Filtering
DONE: Shell Scripting
DONE: networking, UNIX Networking Tools
DONE: Security
DONE: X Window System

1: File manipulation

Definition

The ability to control files, such as moving them, removing them, copying them, organizing them.

Demonstration

In this command line sample, you can see me moving files between directories, or for the sake of renaming, copying files, making directories, and removing a file. All of this is file manipulation.

lab46:~$ cd src/cprog
lab46:~/src/cprog$ ls
Feb14    Feb2.c    JAN26_printf_examples.c  JAN31_pointers3.c
Feb14.c  Feb7.c    JAN26_printf_pointers.c  MessingAround
Feb16    Feb7x0.c  JAN31_pointers.c         Projects
Feb16.c  Feb9.c    JAN31_pointers2.c        out.txt
lab46:~/src/cprog$ cd MessingAround
lab46:~/src/cprog/MessingAround$ ls
Algorithm.c   RepetitiveDoWhile.c  files    numbers.txt  pointers
Repetitive.c  RepetitiveWhile.c    files.c  output.txt   pointers.c
lab46:~/src/cprog/MessingAround$ rm files
rm: remove regular file `files'? y
lab46:~/src/cprog/MessingAround$ mv pointers lookitrenamed
lab46:~/src/cprog/MessingAround$ cp lookitrenamed lookanothercopy!!
cp lookitrenamed lookanothercopymv pointers lookitrenamed
cp: target `lookitrenamed' is not a directory
lab46:~/src/cprog/MessingAround$ cp lookitrenamed lookanothercopy
lab46:~/src/cprog/MessingAround$ mkdir lookanewdirectory
lab46:~/src/cprog/MessingAround$ mv lookitrenamed lookanewdirectory
lab46:~/src/cprog/MessingAround$ ls
Algorithm.c          RepetitiveWhile.c  lookanothercopy  pointers.c
Repetitive.c         files.c            numbers.txt
RepetitiveDoWhile.c  lookanewdirectory  output.txt
lab46:~/src/cprog/MessingAround$ cd lookanewdirectory
lab46:~/src/cprog/MessingAround/lookanewdirectory$ ls
lookitrenamed
lab46:~/src/cprog/MessingAround/lookanewdirectory$ 

2: Home directory

Definition

The home directory is your default directory. Whenever you open up a cmd prompt this is the file location that you start at.

Demonstration

So here, you can see that right when I log into lab46 my home directory is denoted by ~$. Using the pwd command (to show the full file structure) reveals that my home directory is at /home/smalik2

saad@saad-Satellite-P775:~$ ssh smalik2@lab46.corning-cc.edu
smalik2@lab46.corning-cc.edu's password: 
 __         _     _ _   __   . . . . . . . . . . . . . . . . . . . . . . . . .
|  |   __ _| |__ / | |_/ /   . Basic System Usage:  Type 'usage' at prompt   .
|  |__/ _` | '_ \\_  _/ _ \  . Events and News:     Type 'news' at prompt    .
|_____\__,_|_.__/  |_|\___/  . Broken E-mail?       Type 'fixmail' at prompt .
---------------------------  . Check Lab46 Mail:    Type 'alpine' at prompt  .
c o r n i n g - c c . e d u  . . . . . . . . . . . . . . . . . . . . . . . . .

 Lab46 is the Computer & Information Science Department's Student Development
 Server for Computer-related coursework, projects, and exploration.  For more
 information, please check out:
  .. .  .    .        .                .                .        .    .  . ..
 .  Lab46 Web Page:       http://lab46.corning-cc.edu/                       .
 .  Lab46 Help Form:      http://lab46.corning-cc.edu/help_request           .
 .  Help E-mail:          haas@corning-cc.edu or wedge@lab46.corning-cc.edu  .
  .. .  .    .        .                .                .        .    .  . ..
      _        
  .-./*)       
_/___/   `.    
  U U      'You can delete files with the 'rm' command.'
No mail.
Last login: Fri Feb 17 19:09:50 2012 from cpe-69-205-204-88.stny.res.rr.com
lab46:~$ pwd
/home/smalik2
lab46:~$ 

In this example, you can see me moving to a different directory using the cd command. The cd command with no argument takes you back to your home directory.

lab46:~$ pwd
/home/smalik2
lab46:~$ cd src/cprog/Projects
lab46:~/src/cprog/Projects$ pwd
/home/smalik2/src/cprog/Projects
lab46:~/src/cprog/Projects$ cd
lab46:~$ pwd
/home/smalik2
lab46:~$ 

3: Current working directory

Definition

The current working directory is the directory that you're in.

Demonstration

The current working directory is basically the directory that you are in. The cd command with no arguments will change your current working directory to your home directory.

The pwd command reveals the current working directory.

lab46:~$ pwd
/home/smalik2
lab46:~$ 

4: Types of files (regular, directory, special)

Definition

There are three types of files, regular files (such as .txt), directories (technically a file with other file locations in it), and special files (a directory is a type of special file, or C programs).

Demonstration

In the example we see that there are regular files, which are the .c files, directories, and then the special files (the compile C code).

lab46:~$ cd src/cprog
lab46:~/src/cprog$ ls
Feb14    Feb2.c    JAN26_printf_examples.c  JAN31_pointers3.c
Feb14.c  Feb7.c    JAN26_printf_pointers.c  MessingAround
Feb16    Feb7x0.c  JAN31_pointers.c         Projects
Feb16.c  Feb9.c    JAN31_pointers2.c        out.txt
lab46:~/src/cprog$ 

5: Local Host

Definition

The device that is running the session.

Demonstration

My laptop is running Ubuntu. The terminal on my laptop screen is an example of a local host, because my device (the laptop) is what the services are being ran on.

6: Remote Host

Definition

A remote host is a device that is running services remotely.

Demonstration

My laptop is connected to lab46.corning-cc.edu via ssh, which is an example of a remote host.

7. The VI editor

Definition

The VI editor is a text editor where in one mode you can always insert, and in another mode you enter commands. To toggle command mode you hit escape, and to enter insert mode you push i when in command mode.

Demonstration
lab46:~/src/cprog$ vim Hello.txt
lab46:~/src/cprog$ cat Hello.txt
I couldn't type until I hit I to enter insert mode.

Now, to exit VI editor (and save the work) I will hit the ESCAPE key to enter command mode, from there I will type :wq to quit and save my work.  If I didn't want to save, I would use :q!
lab46:~/src/cprog$ 

After typing vim Hello.txt, this appeared on my screen.

I couldn't type until I hit I to enter insert mode.

Now, to exit VI editor (and save the work) I will hit the ESCAPE key to enter command mode, from there I will type :wq to quit and save my work.  If I didn't want to save, I would use :q!
~                                                                               
~     

8: Text Processing

Definition

Manipulating text

Demonstration of the chosen keyword.

You can print the contents of a file using the cat command. You can manipulate text using many things. The nano and vim text editors are really good for editing text based files.

Just nano filename.txt or vim to open the editor.

unix Objective

unix Objective

Familiarity with the structure of UNIX systems.

Definition

Basically this means that I will become much more familiar with how UNIX systems look like, like the file structure and whatnot.

Method

If I can tell if a system is based off the UNIX system based on the file structure.

Measurement

I think I can do this pretty well, as I can recognize that mobile phones (such as my android powered phone and my previous iPhone) are based off of UNIX because of their file structure. They have the files named var, usr, bin, and others.

Analysis

I think I did pretty well. Not particularly, unless if there is some other discrepancy that I am unaware of. I don't think so. If there was a quick description associated with the objective.

Experiments

Change base 8 to base 10

Question

My experiment is to create a program that will convert any 3 digit base 8 number to base 10, and vice versa.

Resources

http://www.unitconversion.org/numbers/base-10-to-base-8-conversion.html – To double check program once I begin testing it.

http://science.widener.edu/~schultz/mathhelp2.html – This will help me develop the logic to run behind the program.

Hypothesis

I'm guessing that by the time I finish, I will have a program that can accomplish the goal.

Experiment

See if the program will output correct data given an input (checked against the converter I found online).

Data

Program so far:

  GNU nano 2.2.4                                                                                                                        File: Algorithm.c                                                                                                                                                                                                                                            Modified  
 
=#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
 
        int number = 0;
        int array[3];
        int count = 0;
        int random = 0;
        int result = 0;
        int choice;
 
        printf("This program will convert any three digit number to a different base\n");
 
        printf("For base 8 to base 10, press 1\n");
        printf("For base 10 to base 8, press 2\n");
 
        scanf("%d", &choice);
 
        if(choice == 1)
        {
 
                printf("\nEnter a base 8, three digit number :");
                scanf("%d", &number);
                printf("\nThat number will now be converted to base 10\n");
                printf("\nThe number you input was: %d\n", number);
 
                for(count = 0; count <=2; ++count)
                {
                        random = number%10;
 
                        array[count] = random;
 
                        number = (number-random)/10;
                }
 
                for(count = 2; count >=0; --count)
                {
                        result = result + array[count]*(pow(8, count));
                }
 
                printf("\nThat number in base 10 is %d\n", result);
        }
 
        if(choice == 2)
        {
 
                printf("Enter a base 10 number: ");
                scanf("%d", &number);
                printf("\nThat number will not be converted to base 8\n");
                printf("\nThe number you input was: %d\n", number);
 
                for(count = 0; count <=2; ++count)
                {
 
 
                        random = number%8;
 
                        array[count] = random;
 
                        number = (number-random)/8;
                }
 
                printf("\nThat number in base 8 is: ");
 
                for(count = 2; count >=0; --count)
                {
                        printf("%d", array[count]);
                }
 
                printf("\n");
        }
 
        return (0);
}

This code can successfully convert from base 8 to base 10.

When it comes to converting from base 10 to base 8 however, there is no check to take into account that the numbers 8 and 9 don't exist in base 8, (they have to carry over). This is something that will have to be incorporated. Perhaps as a second experiment it would be good to do that, with some more menu options and invalid input coding added in.

Analysis

My hypothesis was mostly correct. The program mostly works. It still needs some safety nets built into it, however.

Conclusions

I know the language better.

One major thing that I didn't realize was that when you input a number, you have to input it by reference (denoted by an &) if you wish to make changes to that number as the program progresses.

Directory navigation

Question

I know that I can change directory from one end of the file system tree to the other by stating my destination as the entire file tree starting from the root. Can I do it if I only do part of the entire file tree that I wish to get to?

Resources

No resources consulted.

Hypothesis

I don't think it will work, but if UNIX has some sort of measure in place then I wouldn't be surprised by that.

I don't think it will work because if you don't specify the entire file tree starting from the root, then UNIX wouldn't know for sure what you're talking about.

Experiment

I'm going to try to change from two different directories, first by using the file tree from the root, and then by using only part of it.

Data

lab46:/$ cd home/smalik2/src/cprog/Projects
lab46:~/src/cprog/Projects$ cd /
lab46:/$ cd src/cprog/Projects
-bash: cd: src/cprog/Projects: No such file or directory
lab46:/$ 

Analysis

My hypothesis was correct, and applicable.

Conclusions

If you want to change directories more then one level at a time, you have to start by specifying the file system from the root (or starting from your current location).

Change base 10 to base 8

Question

I am trying to figure out what is wrong with the code for experiment 1, where it will randomly return an incorrect value for a base 10 to base 8 conversion.

Resources

No other resources were consulted, other than the ones from Experiment 1.

Hypothesis

I think it's a problem with carrying over a digit, because going from base ten to base 8, base 8 requires more symbols to represent the same number as in base 10, so I may need an additional placeholder.

Experiment

Try to see if I can incorporate some logic to carry over a 1.

Data

New and improved code for Algorithm program
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
 
        int number = 0;
        int array[4];
        int count = 0;
        int random = 0;
        int result = 0;
        int choice;
 
        printf("This program will convert any three digit number to a different base\n");
 
        printf("For base 8 to base 10, press 1\n");
        printf("For base 10 to base 8, press 2\n");
 
        scanf("%d", &choice);
 
        if(choice == 1)
        {
 
                printf("\nEnter a base 8, three digit number :");
                scanf("%d", &number);
                printf("\nThat number will now be converted to base 10\n");
                printf("\nThe number you input was: %d\n", number);
 
                for(count = 0; count <=2; ++count)
                {
                        random = number%10;
 
                        array[count] = random;
 
                        number = (number-random)/10;
                }
 
                for(count = 2; count >=0; --count)
                {
                        result = result + array[count]*(pow(8, count));
                }
 
                printf("\nThat number in base 10 is %d\n", result);
        }
 
        if(choice == 2)
        {
 
                printf("Enter a base 10 number: ");
                scanf("%d", &number);
                printf("\nThat number will not be converted to base 8\n");
                printf("\nThe number you input was: %d\n", number);
 
                for(count = 0; count <=2; ++count)
                {
 
 
                        random = number%8;
 
                        array[count] = random;
 
                        number = (number-random)/8;
                }
 
                array[3] = number;
 
                printf("\nThat number in base 8 is: ");
 
                for(count = 3; count >=0; --count)
                {
                        printf("%d", array[count]);
                }
 
                printf("\n");
        }
 
        return (0);
}
Program running

The problem from experiment one was that with larger numbers, the program wouldn't work. It could convert 511 to 777, but fail to convert 777 to 1411. Now it does.

lab46:~/src/cprog/MessingAround$ ./Algorithm
This program will convert any three digit number to a different base
For base 8 to base 10, press 1
For base 10 to base 8, press 2
2
Enter a base 10 number: 777

That number will not be converted to base 8

The number you input was: 777

That number in base 8 is: 1411

Analysis

My hypothesis was correct.

It wasn't necessarily a need to carry over a one, as the number%8 always returned a value between 0 and 7 (inclusive). However, if the final number that I end with is a 1, then that means I need a 1 in the thousands place. Which was a very easy fix.

Conclusions

This experiment was very good at developing troubleshooting, and forcing me to think on a very logical level.

opus/spring2012/smalik2/part1.txt · Last modified: 2012/03/02 21:04 by smalik2