Created
October 25, 2014 00:02
-
-
Save awojnowski/d244427f622f324fd6cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// guavajuice | |
// | |
// Created by Aaron Wojnowski on 2014-10-24. | |
// Copyright (c) 2014 aaron. All rights reserved. | |
// | |
#include <stdio.h> | |
#include <string.h> | |
#pragma mark - Declarations | |
void pointerArithmetic(); | |
void question1(); | |
void question2to4(); | |
void question5(); | |
void question6(); | |
void question7(); | |
void question8to10(); | |
void questionLong1(); | |
void questionLong2(); | |
#pragma mark - Main | |
int main(int argc, const char * argv[]) { | |
printf("GORD 2060\n"); | |
printf("=========\n\n"); | |
// pointer arithmetic | |
pointerArithmetic(); | |
// midterm preparation questions | |
question1(); | |
question2to4(); | |
question5(); | |
question6(); | |
question7(); | |
question8to10(); | |
questionLong1(); | |
questionLong2(); | |
return 0; | |
} | |
#pragma mark - Helper Methods | |
void printIntArray(int *array, int size) { | |
printf("["); | |
{ | |
for (int i = 0; i < size; i++) { | |
printf("%d",array[i]); | |
if (i + 1 < size) printf(", "); | |
} | |
} | |
printf("]\n"); | |
} | |
#pragma mark - Pointer Arithmetic | |
void pointerArithmetic() { | |
printf("Pointer Arithmetic\n"); | |
printf("------------------\n\n"); | |
// let's do some looking to arrays and | |
// how to interact with elements | |
int const arraySize = 10; | |
int array[arraySize]; | |
for (int i = 0; i < arraySize; ++i) { | |
int const value = i + 1; | |
// all arrays in c are just pointers to the starting memory | |
// array[1] doesn't actually go to the "first element" it just | |
// takes the starting pointer and increments it by 1 | |
// however, it is assignable which is nice | |
// here, we could do array[i] = value; | |
// or we could manipulate the pointer without subscripting | |
// here we use memcpy since that allows us to write to the memory directly | |
memcpy(array + i, &value, sizeof(value)); | |
// this does the same thing too (copies memory into the pointer returned by array[i]) | |
memcpy(&array[i], &value, sizeof(value)); | |
} | |
printIntArray(array, arraySize); | |
printf("\n"); | |
} | |
#pragma mark - Question Requirements | |
char *printit(char *s) { | |
char *temp = s; | |
// goes through temp and prints it out | |
while (*temp) putchar(*temp++); | |
// goes back through temp and prints it in reverse | |
// initially, temp is pointing one byte higher than where the last character is stored | |
// therefore it is ok to dereference the decremented "temp" | |
do { | |
putchar(*--temp); | |
} while (temp - s); | |
putchar('\n'); | |
return(temp); | |
} | |
#pragma mark - Questions | |
void question1() { | |
printf("Question 1\n"); | |
printf("----------\n\n"); | |
int const ARRAY_SIZE = 5; | |
int array[ARRAY_SIZE]; | |
for (int i = 0; i < sizeof(array) / sizeof(int); i++) | |
array[i] = (i + 1) * 2; | |
printf("Initial array: \n"); | |
printIntArray(array, ARRAY_SIZE); | |
int *pi; | |
for (pi = &array[0]; pi < &array[ARRAY_SIZE]; ) | |
*++pi = 0; | |
printf("\n"); | |
printf("Array after iterating: \n"); | |
printIntArray(array, ARRAY_SIZE); | |
printf("\n"); | |
} | |
void question2to4() { | |
printf("Questions 2 to 4\n"); | |
printf("----------------\n\n"); | |
printf("2: "); | |
printit("oh-oh"); | |
printf("3: "); | |
char *x = printit("oh-oh"); | |
printf(" x: %s\n",x); | |
printf("4: *--temp dereferences the pointer after it is decremented whereas --*temp decrements the dereferenced pointer value.\n"); | |
printf("\n"); | |
} | |
void question5() { | |
printf("Question 5\n"); | |
printf("----------\n\n"); | |
// note: the code given in the question is incorrect (there is a semicolon where | |
// there should be a comma) | |
char *string, **strings; | |
// fill strings variable | |
char *temp = "test"; | |
strings = &temp; | |
// initially string will point to strings just fine but then it's incremented | |
// and I have no idea what the fuck happens then | |
while ((string = *strings++) != NULL) { | |
printf("%s\n",string); | |
} | |
printf("\n"); | |
} | |
void question6() { | |
printf("Question 6\n"); | |
printf("----------\n\n"); | |
int const ARRAY_SIZE = 5; | |
int array[ARRAY_SIZE]; | |
for (int i = 0; i < sizeof(array) / sizeof(int); i++) { | |
array[i] = (i + 1) * 2; | |
//array[i] = 0; | |
} | |
int non_zero = 0; | |
for (int i = 0; i < ARRAY_SIZE; i += 1) | |
non_zero += array[i]; | |
if (!non_zero) { | |
printf("Values are all zero.\n"); | |
} else { | |
printf("Values are not all zero.\n"); | |
} | |
printf("\n"); | |
} | |
void question7() { | |
printf("Question 7\n"); | |
printf("----------\n\n"); | |
int ints[20] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200 }; | |
int *ip = ints + 3; | |
printf("Initial array: \n"); | |
printIntArray(ints, 20); | |
printf("\n"); | |
printf("ints: %p\n",ints); | |
printf("ints[4]: %d\n",ints[4]); | |
printf("*(ints + 4): %d\n",*(ints + 4)); | |
printf("*ints + 4: %d\n",*ints + 4); | |
printf("ip[-2]: %d\n",ip[-2]); | |
printf("&ip + 4: memory location of the ip pointer + (4 * 4 bytes)\n"); | |
printf("\n"); | |
} | |
void question8to10() { | |
printf("Questions 8 to 10\n"); | |
printf("-----------------\n\n"); | |
struct inner { | |
int x; | |
double y; | |
char *p; | |
} c1 = {4, 1.7, "Happy Halloween"}; | |
struct outer { | |
int x; | |
struct inner str_in; | |
} c2 = {2, c1}; | |
c2.str_in.p = "Happy New Year"; | |
printf("8: %s\n",c2.str_in.p); | |
struct inner *pointer = &c1; | |
printf("9: %s : %s\n",pointer->p,c1.p); | |
printf("10: Question is duplicate of 9.\n"); | |
printf("\n"); | |
} | |
void merge(int *array1, int *array2, int *array3) { | |
int arrayIndex1 = 0; | |
int arrayIndex2 = 0; | |
int arrayIndex = 0; | |
while (1) { | |
int integer1 = array1[arrayIndex1]; | |
int integer2 = array2[arrayIndex2]; | |
// are we at the end? | |
if (integer1 == -1 && integer2 == -1) return; | |
// figure out which integer is greater and then add that | |
if (integer1 == -1) array3[arrayIndex] = integer2, arrayIndex2 ++; | |
else if (integer2 == -1) array3[arrayIndex] = integer1, arrayIndex1 ++; | |
else if (integer1 < integer2) array3[arrayIndex] = integer1, arrayIndex1 ++; | |
else array3[arrayIndex] = integer2, arrayIndex2 ++; | |
arrayIndex ++; | |
} | |
} | |
void questionLong1() { | |
printf("Long Answer Question 1\n"); | |
printf("----------------------\n\n"); | |
int const arraySize = 11; | |
int const mergedArraySize = arraySize * 2 - 2; | |
int array[arraySize] = { 20, 31, 50, 88, 105, 666, 889, 890, 901, 905, -1 }; | |
int array2[arraySize] = { 1, 4, 8, 25, 68, 101, 546, 700, 705, 980, -1 }; | |
int array3[mergedArraySize]; | |
merge(array, array2, array3); | |
printf("Merged array:\n"); | |
printIntArray(array3, mergedArraySize); | |
printf("\n"); | |
} | |
// name changed since dup already exists in this namespace | |
// using the string.h module to get the length of the string | |
int aaron_dup(char *string) { | |
int size = (int)strlen(string); | |
if (size % 2 != 0) return 0; | |
for (int i = size / 2; i < size; i++) { | |
if (string[i - size / 2] != string[i]) return 0; | |
} | |
return 1; | |
} | |
void questionLong2() { | |
printf("Long Answer Question 2\n"); | |
printf("----------------------\n\n"); | |
char *string; | |
string = "giantsnake"; | |
//string = "redred"; | |
if (aaron_dup(string)) { | |
printf("String \"%s\" is duplicated.\n",string); | |
} else { | |
printf("String \"%s\" is not duplicated.\n",string); | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment