User Tools

Site Tools


opus:spring2012:swilli31:part1

Part 1

Entries

Entry 1: January 31, 2012

Today we played more with pointers. We wrote another program which delved into the realm of double pointers. I think I understand pointers but I may be over-simpplifing them. Esssentialy, they direct the computer to another location. When called to give the information contained in them they return the value of what they are pointing to.

Entry 2: Febrauary 2, 2012

Todday we wrote a game in class. We used some elements of things that we used in previous classes as well as looked into loops. We also looked at the rand function which is supposed to resemble a random number generator but doesn't do it very well. We did go through and fix the problem which did make it more random. We also added a loop into the code which looped the game until the user decides to quit. I understand this program and what it is doing, which is a good sign. I haven't gotten lost in the class yet which is always a hopeful start.

Entry 3: February 7, 2012

Today we looked at arrays. My previous experince with arrays is limited to the theoretical realm. I understood that arrays needed to be sized before you start to fill them but I never was quite sure how to actually create them in a program. Today's class helped. I am still not 100% comfortable with creating them on my own but I do see how it can be done.

Entry 4: February 25, 2012

Today Project 1 was due and it was rather more challenging than previously thought. I understand the logic of how to do the program and I understand what needs to be done on the project, however, I am struggling with the actual implementation of the code. When I sit to write the code I essentially draw a blank and have no idea where to begin. I believe that if I sit down more with a pseudocode and play more with syntax and read source material on how to code in C I can overcome this dilemma. Otherwise, this will be a very difficut semester.

Keywords

cprog Keywords

Standard I/O

(STDIO, STDOUT, STDERR)

Definition

The main libraries in which C programs run

Demonstration
#include <stdio.h>

Logic Operators

AND, OR, NOT, XOR

Definition

Specifies the parameters of the loop and determines if the loop should execute

Demonstration
while((x!='\0')&&(x!='\n'))

This is an AND operator in a program

Typecasting

Definition

taking one form of variable and making it fit into another form of variable

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 *p1, *p2, p3, **p4;
        p1=(int*)malloc(sizeof(int)*1);
        *p1=26;
        printf("*p1 is %u\n", *p1);
        p2=p1;
        p4=&p1;
        printf("**p4 is %u\n",**p4);
        printf("*p2 is %u\n",*p2);
        *p2=61;
        printf("*p1 is %u\n", *p1);
        printf("**p4 is %u\n", **p4);
        printf("*p2 is %u\n", *p2);
        return(0);
}

This allocated memory in the size of an int

Pointers

Definition

Sets the memory address for something

Demonstration
#include <stdio.h>
 
int main()
{
        int a=0;
        int *b;
        b=&a;
        *b=12;
        printf("a contains %u\n", a);
        printf("a's address is 0x%x\n", &a);
        printf("b contains %u\n", *b);
        printf("b points to 0x%x\n", b);
        printf("b's address is 0x%x\n", &b);
        return(0);
}

b is a pointer to a

Arrays

Definition

Holds many different elements of the same data type at one address

Demonstration
#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);
        printf("Debug A1\n");
        while((x!='\0')&&(x!='\n'))
        {
                printf("in the while, x is %hhd\n",x);
                len++;
                pos++;
                x=*(word+pos);
        }
        printf("Debug B\n");
        for(pos=0; pos<len;  pos++)
        {
                fprintf(stdout, "%c", *(word+pos)-32);

File Access

read, write append

Definition

Creating a file and reading it into a program or writing the output of a program to a file

Demonstration
#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);
        n, *out;
        char value=0;
        in=fopen("file.txt", "r");
        out=fopen("out.txt", "w");
        if(in==NULL)
        if(in==NULL)
        if(in==NULL)
}
        fscanf(in, "%hhd", &value);
        while(value !=-1)
         {
                value *=2;
                fprintf(out,"%hhd\n", value);
                fscanf(in, "%hhd", &value);
        }
        fclose(in);
        fclose(out);
        return(0);
}

Repetiion/iteration structures

for, while, do while

Definition

These are loops that govern how many times a program will execute a certain task

Demonstration
/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    int v;
    while v!=0
    {
       printf("Rise against the machines\n");
    }
    else
    {
       printf(The machines have won\n")
    }
    return(0);
}

Header Files

Libraries

Definition

These are the #include statements at the beginning of a program. They are needed so that certain commands can be understood by the compiler to run a program

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

There are others but these are just a few

cprog Objective

cprog Objective

comprehend the basics of memory management, data representation and storage

Definition

Can the student understand how to allocate memory and represent what they wish to represent with the proper file type? Does the student understand how to store values in C?

Method

Explain the code of project 0

Measurement

The code of project 0 is defining the size of the storage available for the different types of arguments in C. Each storage size dictates how much memory needs to be allocated for that type and whether or not datat will fit into that storage type.

Analysis

I understand the logic behind most codes but will need to continue to work on writing the actual code.

Experiments

Experiment 1

Question

Can a program check the upper and lower bounds

Resources

Class notes and book

Hypothesis

Yes. The program can check the upper

Experiment

Write a code

Data

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

int main(int argc, char **argv)
{
        signed int input;
        char column=0;
        char row=0;
        printf("Enter a value (0-4): ");
        scanf("%d", &input);
        while((input<4) && (input>0))
        {
                printf("argv[%hhu]: %c\n", input, *(*(argv+row)+column));
                printf("You ran this program with %hhu arguments, they are:\n",argc);
                exit(1);
        }


        printf("You have input an invalid value\n");

        return(0);
}

Analysis

I know that the code should work but I am unable to get the right syntax to compile it.

Conclusions

I need to spend more time learning syntax.

Experiment 2

Question

What happens if you put a semicolon on the end of a loop statement

Resources

the book was consulted

Hypothesis

The program won't compile

Experiment

I will add a semicolon on the end of a loop statement in a program and see what happens

Data

The program didn't compile. The loop statement was treated like another regular statement in the program which threw the whole program off.

Analysis

The hypothesis was correct although I could have gone much further into figuring out why it would not compile. The hypothesis short cited and lacks depth.

Conclusions

Although the program behaved as expected it could have been explore further.

Experiment 3

Question

What types of things create a segmentation fault

Resources

class programs and book

Hypothesis

Faulty syntax will cause a seg fault

Experiment

Play with codes already written

Data

I found that mainly a logic error in the loops will give a seg fault

Analysis

Based on the data collected: The hypothesis was not entirely correct. The hypothesis was very vague as was the question posed. If The hypothesis could have been slightly more specific the experiment might have had a little more guidance.

Conclusions

The main cause of segmentation faults are logic errors but logic errors also lead to other types of problems within the program.

opus/spring2012/swilli31/part1.txt · Last modified: 2012/03/03 00:51 by swilli31