User Tools

Site Tools


opus:spring2013:khoose2:journal

cprog Journals

February 1st,2013

Today we covered the concept of varying variable sizes, used when storing values of varying size. For example small numbers need less memory/space then large numbers, this is used to save memory in the long run. These different variable sizes are as follows:

          
  - unsigned char
  - signed char
  - unsigned short int
  - signed short int
  - unsigned int
  - signed int
  - unsigned long int
  - signed long int
  - unsigned long long int
  - signed long long int

The final version of the program that displays data on these variable is shown below:

  1 #include <stdio.h>
  2 int main()
  3 {
  4     unsigned char a=0;
  5     fprintf(stdout, "\n---------------\n Unsigned Char:\n---------------\n");
  6     fprintf(stdout, "Low: %hhu\n", a);
  7     a=a-1;
  8     fprintf(stdout, "High: %hhu\n" , a);
  9     fprintf(stdout, "The size of an unsigned char in %hhu bytes\n", sizeof (a));
 10 
 11     signed char b=(a/2);
 12     fprintf(stdout, "\n-------------\n Signed Char:\n-------------\n");
 13     b=b+1;
 14     fprintf(stdout, "Low: %hhd\n", b);
 15     b=b-1;
 16     fprintf(stdout, "High: %hhd\n" , b);
 17     fprintf(stdout, "The size of an signed char in %hhd bytes\n", sizeof (b));
 18 
 19     unsigned short int c=0;                                                                                                             
 20     fprintf(stdout, "\n--------------------\n Unsigned Short Int:\n--------------------\n");
 21     fprintf(stdout, "Low: %hu\n", c);
 22     c=c-1;
 23     fprintf(stdout, "High: %hu\n" , c);
 24     fprintf(stdout, "The size of an unsigned short int in %hu bytes\n", sizeof (c));
 25 
 26     signed short int d=(c/2);
 27     fprintf(stdout, "\n------------------\n Signed Short Int:\n------------------\n");
 28     d=d+1;                                                                                                                              
 29     fprintf(stdout, "Low: %hd\n", d);
 30     d=d-1;
 31     fprintf(stdout, "High: %hd\n" , d);
 32     fprintf(stdout, "The size of an signed short int in %hd bytes\n", sizeof (d));
 33 
 34     unsigned int e=0;
 35     fprintf(stdout, "\n--------------\n Unsigned Int:\n--------------\n");
 36     fprintf(stdout, "Low: %u\n", e);
 37     e=e-1;
 38     fprintf(stdout, "High: %u\n" , e);
 39     fprintf(stdout, "The size of an unsigned int in %u bytes\n", sizeof (e));
 40 
 41     signed int f=(e/2);
 42     fprintf(stdout, "\n------------\n Signed Int:\n------------\n");
 43     f=f+1;
 44     fprintf(stdout, "Low: %d\n", f);
 45     f=f-1;
 46     fprintf(stdout, "High: %d\n" , f);
 47     fprintf(stdout, "The size of an signed int in %d bytes\n", sizeof (f));
 48 
 49     unsigned long int g=0;
 50     fprintf(stdout, "\n-------------------\n Unsigned Long Int:\n-------------------\n");
 51     fprintf(stdout, "Low: %lu\n", g);
 52     g=g-1;
 53     fprintf(stdout, "High: %lu\n" , g);
 54     fprintf(stdout, "The size of an unsigned long int in %lu bytes\n", sizeof (g));
 55 
 56     signed long int h=(g/2);
 57     fprintf(stdout, "\n-----------------\n Signed Long Int:\n-----------------\n");
 58     h=h+1;
 59     fprintf(stdout, "Low: %ld\n", h);
 60     h=h-1;
 61     fprintf(stdout, "High: %ld\n" , h);
 62     fprintf(stdout, "The size of an signed long int in %ld bytes\n", sizeof (h));
 63 
 64     unsigned long long int i=0;
 65     fprintf(stdout, "\n------------------------\n Unsigned Long Long Int:\n------------------------\n");
 66     fprintf(stdout, "Low: %llu\n", i);
 67     i=i-1;
 68     fprintf(stdout, "High: %llu\n" , i);
 69     fprintf(stdout, "The size of an unsigned long long in %llu bytes\n", sizeof (i));
 70 
 71     signed long long int j=(i/2);
 72     fprintf(stdout, "\n----------------------\n Signed Long Long Int:\n----------------------\n");
 73     j=j+1;
 74     fprintf(stdout, "Low: %lld\n", j);
 75     j=j-1;
 76     fprintf(stdout, "High: %lld\n" , j);

Resulting in the following values:

---------------
 Unsigned Char:
---------------
Low: 0
High: 255
The size of an unsigned char in 1 bytes

-------------
 Signed Char:
-------------
Low: -128
High: 127
The size of an signed char in 1 bytes

--------------------
 Unsigned Short Int:
--------------------
Low: 0
High: 65535
The size of an unsigned short int in 2 bytes

------------------
 Signed Short Int:
------------------
Low: -32768
High: 32767
The size of an signed short int in 2 bytes

--------------
 Unsigned Int:
--------------
Low: 0
High: 4294967295
The size of an unsigned int in 4 bytes

------------
 Signed Int:
------------
Low: -2147483648
High: 2147483647
The size of an signed int in 4 bytes

-------------------
 Unsigned Long Int:
-------------------
Low: 0
High: 18446744073709551615
The size of an unsigned long int in 8 bytes

-----------------
 Signed Long Int:
-----------------
Low: -9223372036854775808
High: 9223372036854775807
The size of an signed long int in 8 bytes

------------------------
 Unsigned Long Long Int:
------------------------
Low: 0
High: 18446744073709551615
The size of an unsigned long long in 8 bytes

----------------------
 Signed Long Long Int:
----------------------
Low: -9223372036854775808
High: 9223372036854775807
The size of an signed long long int in 8 bytes

February 6th,2013

Today we made a program that looks at why chars might be called chars and not short short ints.


#include <stdio.h>
 
int main()
{
    char a, b, c;
 
    a = 0x41;
    b = 97;
    c = '1';
 
    printf("a is %hhu but can be mapped to '%c' in ASCII\n", a, a);
    printf("b is %hhu but can be mapped to '%c' in ASCII\n", b, b);
    printf("c is %hhu but can be mapped to '%c' in ASCII\n", c, c);
 
    a = a + 1;
    b = b - ' ';
    c = c * 2;   // this is a *, we are multiplying
 
    return(0);
}

We also made a program that plays a bit more with memory.


#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a, *b;
 
    a = 'a'; // what numeric value is being stored in the variable a? Why?
    b = &a;
 
    printf("a contains     '%c'\n", a);
    printf("a's address is 0x%X\n", &a);
    printf("-----------------------\n");
    printf("b dereferenced contains '%c'\n", *b);
    printf("b contains     0x%X\n", b);
    printf("b's address is 0x%X\n", &b);
 
    return(0);
}

Also a third and final program that digs a little deeper with memory.


#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char *a;
 
    a = (char *) malloc (sizeof(char));
 
    *a = 48;
 
    //fprintf(stdout, "*a is %c\n", *a);
 
    return(0);
}

February 15, 2013

Today we covered the use of arrays in our functions, we were given a somewhat complete program and were told to finish it. The goal was to make it capable of averaging an array of numbers, resulting in the following code.

  1 #include <stdio.h>                                                          
  2 #include <stdlib.h>
  3 
  4  #define NUM_SCORES 5
  5  #define SCORE0 87
  6  #define SCORE1 92
  7  #define SCORE2 97
  8  #define SCORE3 83
  9  #define SCORE4 79
 10 
 11  int main()
 12  {
 13      unsigned char *scores;
 14      float average;
 15 
 16      scores = (unsigned char *) malloc (sizeof(unsigned char) * NUM_SCORES);
 17      *(scores+0) = SCORE0;
 18      *(scores+1) = SCORE1;
 19      *(scores+2) = SCORE2;
 20      *(scores+3) = SCORE3;
 21      *(scores+4) = SCORE4;
 22 
 23      average=((float)(SCORE0+SCORE1+SCORE2+SCORE3+SCORE4)/NUM_SCORES);
 24      fprintf(stdout, "Your scores are: %hhu, %hhu, %hhu, %hhu, %hhu\n",SCORE    0, SCORE1, SCORE2, SCORE3, SCORE4);
 25      fprintf(stdout, "Your average is: %f\n",average);
 26      /* Please provide the equation to do average of the scores */
 27      //average = EQUATION TO DO AVERAGE
 28 
 29      /* Display the scores via array referencing and then the average.
 30       * %f can be used to display a floating point value. */
 31      //fprintf(stdout, "YOUR FORMATTED TEXT STRING", LIST OF VARIABLES);
 32 
 33      return(0);
 34 }
Resulting in:
lab46:~/src/C+$ ./pointerarray 
Your scores are: 87, 92, 97, 83, 79
Your average is: 87.599998
lab46:~/src/C+$ 

Next we covered scanf() and using it to assign input to given variables. Below is a program that was modified in order to work correctly. The 'char' scanf() will grab the new line from the entry before unless properly dealt with as so:

  1 #include <stdio.h>                                                          
  2 #include <stdlib.h>
  3 
  4 int main()
  5 {
  6     int a, *b;
  7     short int c;
  8     char d;
  9     float e;
 10 
 11     b = (int *) malloc (sizeof(int) * 1);
 12 
 13     fprintf(stdout, "Enter an integer: ");
 14     fscanf(stdin, "%u", &a);
 15 
 16     fprintf(stdout, "Enter another integer: ");
 17     fscanf(stdin, "%u", b);// why don't we need & in this case?
 18 
 19     fprintf(stdout, "Enter a short int: ");
 20     fscanf(stdin, "%hu", &c);
 21 
 22 
 23     fprintf(stdout, "Enter a char: ");
 24    // fscanf(stdin, "%c", &d);
 25 getchar();
 26 fscanf(stdin, "%hu", &d);
 27 
 28     fprintf(stdout, "Enter a float value: ");
 29     fscanf(stdin, "%f", &e);
 30 
 31     fprintf(stdout,  "Your ints are: %u and %u\n", a, *b);
 32     fprintf(stdout, "Your short int is: %hu\n", c);
 33     fprintf(stdout, "Your char is: '%hhu'\n", d);
 34     fprintf(stdout, "Your float (trimmed at 2 decimal places) is: %.2f\n", e    );
 35 
 36     return(0);
 37 }
Supplying the following output:
lab46:~/src/C+$ ./scanf
Enter an integer: 4
Enter another integer: 3
Enter a short int: 2
Enter a char: a
Enter a float value: Your ints are: 4 and 3
Your short int is: 2
Your char is: '0'
Your float (trimmed at 2 decimal places) is: 0.00
lab46:~/src/C+$ 

February 20, 2013

Today we worked on one program, and beginning a second. The first of these programs uses fgetc() to read a single character from a specified file pointer. This program converts character input into its ASCII equivalent:

  1 #include <stdio.h>                                                          
  2 
  3 int main()
  4 {
  5     char a, b, c, d;
  6 
  7     fprintf(stdout, "Enter a Char: ");
  8     a = fgetc(stdin);
  9 
 10 
 11     fprintf(stdout, "Enter a Char: ");
 12     getchar();
 13     b = fgetc(stdin);
 14 
 15     fprintf(stdout, "Enter a Char: ");
 16     getchar();
 17     c = fgetc(stdin);
 18 
 19     fprintf(stdout, "Enter a Char: ");
 20     getchar();
 21     d = fgetc(stdin);
 22 /* 
 23 if((a>=48)&&(a<=57));
 24 {
 25 a=a-48;
 26 }
 27 */
 28 fprintf(stdout, "You entered ASCII chars %hhu, %hhu, %hhu, and %hhu\n", a, b, c, d);
 29 
 30 return(0);
 31 }
 32 
Output:
lab46:~/src/C+$ ./fgetc 
Enter a Char: a
Enter a Char: b
Enter a Char: c
Enter a Char: d
You entered ASCII chars 49, 98, 99, and 100
lab46:~/src/C+$ 

The second we started working on is the program below, which needs tweaking.

 1: #include <stdio.h>
 2: #include <stdlib.h>
 3:
 4: int main()
 5: {
 6:     char *initials, i = 0;
 7:
 8:     name = (char *) malloc (sizeof(char) * 4);
 9:
10:     fprintf(stdout, "Enter a 3 character set of initials and hit ENTER: ");
11:     *(initials+i) = fgetc(stdin);
12:     i = i + 1;
13:     *(initials+i) = fgetc(stdin);
14:     i = i + 1;
15:     *(initials+i) = fgetc(stdin);
16:     i = i + 1;
17:     *(initials+i) = fgetc(stdin);
18:
19:     fprintf(stdout, "You entered %s\n", initials);
20:
21:     return(0);
22: }

February 22, 2013

Loop

Today we covered Loops in general, today we covered for loops, shown in the following code and output.

  1 #include<stdio.h>                                                                                                                       
  2 #include<stdlib.h>
  3 
  4 int main()
  5 {
  6     int a;
  7         for(a=12;a>=0;a=a-1)
  8     {
  9         if (a==10)
 10         {
 11         fprintf(stdout,"Friggin ");
 12         fprintf(stdout,"%d\n", a);
 13         }
 14         else if (a==0)
 15         {
 16         fprintf(stdout,"BOOOOMMMM EXPLOSION GRAPHICS! (imagined)\n");
 17 }
 18         else if (a!=6)
 19 fprintf(stdout,"%d\n", a);
 20     }
 21     return 0;
 22 }

=Output=

Displayed below is the output of the above program.

lab46:~/src/C+$ ./forloop 
12
11
Friggin 10
9
8
7
5
4
3
2
1
BOOOOMMMM EXPLOSION GRAPHICS! (imagined)
lab46:~/src/C+$ 
Conditional

Next we covered conditionals, using, if, else if, and else. These are single conditions, usually used for specific values, different than while loops that cover a range of values.

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 int main()
  5 {
  6     int a;
  7     fprintf(stdout, "Please input a number:");
  8     fscanf(stdin, "%u", &a);
  9 
 10         if (a<50)
 11         {
 12          fprintf(stdout,"too low\n");
 13         }
 14 
 15         else if (a==50)
 16         {
 17         fprintf(stdout,"equal to\n");
 18         }
 19 
 20         else if (a>50)
 21         {
 22         fprintf(stdout,"too high\n");
 23         }
 24     return(0);
 25 }  

=Output=

Here is an example of my code in action.

lab46:~/src/C+$ ./ifelse 
Please input a number:4
too low
lab46:~/src/C+$ ./ifelse 
Please input a number:50
equal to
lab46:~/src/C+$ ./ifelse 
Please input a number:54
too high
lab46:~/src/C+$ 

February 27, 2013

Today we worked on a random number generator, based off of time. There is no such thing as 'random' with computers since they can only talk in equation so to fix that the following program uses time, seconds, to determine the variable to sets the equation in motion that way there is a new 'random' number generated every second.

Program
  1 #include <stdio.h>                                                                                                                      
  2 #include <stdlib.h>
  3 
  4 int main()
  5 {
  6     int x;
  7 
  8     srand(time(NULL));
  9 
 10     // Pick a pseudorandom number between 1 and 10
 11     //
 12     x = rand()%99+1;
 13 
 14     fprintf(stdout, "The number chosen was: %u\n", x);
 15 
 16     return(0);
 17 
 18 }
 19 
 20 
Output

Here is the output of the above program:

lab46:~/src/C+$ ./randomnum 
The number chosen was: 69
lab46:~/src/C+$ ./randomnum 
The number chosen was: 45
lab46:~/src/C+$ ./randomnum 
The number chosen was: 79
lab46:~/src/C+$ ./randomnum 
The number chosen was: 11
lab46:~/src/C+$ 

March 1, 2013

gcc filesource -g -o filedestination:  compile code as normal, plus the  -g to allow debugging.
gdb ./fieldestination: opens debugging for the indicated file.
(gdb) list:display code of program
(gdb)break main: sets break as the 'main'
(gdb)run: runs program until break
(gdb)step: single step past break, next line, into functions
(gdb)next: jump to next break, next line, past functions on next line
(gdb)continue: continue execution, till break (if program is loop)
(gdb)print'i':prints the value of the variable at that current spot in the program.
(gdb)display 'i': displays the indicated variable everytime it hits the break
(gdb)healp break: gives information on the break function
(gdb)bt: lists functions that have been used.
lab46:~/src/C+$ gcc expo.c -g -o expo
lab46:~/src/C+$ gdb ./expo
GNU gdb (GDB) 7.0.1-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/khoose2/src/C+/expo...(no debugging symbols found)...done.
(gdb) 
(gdb) list
1	#include<stdio.h>
2	
3		int main()
4	{
5		int i,j, sum=0;
6		
7		for(i=0;i<6;i++)
8		{
9			for(j=0;j<5;j++)
10			{
(gdb) list
11			sum=sum+j;
12			}
13		}
14	
15		fprintf(stdout,"sum is %u\n",sum);
16		return 0;	
17	
18	}
(gdb) display j
1: j = 2
(gdb) display i
2: i = 0
(gdb) display sum
3: sum = 3
(gdb) c
Breakpoint 2, main () at class.c:11
11			sum=sum+j;
3: sum = 10
2: i = 1
1: j = 0
(gdb) c
Continuing.

Breakpoint 2, main () at class.c:11
11			sum=sum+j;
3: sum = 10
2: i = 1
1: j = 1
(gdb) c
Continuing.

March 6, 2013

Today we spent creating a multi-file program, utilizing functions and object-file compilation.

Multiple Files

First I created the individual C files to be compiled into my object files; Exponentiator,Increment, Adding, Multiply, Exponent, and Main.

Exponentiator
//exponentiator.h
#ifndef _EXPONENTIATOR_H   //symbol:unique; delcares existence
#define _EXPONENTIATOR_H   //if it doesnt exist, make it

#include <stdio.h>         //declares existence of printf
#include <stdlib.h>

int increment(int);
int addition(int, int);
int multiply(int, int);
int exponent(int, int);

#endif
Increment
//increment.c
#include "exponentiator.h"
int increment(int num)
{
    num++;
    return(num);
}
Adding
//adding.c
#include "exponentiator.h"

int addition(int num1, int num2)
{
	int i;
	for(i=0;i<num2;i++)
	{
		num1=increment(num1);
	}
	return(num1);
}
Multiply
//multiply.c
#include "exponentiator.h"
int multiply(int num1, int num2)
{
	int i, num;
	num=num1;
	for(i=0;i<(num2-1);i=increment(i))
	{
		num1=addition(num1, num);
	}
	return(num1);
}
Exponent
//exponent.c
#include "exponentiator.h"
int exponent(int num1, int num2)
{
    int i, num;
    num=num1;
    for(i=0;i<(num2-1);i=increment(i))
    {
        num1=multiply(num1, num);                                        
    }
    return(num1);
}
Main
//main.c
#include "exponentiator.h"

int main()
{
	printf("incrementing(9) : %u\n", increment(9));
	printf("addition (105+13): %u\n", addition(105, 13));
	printf("multiply (9*10)  : %u\n", multiply(9, 10));
	printf("exponent (2^5)  : %u\n", exponent(2, 5));

	return(0);
}

Next

After I created all the neccessary C files, I then compiled them into object files so I could use them further. To do this I input the following commands:

lab46:~/src/C+/cprog$ gcc -c main.c       
lab46:~/src/C+/cprog$ gcc -o exponentiator *.o

The first line converts the files indicated to object files, this is required for each file. The second line connects all object files, the * indicates anything that ends in “.o”, to the indicated primary file which in this case is the “exponentiator.h”.

Output

When The compiled executable is run, it uses the values present in the main.c, source file.

Here is the main.c file's content.

//main.c
#include "exponentiator.h"

int main()
{
	printf("incrementing(9) : %u\n", increment(9));
	printf("addition (105+13): %u\n", addition(105, 13));
	printf("multiply (9*10)  : %u\n", multiply(9, 10));
	printf("exponent (2^5)  : %u\n", exponent(2, 5));

	return(0);
}

This is the resulting output.

lab46:~/src/C+/cprog/exponentiator$ ./exponentiator
incrementing(9) : 10
addition (105+13): 118
multiply (9*10)  : 90
exponent (2^5)  : 32
lab46:~/src/C+/cprog/exponentiator$ lab46:~/src/C+/cprog/exponentiator$

March 13, 2013

If you open a file for writting, past contents are erased. If you open a file appent, your cursor is moved to the end and inputs from there.

ldd 'executable file': shows the libraries linked to the file. This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.

Today we began working on a multifile program called 'scores' which was composed of four files; main.c, iscores.c, fscores.c, display.c and project.h.

main.c
  1 #include "project.h"                                                        
  2 
  3 int main(int argc, char **argv)
  4 {
  5     FILE *fPtr = NULL;
  6     size = 0;
  7 
  8     if (argc == 1)
  9     {
 10         score = &iscore;
 11         do {
 12             fprintf(stdout, "How many scores do you wish to enter?");
 13             fscanf(stdin, "%d", &size);
 14         } while (size < 1);
 15 
 16         fprintf(stdout, "Input score (0/%d): ", (size-1));
 17     }
 18     else
 19     {
 20         score = &fscore;
 21         fPtr = fopen(*(argv+1), "r");
 22         if (fPtr == NULL)
 23         {
 24             fprintf(stderr, "Error opening '%s'\n", *(argv+1));
 25             exit(1);
 26         }
 27 
 28         fscanf(fPtr, "%d", &size); //first element in datafile is set to siz    e
 29     }
 30 
 31     data = (int *) malloc (sizeof(int) * size);
 32 
 33     score(fPtr);
 34 
 35     display();
 36        if (fPtr != NULL)
 37 
 38     fclose(fPtr);
 39     return(0);
 40 }
iscores.c
  1 #include "project.h"                                                        
  2 
  3 int iscore(FILE *fPtr)
  4 {
  5     int i = 0, status = -1;
  6 
  7     while(i < size)
  8     {
  9         fscanf(stdin, "%d", (data+i));
 10         fprintf(stdout, "Read in a %d ...\n", *(data+i));
 11         i++;
 12         fprintf(stdout, "Input score (%d/%d): ", i, (size-1));
 13     }
 14 
 15     if (i > 0)
 16         status = 0;
 17 
 18     return(status);
 19 }
fscore.c
  1 #include "project.h"                                                        
  2 
  3 int fscore(FILE *fPtr)
  4 {
  5     int i = 0, status = -1;
  6 
  7     while(fscanf(fPtr, "%d", (data+i)) != EOF)
  8     {
  9         fprintf(stdout, "Read in a %d ...\n", *(data+i));
 10         i++;
 11     }
 12 
 13     if (i > 0)
 14         status = 0;
 15 
 16     return(status);
 17 }
display.c
  1 #include "project.h"                                                        
  2 
  3 void display()
  4 {
  5     int i = 0;
  6 
  7     while (i < size)
  8     {
  9         fprintf(stdout, "%d\t", *(data+i));
 10         i++;
 11     }
 12     fprintf(stdout, "\n");
 13 }
project.h
  1 #ifndef _PROJECT_H                                                          
  2 #define _PROJECT_H
  3 
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 
  7 int  fscore(FILE *);
  8 int  iscore(FILE *);
  9 int (*score)(FILE *);
 10 void display();
 11 
 12 int *data;
 13 int  size;
 14 
 15 #endif

March 15, 2013

STRUCTURES:

Structure;
array name
array name 
array name
struct person{
char name[80];
int age;
float height;
};

struct person people[8]
    people[3].age=37; //sets fourth entry in 'people's array age to 37

typedef struct person P;  //P=struct person
P people[8]; == struct person people[8]
prestruct.c

We began writing the prestuct program today in preparation for the struct program. The prestruct program is displayed below:

  1 /*
  2  * prestructs1.c
  3  *
  4  *  An example leading into using structs in C, along with arrays.
  5  *
  6  * To compile: gcc -o prestructs1 prestructs1.c
  7  *
  8  */
  9 #include<stdio.h>
 10 int main()
 11 {
 12        int age1, age2;
 13        float height1, height2;
 14         char name1[80], name2[80], junk;
 15         printf("Person 1 of 2:\n");
 16         printf("==============\n");
 17         // prompt for name (string)
 18         printf("Please enter the person's first name: ");
 19         scanf("%s", name1);
 20         // prompt for age (integer)
 21         printf("Please enter %s's age: ", name1);
 22         scanf("%d", &age1);
 23         // prompt for height (float)
 24         printf("Please enter %s's height: ", name1);
 25         scanf("%f", &height1);
 26         // Get newline out of the input stream
 27         junk = fgetc(stdin);
 28         printf("Person 2 of 2:\n");
 29         printf("==============\n");
 30         // prompt for name (string)
 31         printf("Please enter the person's first name: ");
 32         scanf("%s", name2);
 33         // prompt for age (integer)
 34         printf("Please enter %s's age: ", name2);
 35         scanf("%d", &age2);
 36         // prompt for height (float)
 37         printf("Please enter %s's height: ", name2);
 38         scanf("%f", &height2);
 39         // Get newline out of the input stream
 40         junk = fgetc(stdin);
 41         printf("\n\nThe names of the people are:\n");
 42         printf("#1: %s\t", name1);
 43         printf("#2: %s\t", name2);
 44         printf("\n\n");
 45         printf("The full bios of the people are:\n");
 46         printf("#1: %s, age: %d, height: %f\n", name1, age1, height1);
 47         printf("#2: %s, age: %d, height: %f\n", name2, age2, height2);
 48         return(0);
 49 }  

March 20, 2013

gcc -o filename filesource -lm : compile while linking to math library (lm)

Today we assembled the proper pieces neccessary to run a program that can create visul displays for us, using the colors we choose; mathlib.c,

mathlib.c
  1 #include <stdio.h>                                                          
  2 #include <math.h>
  3 
  4 int main()
  5 {
  6     double a=25, b=0;
  7     b=sqrt(a);
  8     printf("The square root of %f is %f\n", a, b);
  9     return(0);
 10 }

March 22, 2013

Today we formatted the program that will draw an image, send it to a address where we can view it. Using the following figure as the scale for our image coordinates.

  1 #include <stdio.h>                                                          
  2 #include <gd.h>
  3 #include <math.h>
  4 
  5 #define PI 3.1415926535897
  6 
  7 int main ()
  8 {
  9     FILE *out;
 10     char outfile[] = "image.png";
 11     gdImagePtr       img;
 12     unsigned int current;
 13     unsigned short int wide, high;
 14     int degree, x, y, r=0, red=0, green=0, blue=0;
 15     float radian;
 16 
 17     wide=800;
 18     high=800;
 19 
 20     img=gdImageCreateTrueColor(wide, high);
 21 
 22     current=gdImageColorAllocate(img, 0x00, 0x00, 0x00);
 23     gdImageFilledRectangle(img, 0, 0, wide, high, current);
 24 
 25     r=10;
 26     green=0xFF;
 27     for(degree=0; degree < 1080; degree+=8)
 28 {
 29     radian=degree*(PI/180);
 30     x=(wide/2)+r*sin(radian);
 31     y=(high/2)+r*cos(radian);
 32     current=gdImageColorAllocate(img, red, 0, blue);
 33     gdImageFilledRectangle(img, x, y, x+30, y+30, current);
 34     r=r+2;
 35     blue=blue+2;
 36     red=red+2;
 37 }
 38 
 39     
 40     //output the data
 41     //
 42     out=fopen(outfile, "wb");
 43     gdImagePngEx(img, out, -1);
 44 
 45     //close things up
 46     //
 47     fclose(out);
 48     gdImageDestroy(img);
 49 
 50     return(0);
 51 } 

0,0 800, 0

 _____________________
|                     |
|                     |
|                     |
|                     |
|_____________________|

0, 600 800, 600

March 27, 2013

We worked on some more GD projects today, this time for drawing a triangle, as displayed below:

==GDpoly.c==
<code>
  1 #include <stdio.h>                                                          
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <gd.h>                                                             
  5  
  6 // color values
  7 //
  8 #define BLACK       0
  9 #define GRAY        1
 10 #define VIOLET      2
 11 #define INDIGO      3
 12 #define BLUE        4
 13 #define GREEN       5
 14 #define YELLOW      6
 15 #define ORANGE      7
 16 #define RED         8
 17 #define WHITE       9
 18 #define DARKGREEN   10
 19  
 20 int main(int argc, char **argv)
 21 {
 22     FILE                *out;       // output file pointer
 23     char                *outfile;   // output file name
 24     gdImagePtr          img;        // GD Image Construct
 25     gdPoint             points[3], points1[3], points2[3];  // Points for a     Polygon
 26     unsigned int        color[11];  // color array
 27     unsigned short int  wide, high, // useful image and
 28                         x, y;       // coordinate variables
 29  
 30     if (argc == 2)
 31     {
 32         outfile = *(argv+1);
 33     }
 34     else
 35     {
 36         outfile = (char *) malloc (sizeof(char) * 10);
 37         strcpy(outfile, "image.png");
 38     }
 39  
 40     fprintf(stdout, "Using '%s' as output filename\n", outfile);
 41  
 42     wide = 800;
 43     high = 600;
 44  
 45     img = gdImageCreateTrueColor(wide, high);
 46  
 47     // My GD color definitions
 48     //
 49     color[BLACK]             = gdImageColorAllocate(img,   0,   0,   0);
 50     color[BLUE]              = gdImageColorAllocate(img,   0,   0, 255);
 51     color[GREEN]             = gdImageColorAllocate(img,   0, 255,   0);
 52     color[DARKGREEN]         = gdImageColorAllocate(img,  51, 107,   0);
 53     color[RED]               = gdImageColorAllocate(img, 255,   0,   0);
 54     color[GRAY]              = gdImageColorAllocate(img, 204, 204, 204);
 55     color[WHITE]             = gdImageColorAllocate(img, 255, 255, 255);
 56     color[VIOLET]            = gdImageColorAllocate(img, 255, 0, 255);
 57     /* Draw a triangle. */
 58     points[0x00].x = (wide / 2);
 59     points[0x00].y = 0;
 60     points[0x01].x = wide;
 61     points[0x01].y = high;
 62     points[0x02].x = 0;
 63     points[0x02].y = high;
 64 
 65     points1[0x00].x = (wide/2);
 66     points1[0x00].y = high;
 67     points1[0x01].x = (wide/2)-200;
 68     points1[0x01].y = (high/2);
 69     points1[0x02].x = (wide/2)+200;
 70     points1[0x02].y = (high/2); 
 71 
 72     points2[0x00].x = (wide/2);
 73     points2[0x00].y = high-75;
 74     points2[0x01].x = (wide/2)-150;
 75     points2[0x01].y = (high/2);
 76     points2[0x02].x = (wide/2)+150;
 77     points2[0x02].y = (high/2);
 78     /* Paint it in white */
 79     gdImageFilledPolygon(img, points, 3, color[VIOLET]);                    
 80     gdImageFilledPolygon(img, points1, 3, color[BLACK]);
 81     gdImageFilledPolygon(img, points2, 3, color[VIOLET
 82 ]);
 83     /* Outline it in red; must be done second */
 84     gdImagePolygon(img, points, 3, color[BLUE]);
 85 
 86 // Output the data
 87     //
 88     out = fopen(outfile, "wb");
 89     gdImagePngEx(img, out, -1);
 90  
 91     // Close things up
 92     //
 93     fclose(out);
 94     gdImageDestroy(img);
 95  
 96     return(0);
 97 }
 98 
 99 

outputting a pink triangle with a black 'V' in its front

March 29,2013

rectangle.h
  1 class Rectangle {                                                           
  2     public:
  3         Rectangle();         // Constructor (parameterless)
  4         Rectangle(int, int); // Overloaded  (parametered)
  5         void setWidth(int);  // Accessor Method
  6         void setHeight(int); // Accessor Method
  7         int  getWidth();     // Accessor Method
  8         int  getHeight();    // Accessor Method
  9         int  area();         // Calculate the area of the Rectangle
 10         int  perimeter();    // Calculate the perimeter of the Rectangle
 11 
 12     private:
 13         int width;
 14         int height;
 15 };
main.cc
  1 #include <stdio.h>                                                          
  2 #include "rectangle.h"
  3 
  4 int main()
  5 {
  6     Rectangle r1, r2, *r3;
  7 
  8     r3 = new Rectangle(14, 15);
  9 
 10     r1.setWidth(24);
 11     r1.setHeight(16);
 12 
 13     //r2.height = 17;
 14     //r2.width  = 13;
 15 
 16     printf("r1 has a width of %d and a height of %d.\n", r1.getWidth(), r1.g    etHeight());
 17     printf("r1's perimeter is %d and area is %d.\n", r1.perimeter(), r1.area    ());
 18 
 19     //printf("r1 has a width of %d and a height of %d.\n", r1.getWidth(), r1    .getHeight);
 20     //printf("r1's perimeter is %d and area is %d.\n", r1.perimeter(), r1.ar    ea);
 21 
 22     // Do the same- display these values for r3
 23 
 24     return(0);
 25 }
rectangle.cc
  1 #include"rectangle.h"                                                       
  2 
  3 Rectangle :: Rectangle()
  4 {
  5     width  = 200;
  6     height = 400;
  7 }
  8 
  9 Rectangle :: Rectangle(int width, int height)
 10 {
 11     this->width  = width;
 12     this->height = height;
 13 }
 14 
 15 void Rectangle :: setWidth(int width)
 16 {
 17     this->width  = width;
 18 }
 19 
 20 void Rectangle :: setHeight(int height)
 21 {
 22     // Left as an exercise to the implementer
 23 }
 24 
 25 int Rectangle :: getWidth()
 26 {
 27     // Left as an exercise to the implementer
 28 }
 29 
 30 int Rectangle :: getHeight()
 31 {
 32     return(height);
 33 }
 34 
 35 int Rectangle :: area()
 36 {
 37     // Left as an exercise to the implementer
 38 }
 39 
 40 int Rectangle :: perimeter()
 41 {
 42     // Left as an exercise to the implementer
 43 }

April 10, 2013

At this point in time we finished playing/modifying the circle of squares and triangle drawing programs, and have moved on to polymorphism, displayed below, it outputs your bank account based upon hardwritten values.

  1#include <cstdio>
  2
  3 class Account
  4 {
  5     public:
  6         Account()
  7         {
  8             balance = 32.00;
  9         }
 10
 11         Account(double blc)
 12         {
 13             balance = blc;
 14         }
 15
 16         virtual void withdraw(double amt)
 17         {
 18             balance = balance - amt;
 19             printf("Account balance is %f\n", balanc    e);
 20         }
 21     protected:
 22         double balance;
 23 };
 24
 25 class BankAccount : public Account
 26 {
 27     public:
 28         BankAccount(double blc)
 29         {
 30             balance = blc;
 31         }
 32
 33         virtual void withdraw(double amt)
 34         {
 35             if (balance - amt >= 0.0)
 36             {
 37                 balance = balance - amt;
 38             }
 39             printf("Bank Account balance is %f\n", b    alance);
 40         }
 41 };
 42
 43 int main()
 44 {
 45     Account account1(25.00);
 46     BankAccount account2(50.00);
 47
 48     account1.withdraw(5.00);
 49     account2.withdraw(17.00);
 50
 51     return(0);
 52 }

April 12, 2013

This Friday was spent asking questions in class, as not many were present, I worked on a program that ended up being used for the final projects:

 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4
  5 int main(int argc, char **argv)
  6 {
  7         int i;
  8         char value = 0;
  9
 10         if(argc==1)
 11         {
 12                 fprintf(stderr, "Error! Please provide word to encode ");
 13                 fprintf(stderr, "on command-line.\n\n");
 14                 fprintf(stderr, "Syntax is:\n");
 15                 fprintf(stderr, "===================================\n");
 16                 fprintf(stderr, "a,e,i,o,u,w,h,y .......... no value\n");
 17                 fprintf(stderr, "s,c,z .................... 0\n");
 18                 fprintf(stderr, "t,d,th ................... 1\n");
 19                 fprintf(stderr, "n ........................ 2\n");
 20                 fprintf(stderr, "m ........................ 3\n");
 21                 fprintf(stderr, "r ........................ 4\n");
 22                 fprintf(stderr, "l ........................ 5\n");
 23                 fprintf(stderr, "ch,sh,g,j,dg,tch ......... 6\n");
 24                 fprintf(stderr, "k,q,x,C,G,-ng,-nk ........ 7\n");
 25                 fprintf(stderr, "f,v,ph ................... 8\n");
 26                 fprintf(stderr, "p,b ...................... 9\n");
 27                 fprintf(stderr, "===================================\n\n");
 28                 exit(1);
 29         }
 30
 31         else if(argc==2)
 32         {
 33         }
 34
 35         else
 36         {
 37                 fprintf(stderr, "Additional arguments will be ignored.\n");
 38         }
 39
 40         for(i=0; i<strlen(*(argv+1)); i++)
 41         {
 42                 value=*(*(argv+1)+i);
 43
 44                 if(value=='a')
 45                 {
 46                 }
 47                 else if(value=='e')
 48                 {
 49                 }
 50                 else if(value=='i')
 51                 {
 52                 }
 53                 else if(value=='o')
 54                 {
 55                 }
 56                 else if(value=='u')
 57                 {
 58                 }
 59                 else if(value=='w')
 60                 {
 61                 }
 62                 else if(value=='h')
 63                 {
 64                 }
 65                 else if(value=='y')
 66                 {
 67                 }
 68
 69                 else if(value=='s')
 70                 {
 71                         if ((*(*(argv+1)+(i+1))) == 'h')
 72                         {
 73                                 fprintf(stdout, "6");
 74                         }
 75                         else
 76                         {
 77                                 fprintf(stdout, "0");
 78                         }
 79                 }
 80                 else if(value=='c')
 81                 {
 82                         if ((*(*(argv+1)+(i+1))) == 'h')
 83                         {
 84                                 fprintf(stdout, "6");
 85                         }
 86                         else
 87                         {
 88                                 fprintf(stdout, "0");
 89                         }
 90                 }
 91
 92                 else if(value=='z')
 93                 {
 94                         fprintf(stdout, "0");
 95                 }
 96
 97                 else if(value=='d')
 98                 {
 99                         if ((*(*(argv+1)+(i+1))) == 'g')
100                         {
101                                 fprintf(stdout, "6");
102                                 i++;
103                         }
104                         else
105                                 if ((*(*(argv+1)+(i+1))) == 'c')
106                                 {
107                                         if ((*(*(argv+1)+(i+2))) == 'h')
108                                         {
109                                                 fprintf(stdout, "6");
110                                                 i = i + 2;
111                                         }
112                                 }
113                                 else
114                                 {
115                                         fprintf(stdout, "1");
116                                 }
117                 }
118
119                 else if(value=='t')
120                 {
121                         if ((*(*(argv+1)+(i+1))) == 'c')
122                         {
123                                 if ((*(*(argv+1)+(i+2))) == 'h')
124                                 {
125                                         fprintf(stdout, "6");
126                                         i = i + 2;
127                                 }
128                         }
129                         else
130                         {
131                                 fprintf(stdout, "1");
132                         }
133                 }
134
135                 else if(value=='n')
136                 {
137                         if (((*(*(argv+1)+(i+1))) == 'g') || ((*(*(argv+1)+(i+1))) == 'k'))
138                         {
139                                 fprintf(stdout, "7");
140                                 i++;
141                         }
142                         else
143                         {
144                                 fprintf(stdout, "2");
145                         }
146                 }
147
148                 else if(value=='m')
149                 {
150                         fprintf(stdout, "3");
151                 }
152
153                 else if(value=='r')
154                 {
155                         fprintf(stdout, "4");
156                 }
157
158                 else if(value=='l')
159                 {
160                         fprintf(stdout, "5");
161                 }
162
163                 else if(value=='j')
164                 {
165                         fprintf(stdout, "6");
166                 }
167                 else if(value=='g')
168                 {
169                         fprintf(stdout, "6");
170                 }
171
172                 else if(value=='k')
173                 {
174                         if ((*(*(argv+1)+(i+1))) == 'k')
175                         {
176                                 i++;
177                                 fprintf(stdout, "7");
178                         }
179                         if(value == 'x')
180                         {
181                                 fprintf(stdout, "0");
182                         }
183                 }
184                 else if(value=='q')
185                 {
186                         if ((*(*(argv+1)+(i+1))) == 'k')
187                         {
188                                 i++;
189                                 fprintf(stdout, "7");
190                         }
191                         if(value == 'x')
192                         {
193                                 fprintf(stdout, "0");
194                         }
195                 }
196                 else if(value=='x')
197                 {
198                         if ((*(*(argv+1)+(i+1))) == 'k')
199                         {
200                                 i++;
201                                 fprintf(stdout, "7");
202                         }
203                         if(value == 'x')
204                         {
205                                 fprintf(stdout, "0");
206                         }
207                 }
208                 else if(value=='C')
209                 {
210                         if ((*(*(argv+1)+(i+1))) == 'k')
211                         {
212                                 i++;
213                         }
214                         else
215                                 fprintf(stdout, "7");
216                         if(value == 'x')
217                         {
218                                 fprintf(stdout, "0");
219                         }
220                 }
221
222                 else if(value=='G')
223                 {
224                         fprintf(stdout, "7");
225                         if(value == 'x')
226                         {
227                                 fprintf(stdout, "0");
228                         }
229                 }
230
231                 else if(value=='f')
232                 {
233                         fprintf(stdout, "8");
234                 }
235
236                 else if(value=='v')
237                 {
238                         fprintf(stdout, "8");
239                 }
240
241                 else if(value=='p')
242                 {
243                         if ((*(*(argv+1)+(i+1))) == 'h')
244                         {
245                                 fprintf(stdout, "8");
246                                 i++;
247                         }
248                         else
249                                 fprintf(stdout, "9");
250                 }
251
252                 else if(value=='b')
253                 {
254                         fprintf(stdout, "9");
255                 }
256
257                 else
258                 {
259                         fprintf(stdout, " ");
260                 }
261         }
262
263         fprintf(stdout, "\n");
264
265         return(0);
266 }

April 17, 2013

Today we were shown templates, the last new section of the course to be taught, after this it will all be review classes.

Average.c

// average.cc - an example average program using templates
 
#include<iostream>
using namespace std;

template <class T>
void myaverage (T val1, T val2, T val3)
{
    T result;
    result = val1 + val2 + val3;

    cout << "First value is: " << val1 << endl;
    cout << "Second value is: " << val2 << endl;
    cout << "Third value is: " << val3 << endl;

    cout << "The sum of all three are: " << result << endl;

    cout << "The average of all three are: " << (T)(result/3) << endl;
}

int main()
{
    myaverage(3.1, 7.3, 1.7);
    myaverage(2, 4, 6);
    myaverage('~', '|', 'z');
    return 0;
}

April 19, 2013

Today was review class, he went around answering questions that anyone might have about anything.

April 19-end,2013

Review, work, review, work, sleep, eat, work, review, repeat…

opus/spring2013/khoose2/journal.txt · Last modified: 2013/05/06 13:56 by khoose2