Kellen Hoose's spring2013 Opus
Programming and such
In this space you can provide a description of yourself, your pursuits, your interests. To fill out this section, click the edit button off to the right, delete this placement text, and type your original content.
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
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); }
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 }
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 }
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+$
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
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: }
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+$
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+$
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.
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
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+$
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.
Today we spent creating a multi-file program, utilizing functions and object-file compilation.
First I created the individual C files to be compiled into my object files; Exponentiator,Increment, Adding, Multiply, Exponent, and Main.
//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.c #include "exponentiator.h" int increment(int num) { num++; return(num); }
//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.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.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.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); }
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”.
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$
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.
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 }
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 }
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 }
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 }
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
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]
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 }
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,
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 }
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
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
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 };
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 }
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 }
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 }
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 }
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; }
Today was review class, he went around answering questions that anyone might have about anything.
Review, work, review, work, sleep, eat, work, review, repeat…
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct node Node; 5 6 struct node{ 7 int value; 8 int place; 9 struct node *next; 10 struct node * *tmp3; 11 }; 12 13 Node *build(Node *start); 14 Node *insert(Node*start, Node *given, Node *newNode); 15 Node *append(Node *start, Node *given, Node *newNode); 16 //Node *getNode(Node *start, Node **tmp3); 17 Node *display(Node *start); 18 19 int main() 20 { 21 Node *start=NULL; 22 int option; 23 24 while(option!=-1) 25 { 26 fprintf(stdout,"|===========Menu==========|\n"); 27 fprintf(stdout,"| 1.)Build List |\n"); 28 fprintf(stdout,"| 2.)Insert to List |\n"); 29 fprintf(stdout,"| 3.)Append to List |\n"); 30 fprintf(stdout,"| 4.)Remove from List |\n"); 31 fprintf(stdout,"| 5.)Display the List |\n"); 32 fprintf(stdout,"| -1.)Quit |\n"); 33 fprintf(stdout,"|=========================|\n"); 34 fprintf(stdout, "Please select an option: "); 35 fscanf(stdin, "%d", &option); 36 37 if(option==1) 38 { 39 start=build(start); 40 start=display(start); 41 } 42 else if(option==2) 43 { 44 start=display(start); 45 Node *tmp, *tmp2=NULL; 46 int input=5; 47 fprintf(stdout,"Which node would you like to insert before?: "); 48 fscanf(stdin, "%d", &input); 49 int seeker; 50 tmp=start; 51 for(seeker=0; seeker<(input-1); seeker++) 52 { 53 tmp=tmp->next; 54 } 55 if(input==0) 56 { 57 fprintf(stdout,"Enter the value of the new node: "); 58 fscanf(stdin, "%d", &input); 59 tmp2=(Node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp->place=0; 62 } 63 else 64 { 65 fprintf(stdout, "Enter a value to insert: "); 66 fscanf(stdin, "%d", &input); 67 tmp2=(Node*)malloc(sizeof(Node)); 68 tmp2->value=input; 69 tmp->place=1; 70 } 71 start=insert(start, tmp, tmp2); 72 start=display(start); 73 } 74 else if(option==3) 75 { 76 start=display(start); 77 //assinging of values must occur before function execution 78 Node *tmp, *tmp4=NULL; 79 int input, choice; 80 int behind; 81 tmp=start; 82 fprintf(stdout,"Enter what node would you like to append after: "); 83 fscanf(stdin, "%d", &choice); 84 for(behind=0; behind<choice; behind++) 85 { 86 tmp=tmp->next; 87 } 88 //fprintf(stdout,"%d", behind); output tests 89 //start=display(tmp); output tests 90 tmp4=(Node*)malloc(sizeof(Node)); 91 tmp4->place=choice; 92 fprintf(stdout,"Enter value for node to be appended: "); 93 fscanf(stdin, "%d", &input); 94 tmp4->value=input; 95 96 start=display(start); 97 start=append(start, tmp, tmp4); 98 start=display(start); //display acts as test to prove it worked 99 } 100 else if(option==4) 101 { 102 //assinging before functions required 103 /* 104 start=display(start); 105 Node *tmp; 106 tmp=start; 107 Node **tmp3; 108 tmp3=&tmp; 109 int begone, input; 110 fprintf(stdout, "Which node would you like to remove?: "); 111 fscanf(stdin, "%d" , &input); 112 for(begone=0; begone<(input-1);begone++) 113 { 114 tmp=tmp->next; 115 } 116 if(input==0) 117 { 118 start->place=0; 119 } 120 else 121 { 122 start->place=1; 123 } 124 125 start=getNode(start, &tmp); 126 start=display(start); //display aids in proving succes of function 127 */ } 128 else if(option==5) 129 { 130 start=display(start); 131 } 132 else if (option=-1) 133 { 134 fprintf(stdout, "Goodbye\n"); 135 } 136 else 137 { 138 fprintf(stdout, "ERROR, INVALID SELECTION\n"); 139 } 140 } 141 return(0); 142 } 143 144 145 146 Node *build(Node *start) 147 { 148 int input=0; 149 Node *tmp; 150 start=tmp=NULL; 151 152 while(input!=-1) 153 { 154 fprintf(stdout, "Enter a value(-1 to end): "); 155 fscanf(stdin, "%d", &input); 156 157 if(input!=-1) 158 { 159 if(start==NULL) 160 { 161 start=tmp=(Node*)malloc(sizeof(Node)); 162 tmp->next=NULL; 163 start->value=input; 164 } 165 else 166 { 167 tmp->next=(Node*)malloc(sizeof(Node)); 168 tmp->next->next=NULL; 169 tmp->next->value=input; 170 tmp=tmp->next; 171 } 172 } 173 } 174 tmp=start; 175 176 return(start); 177 } 178 179 Node *insert(Node *start, Node *tmp, Node *tmp2) 180 { 181 int placing; 182 placing=tmp->place; 183 184 if(placing==0) 185 { 186 tmp2->next=NULL; 187 tmp2->next=tmp; 188 start=tmp2; 189 } 190 else 191 { 192 tmp2->next=NULL; 193 tmp2->next=tmp->next; 194 tmp->next=tmp2; 195 } 196 197 tmp=start; 198 return(start); 199 } 200 201 Node *append(Node *start, Node *tmp, Node *tmp4) 202 { 203 tmp4->next=tmp->next; 204 tmp->next=tmp4; 205 tmp=start; 206 207 return(start); 208 } 209 210 /* 211 Node *getNode(Node *start, Node **tmp3) 212 { 213 Node *tmp2=NULL; 214 Node *tmp; 215 int placing; 216 placing=start->place; 217 tmp=start; 218 219 if(start->place==0) 220 { 221 tmp2=**tmp3; 222 tmp->next=NULL; 223 start=tmp2; 224 } 225 226 else 227 { 228 tmp2=**tmp3; 229 tmp->next=tmp2->next; 230 tmp2->next=NULL; 231 } 232 233 tmp=start; 234 return(start); 235 } 236 */ 237 238 Node *display(Node *start) 239 { 240 int input=0; 241 Node *tmp; 242 tmp=start; 243 244 while(tmp!=NULL) 245 { 246 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 247 tmp=tmp->next; 248 input=input++; 249 } 250 fprintf(stdout, "NULL\n"); 251 252 return(start); 253 } 254
Above is my SLL project code, now perfect as the getNode function still has so double pointer recognition issues, will look online for material to reference.
After some outside help, I have fixed my SLL program to become SLL2, the getNode isn't perfect yet but I have gotten it to compile so after some pointer analyzing, everything should be working nicely.
SLL2.0
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct node Node; 5 6 struct node{ 7 int value; 8 int place; 9 struct node *next; 10 11 }; 12 13 Node *build(Node *start); 14 Node *insert(Node*start, Node *given, Node *newNode); 15 Node *append(Node *start, Node *given, Node *newNode); 16 //Node *getNode(Node *start, Node **tmp3); 17 Node *display(Node *start); 18 19 int main() 20 { 21 Node *start=NULL; 22 int option; 23 24 while(option!=-1) 25 { 26 fprintf(stdout,"|===========Menu==========|\n"); 27 fprintf(stdout,"| 1.)Build List |\n"); 28 fprintf(stdout,"| 2.)Insert to List |\n"); 29 fprintf(stdout,"| 3.)Append to List |\n"); 30 fprintf(stdout,"| 4.)Remove from List |\n"); 31 fprintf(stdout,"| 5.)Display the List |\n"); 32 fprintf(stdout,"| -1.)Quit |\n"); 33 fprintf(stdout,"|=========================|\n"); 34 fprintf(stdout, "Please select an option: "); 35 fscanf(stdin, "%d", &option); 36 37 if(option==1) 38 { 39 start=build(start); 40 start=display(start); 41 } 42 else if(option==2) 43 { 44 start=display(start); 45 Node *tmp, *tmp2=NULL; 46 int input=5; 47 fprintf(stdout,"Which node would you like to insert before?: "); 48 fscanf(stdin, "%d", &input); 49 int seeker; 50 tmp=start; 51 for(seeker=0; seeker<(input-1); seeker++) 52 { 53 tmp=tmp->next; 54 } 55 if(input==0) 56 { 57 fprintf(stdout,"Enter the value of the new node: "); 58 fscanf(stdin, "%d", &input); 59 tmp2=(Node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp->place=0; 62 } 63 else 64 { 65 fprintf(stdout, "Enter a value to insert: "); 66 fscanf(stdin, "%d", &input); 67 tmp2=(Node*)malloc(sizeof(Node)); 68 tmp2->value=input; 69 tmp->place=1; 70 } 71 start=insert(start, tmp, tmp2); 72 start=display(start); 73 } 74 else if(option==3) 75 { 76 start=display(start); 77 //Assinging of values must occur before function execution 78 Node *tmp, *tmp4=NULL; 79 int input, choice; 80 int behind; 81 tmp=start; 82 fprintf(stdout,"Enter what node would you like to append after: "); 83 fscanf(stdin, "%d", &choice); 84 for(behind=0; behind<choice; behind++) 85 { 86 tmp=tmp->next; 87 } 88 //fprintf(stdout,"%d", behind); output tests 89 //start=display(tmp); output tests 90 tmp4=(Node*)malloc(sizeof(Node)); 91 tmp4->place=choice; 92 fprintf(stdout,"Enter value for node to be appended: "); 93 fscanf(stdin, "%d", &input); 94 tmp4->value=input; 95 96 start=display(start); 97 start=append(start, tmp, tmp4); 98 start=display(start); //display acts as test to prove it worked 99 } 100 else if(option==4) 101 { 102 //assinging before functions required 103 104 start=display(start); 105 Node *tmp; 106 tmp=start; 107 Node **tmp3; 108 tmp3=&tmp; 109 int begone, input; 110 fprintf(stdout, "Which node would you like to remove?: "); 111 fscanf(stdin, "%d" , &input); 112 for(begone=0; begone<(input-1);begone++) 113 { 114 tmp=tmp->next; 115 } 116 if(input==0) 117 { 118 start->place=0; 119 } 120 else 121 { 122 start->place=1; 123 } 124 125 start=getNode(start, (*tmp3)); 126 start=display(start); //display aids in proving succes of function 127 } 128 else if(option==5) 129 { 130 start=display(start); 131 } 132 else if (option=-1) 133 { 134 fprintf(stdout, "Goodbye\n"); 135 } 136 else 137 { 138 fprintf(stdout, "ERROR, INVALID SELECTION\n"); 139 } 140 } 141 return(0); 142 } 143 144 145 146 Node *build(Node *start) 147 { 148 int input=0; 149 Node *tmp; 150 start=tmp=NULL; 151 152 while(input!=-1) 153 { 154 fprintf(stdout, "Enter a value(-1 to end): "); 155 fscanf(stdin, "%d", &input); 156 157 if(input!=-1) 158 { 159 if(start==NULL) 160 { 161 start=tmp=(Node*)malloc(sizeof(Node)); 162 tmp->next=NULL; 163 start->value=input; 164 } 165 else 166 { 167 tmp->next=(Node*)malloc(sizeof(Node)); 168 tmp->next->next=NULL; 169 tmp->next->value=input; 170 tmp=tmp->next; 171 } 172 } 173 } 174 tmp=start; 175 176 return(start); 177 } 178 179 Node *insert(Node *start, Node *tmp, Node *tmp2) 180 { 181 int placing; 182 placing=tmp->place; 183 184 if(placing==0) 185 { 186 tmp2->next=NULL; 187 tmp2->next=tmp; 188 start=tmp2; 189 } 190 else 191 { 192 tmp2->next=NULL; 193 tmp2->next=tmp->next; 194 tmp->next=tmp2; 195 } 196 197 tmp=start; 198 return(start); 199 } 200 201 Node *append(Node *start, Node *tmp, Node *tmp4) 202 { 203 tmp4->next=tmp->next; 204 tmp->next=tmp4; 205 tmp=start; 206 207 return(start); 208 } 209 210 /* 211 Node *getNode(Node *start, Node (*tmp3)) 212 { 213 Node *tmp2=NULL; 214 Node *tmp; 215 int placing; 216 placing=start->place; 217 tmp=start; 218 219 if(start==tmp3) 220 { 221 tmp2=tmp3; 222 tmp->next=NULL; 223 start=tmp2; 224 } 225 226 else 227 { 228 tmp2=tmp3; 229 tmp->next=tmp2->next; 230 tmp2->next=NULL; 231 } 232 233 tmp=start; 234 return(start); 235 } 236 */ 237 238 Node *display(Node *start) 239 { 240 int input=0; 241 Node *tmp; 242 tmp=start; 243 244 while(tmp!=NULL) 245 { 246 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 247 tmp=tmp->next; 248 input=input++; 249 } 250 fprintf(stdout, "NULL\n"); 251 252 return(start); 253 } 254
The output of which, focused on the malfunctioning getNode function called 'remove' in menu listing:
lab46:~/src/DATA/spring2014$ ./SLL2 |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 1 Enter a value(-1 to end): 1 Enter a value(-1 to end): 2 Enter a value(-1 to end): 3 Enter a value(-1 to end): 4 Enter a value(-1 to end): 5 Enter a value(-1 to end): 6 Enter a value(-1 to end): 7 Enter a value(-1 to end): 8 Enter a value(-1 to end): 9 Enter a value(-1 to end): -1 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 4 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL Which node would you like to remove?: 4 [0] 1 -> [1] 5 -> [2] 6 -> [3] 7 -> [4] 8 -> [5] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 4 [0] 1 -> [1] 5 -> [2] 6 -> [3] 7 -> [4] 8 -> [5] 9 -> NULL Which node would you like to remove?: 5 [0] 1 -> [1] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: -1 Goodbye
It seems that the input node to be removed becomes the start→next due to an error in my code, I will be looking on how to fix it.
I Finally got the damn thing working, so far behind schedule it's extremely upsetting but working all the same, the double pointers had me confused until the always benevolent teacher aided me with some advice.
1 /* 2 Author:Kellen Hoose 3 Created:February 2014 4 Purpose:For the SLL_moar project for class, menu driven program with seperate functions call ings and definitions, build, insert, append, remove, display, and quit as options. 5 */ 6 #include <stdio.h> 7 #include <stdlib.h> 8 9 typedef struct node Node; 10 11 struct node{ 12 int value; 13 int place; 14 struct node *next; 15 }; 16 17 Node *build(Node *start); 18 Node *insert(Node*start, Node *given, Node *newNode); 19 Node *append(Node *start, Node *given, Node *newNode); 20 Node *getNode(Node *start, Node **tmp3); 21 Node *display(Node *start); 22 23 int main() 24 { 25 Node *start=NULL; 26 int option; 27 28 while(option!=-1) 29 { 30 fprintf(stdout,"|===========Menu==========|\n"); 31 fprintf(stdout,"| 1.)Build List |\n"); 32 fprintf(stdout,"| 2.)Insert to List |\n"); 33 fprintf(stdout,"| 3.)Append to List |\n"); 34 fprintf(stdout,"| 4.)Remove from List |\n"); 35 fprintf(stdout,"| 5.)Display the List |\n"); 36 fprintf(stdout,"| -1.)Quit |\n"); 37 fprintf(stdout,"|=========================|\n"); 38 fprintf(stdout, "Please select an option: "); 39 fscanf(stdin, "%d", &option); 40 41 if(option==1) 42 { 43 start=build(start); 44 start=display(start); 45 } 46 else if(option==2) 47 { 48 start=display(start); 49 Node *tmp, *tmp2=NULL; 50 int input=5; 51 fprintf(stdout,"Which node would you like to insert before?: "); 52 fscanf(stdin, "%d", &input); 53 int seeker; 54 tmp=start; 55 for(seeker=0; seeker<(input-1); seeker++) 56 { 57 tmp=tmp->next; 58 } 59 if(input==0) 60 { 61 fprintf(stdout,"Enter the value of the new node: "); 62 fscanf(stdin, "%d", &input); 63 tmp2=(Node*)malloc(sizeof(Node)); 64 tmp2->value=input; 65 tmp->place=0; 66 } 67 else 68 { 69 fprintf(stdout, "Enter a value to insert: "); 70 fscanf(stdin, "%d", &input); 71 tmp2=(Node*)malloc(sizeof(Node)); 72 tmp2->value=input; 73 tmp->place=1; 74 } 75 start=insert(start, tmp, tmp2); 76 start=display(start); 77 } 78 else if(option==3) 79 { 80 start=display(start); 81 //assinging of values must occur before function execution 82 Node *tmp, *tmp4=NULL; 83 int input, choice; 84 int behind; 85 tmp=start; 86 fprintf(stdout,"Enter what node would you like to append after: "); 87 fscanf(stdin, "%d", &choice); 88 for(behind=0; behind<choice; behind++) 89 { 90 tmp=tmp->next; 91 } 92 //fprintf(stdout,"%d", behind); output tests 93 //start=display(tmp); output tests 94 tmp4=(Node*)malloc(sizeof(Node)); 95 tmp4->place=choice; 96 fprintf(stdout,"Enter value for node to be appended: "); 97 fscanf(stdin, "%d", &input); 98 tmp4->value=input; 99 100 start=display(start); 101 start=append(start, tmp, tmp4); 102 start=display(start); //display acts as test to prove it worked 103 } 104 else if(option==4) 105 { 106 //assinging before functions required 107 108 start=display(start); 109 Node *tmp; 110 tmp=start; 111 Node **tmp3; 112 tmp3=&tmp; 113 int begone, input; 114 fprintf(stdout, "Which node would you like to remove?: "); 115 fscanf(stdin, "%d" , &input); 116 for(begone=0; begone<(input-1);begone++) 117 { 118 tmp=tmp->next; 119 } 120 121 // start=display(tmp); //maybe tmp isn't being assigned right?, sets tmp to one before node to be removed. 122 start=getNode(start, &tmp); 123 start=display(start); //display aids in proving succes of function 124 } 125 else if(option==5) 126 { 127 start=display(start); 128 } 129 else if (option=-1) 130 { 131 fprintf(stdout, "Goodbye\n"); 132 } 133 else 134 { 135 fprintf(stdout, "ERROR, INVALID SELECTION\n"); 136 } 137 } 138 return(0); 139 } 140 141 142 143 Node *build(Node *start) 144 { 145 int input=0; 146 Node *tmp; 147 start=tmp=NULL; 148 149 while(input!=-1) 150 { 151 fprintf(stdout, "Enter a value(-1 to end): "); 152 fscanf(stdin, "%d", &input); 153 154 if(input!=-1) 155 { 156 if(start==NULL) 157 { 158 start=tmp=(Node*)malloc(sizeof(Node)); //creates node, allocate memory to si ze of Node struct, thus saving space. 159 tmp->next=NULL; 160 start->value=input; 161 } 162 else 163 { 164 tmp->next=(Node*)malloc(sizeof(Node)); 165 tmp->next->next=NULL; 166 tmp->next->value=input; 167 tmp=tmp->next; 168 } 169 } 170 } 171 tmp=start; 172 173 return(start); 174 } 175 176 Node *insert(Node *start, Node *tmp, Node *tmp2) 177 { 178 int placing; 179 placing=tmp->place; 180 181 if(placing==0) 182 { 183 tmp2->next=NULL; 184 tmp2->next=tmp; 185 start=tmp2; 186 } 187 else 188 { 189 tmp2->next=NULL; 190 tmp2->next=tmp->next; 191 tmp->next=tmp2; 192 } 193 194 tmp=start; 195 return(start); 196 } 197 198 Node *append(Node *start, Node *tmp, Node *tmp4) 199 { 200 tmp4->next=tmp->next; 201 tmp->next=tmp4; 202 tmp=start; 203 204 return(start); 205 } 206 207 208 Node *getNode(Node *start, Node **tmp3) 209 { 210 Node *tmp2=NULL; 211 Node *tmp4; 212 tmp4=*tmp3;//*tmp3=&tmp (address of tmp, which is 1 before node to be removed 213 tmp2=*tmp3; 214 215 if(start==*tmp3) 216 { 217 tmp4=tmp2->next; //if removing start, set start as 2nd from start, cut that thang of f 218 tmp2->next=NULL; 219 start=tmp4; 220 } 221 222 else 223 { 224 tmp4=tmp2->next; 225 tmp2->next=tmp4->next; // removing other than start, skip around node, cut it off 226 tmp4->next=NULL; 227 } 228 229 tmp4=start; 230 return(start); 231 } 232 233 234 Node *display(Node *start) 235 { 236 int input=0; 237 Node *tmp; 238 tmp=start; 239 240 while(tmp!=NULL) 241 { 242 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 243 tmp=tmp->next; 244 input=input++; 245 } 246 fprintf(stdout, "NULL\n"); 247 248 return(start); 249 }
This program results in the following operation/output:
lab46:~/src/DATA/spring2014$ ./SLL3 |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 1 Enter a value(-1 to end): 1 Enter a value(-1 to end): 2 Enter a value(-1 to end): 3 Enter a value(-1 to end): 4 Enter a value(-1 to end): 5 Enter a value(-1 to end): 6 Enter a value(-1 to end): 7 Enter a value(-1 to end): 8 Enter a value(-1 to end): 9 Enter a value(-1 to end): -1 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 2 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL Which node would you like to insert before?: 4 Enter a value to insert: 12 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 3 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> NULL Enter what node would you like to append after: 9 Enter value for node to be appended: 10 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> NULL [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> [10] 10 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 4 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> [10] 10 -> NULL Which node would you like to remove?: 4 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> [9] 10 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 5 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> [9] 10 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: -1 Goodbye lab46:~/src/DATA/spring2014$