User Tools

Site Tools


opus:fall2011:dherman3:part1

Part 1

Entries

September 25, 2011

Up to today, I have read Chapters 1 & 2 and gone through most (if not all) of the exercises. This helped introduce me to many of the starting concepts of C/C++ programming that I have not known before. A few concepts did not make perfect sense to me. This includes what the Data Types really are and Type Conversions. There are a few challenges that I face related to the course. One of the challenges I face is that I have no experience whatsoever (besides Object-oriented and Structured Problem Solving) with any programming languages or programming itself. Another of the challenges I face is the limited time I have given myself to deal with both classes at CCC and my job.

September 27, 2011

I have begun reading Chapter 3 and worked some on the first project. Getting my first project completed is very important as it opens up possibilities to more projects. I have had some trouble remembering how the data types are calculated (long long int, unsigned int, etc.) and will have to refresh my memory with some studying.

September 28, 2011

Today, I ran a few experiments and recorded them into my Opus. I also read about unsigned/signed chars, long long ints, etc. The experiments can be used in many different ways, primarily to figure out different possibilities of the C language in different situations. The char and int data values are important parts of my project and knowing what they are and how to calculate them is very important. The base concepts for them does not seem very logical to me and is somewhat of a challenge.

September 29, 2011

Today, I read more in Chapter 3, about switches and loops. I have already encountered loops before in O.O.&S. Problem Solving, but have not seen switches before. The switch seems like the if statement, but with a lot more possibilities being able to set more conditions and therefore more possible results. Using the Switch can help to simplify a problem if the problem included a lot of conditions that needed to be tested. Using if statements in this type of problem would be much more unnecessary coding.

Topics

Standard I/O

Standard I/O refers to the communication between the input of the keyboard into the command line, and the output in Unix. Standard I/O simplifies the communications into three categories:

STD IN - Standard Input is where the input is read.

STD OUT - Standard Output is where the output is sent to the command window of the user.

STD ERROR - Standard Error is similar to output, but is used as a way to differentiate normal output from errors.

Header Files

Header Files are usually used in C/C++ programs for declarations and definitions.

Here's an example of this:

#include <stdio.h>

This calls stdio.h to be used in said program as a resource.

Arithmetic

Arithmetic is the counting and calculating of numbers, quite simply. Some parts of Arithmetic include addition, subtraction, multiplication, and division. These are the basic functions of Arithmetic. Arithmetic is incorporated into programming languages to save much time for the user. Arithmetic is a core part of how programming works and why it exists.

Logic and Operators

Logic is an important part of problem solving in any program. Operators are incorporated into the C program in the form of:

  • AND
  • OR
  • NOT
  • XOR

These operators check for conditions to be true. The user can direct the results for different conditions with these operators.

Variables

Variables are used to store data which can be manipulated or retrieved in the program. Variables are essential to allow many calculations to take place. The programmer can choose to make variables be initialized at a certain value, or to have a user enter a value to be stored and possibly calculated.

Format/Formatted Text String (or "String")

A Formatted Text String can be used to limit a variable to only accept certain characters as valid.

Scope

The scope in a program dictates where a variable in the program can be accessed.

Pointers

A pointer is a data type in a program used to improve performance by referring directly to another value.

Type Casting

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);
}

Selection Structures

Selection Structures are a way to allow the program to select statements in a different order than sequential. This allows the program to decide what module to call upon based on the input received.

Repetition/Iteration Structures

Repetition/Iteration structures allow the program to repeat parts of the program. Looping is one form of this.

Arrays

Arrays are an arrangement of multiple values. They can be used to store multiple values in an arranged order. An array can be metaphorically thought of as a table of values.

Objectives

Objective 1

The first objective is to demonstrate structured and object-oriented problem solving concepts. Structured and Object-oriented problem solving is a logical form of solving a program to produce a desired result.

Method

The method I will be using to demonstrate my knowledge of this topic is to state multiple solutions to a problem. Using different solutions allows for more demonstration of the knowledge of the concept.

The problem I will be solving is to test for condition A and produce result B if it is true, and show the possible results while doing so.

Measurement

One possible way of testing for condition A is to run an 'if' statement. For example: If A > 0, then B = 2. This is a basic condition check which will set B to 2 if the value of A is greater than 0. An if statement is usually used to set values in this manner.

Another possible way of testing for condition A is to run a 'for' statement. For example: For A = [1 to 10],

 B[Index] = A + 2

endfor This condition check uses A as an array to set values in the array B to A + 2.

Another possible way of testing for condition A is to run a 'while' statement. For example: A = 0 While A < 10

 B[A] = A/2
 A = A + 1

endwhile This condition checks if A is less than 10, and if it is, assigns a value to the A value of the array B, and increases A by one.

Analysis

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

  • How did you do?

I was able to show some of my knowledge in Structured and Object-Oriented Problem Solving.

  • Room for improvement?

There is much more that I could do to show my knowledge of this type of problem solving, such as using modules and more complicated problems.

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

The measurement process I used was very long and could have been shorter.

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

Shortening the measurement process could be efficient if I was able to come up with shorter answers.

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

The course objective could not be altered as it is completely applicable to the course.

Experiments

Experiment 1

Question

What happens when you remove the parenthesis in a printf statement?

Resources

The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie, 2nd Ed., Chapter 1

Hypothesis

The program will not run correctly and the printf statement will not output correctly.

Experiment

To test this hypothesis, I will run the “Hello World” program first with, then without parenthesis in the printf statement.

Data

This is the original program:

#include <stdio.h>
 
main()
 
{
 
        printf("hello, world\n");
 
}

This is the output from this program:

lab46:~/test/experiments$ ./test0.out
hello, world

This is the program after removing parethensis:

#include <stdio.h>
 
main()
 
{
 
        printf"hello, world\n";
 
}

This is the output from trying to compile the program:

lab46:~/test/experiments$ cc test0.c
test0.c: In function 'main':
test0.c:7: error: expected ';' before string constant

Analysis

Based on the data collected:

My hypothesis was partially correct.  The program was unable to compile, and therefore was unable to run.

Conclusions

The parenthesis are very important to the printf function's syntax.  
Without the parenthesis, the program will be unable to compile.

Experiment 2

Question

What happens when you use a declared variable in a statement without initializing it beforehand?

Resources

The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie, 2nd Ed., Chapter 1

Hypothesis

The program will run but will produce different results.

Experiment

I will be using the Fahrenheit experiment to test how the program runs with and without initializing the lower bound.

Data

The program with the lower bound initialized:

#include <stdio.h>
 
/* print Fahrenheit-Celsius table
    for fahr = 0, 20, ..., 300 */
main()
{
        int fahr, celsius;
        int lower, upper, step;
 
        lower = 0;      /* lower limit of temperature table */
        upper = 300;    /* upper limit */
        step = 20;      /* step size */
 
        fahr = lower;
        while (fahr <= upper) {
                celsius = 5 * (fahr-32) / 9;
                printf("%d\t%d\n", fahr, celsius);
                fahr = fahr + step;
        }
}

This is the output of the compiled program:

lab46:~/test/experiments/exp2$ ./test0.out
0       -17
20      -6
40      4
60      15
80      26
100     37
120     48
140     60
160     71
180     82
200     93
220     104
240     115
260     126
280     137
300     148

This is the program after the removal of the initialization of the lower bound:

#include <stdio.h>
 
/* print Fahrenheit-Celsius table
    for fahr = 0, 20, ..., 300 */
main()
{
        int fahr, celsius;
        int lower, upper, step;
 
        upper = 300;    /* upper limit */
        step = 20;      /* step size */
 
        fahr = lower;
        while (fahr <= upper) {
                celsius = 5 * (fahr-32) / 9;
                printf("%d\t%d\n", fahr, celsius);
                fahr = fahr + step;
        }
}

This is the output of the compiled program:

lab46:~/test/experiments/exp2$ ./test1.out
lab46:~/test/experiments/exp2$

Analysis

My hypothesis was partially correct. The program did run, but produced no results at all, instead of different results.

Conclusions

Removing the initialization of the lower bound in this case caused the program to output nothing.

Experiment 3

Question

What happens when you enter two statements on one line, instead of on separate lines?

Resources

The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie, 2nd Ed. Chapter 1.

Hypothesis

The program will not compile correctly as it will not recognize the statement. Including 2 statements in one line will create an error.

Experiment

I am going to compile and run a program with 2 statements in 2 lines and 2 statements in 1 line.

Data

Program with two statements in two lines like usual:

#include <stdio.h>
 
#define LOWER 0         /* lower limit of table */
#define UPPER 300       /* upper limit */
#define STEP  20        /* step size */
 
/*print Fahrenheit-Celsius table */
main()
{
        int fahr;
 
        for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
                printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

Output for the program:

lab46:~/test/experiments/exp3$ ./test1.out
  0  -17.8
 20   -6.7
 40    4.4
 60   15.6
 80   26.7
100   37.8
120   48.9
140   60.0
160   71.1
180   82.2
200   93.3
220  104.4
240  115.6
260  126.7
280  137.8
300  148.9

Program with two statements in one line:

#include <stdio.h>
 
#define LOWER 0         /* lower limit of table */
#define UPPER 300       /* upper limit */
#define STEP  20        /* step size */
 
/*print Fahrenheit-Celsius table */
main()
{
        int fahr;
 
        for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)printf("%3d %6.1f\$
}

Output for the program:

lab46:~/test/experiments/exp3$ ./test2.out
  0  -17.8
 20   -6.7
 40    4.4
 60   15.6
 80   26.7
100   37.8
120   48.9
140   60.0
160   71.1
180   82.2
200   93.3
220  104.4
240  115.6
260  126.7
280  137.8
300  148.9

Analysis

My hypothesis was incorrect. The output was the same for both programs.

Conclusions

Having more than one statement on the same line will not change the output. The amount of space between statements does not matter, as long as they are in the correct syntax.

opus/fall2011/dherman3/part1.txt · Last modified: 2011/10/01 16:12 by wedge