Add Week2, Week3, and Assignment1
This commit is contained in:
parent
df869f07c1
commit
edf292ea01
|
@ -1,3 +1,6 @@
|
|||
a.out
|
||||
*.dSYM/**
|
||||
|
||||
**/*.dSYM
|
||||
**/*.dSYM/**
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Structure to hold grayscale pixel data
|
||||
typedef struct {
|
||||
unsigned char value;
|
||||
} PixelGray;
|
||||
|
||||
// Function to read a PGM image into a 2D array
|
||||
PixelGray** readPGM(const char* filename, int* width, int* height);
|
||||
|
||||
// Function to write a 2D matrix as a PGM image
|
||||
void writePGM(const char* filename, PixelGray** matrix, int* width, int* height);
|
||||
|
||||
// Function to threshold the image matrix
|
||||
PixelGray** threshold(PixelGray** matrix, int* width, int* height);
|
||||
|
||||
// Function to rotate the image matrix
|
||||
PixelGray** rotate(PixelGray** matrix, int* width, int* height);
|
||||
|
||||
|
||||
//main function - DO NOT MODIFY
|
||||
int main() {
|
||||
int width, height; // variable to hold width and height. Use reference in other functions
|
||||
|
||||
PixelGray** image_original = readPGM("lenna.pgm", &width, &height);
|
||||
// Now you have the grayscale image data in the 'image_original' 2D array
|
||||
|
||||
// Access pixel data using image[row][col].value
|
||||
// For example, to access the pixel at row=2, col=3:
|
||||
// unsigned char pixel_value = image[2][3].value;
|
||||
|
||||
// Create a new 2D array 'image_thresh' to store the threshold image
|
||||
PixelGray** image_thresh = threshold(image_original, &width, &height);
|
||||
//write the image data as "threshold.pgm"
|
||||
writePGM("threshold.pgm", image_thresh, &width, &height);
|
||||
|
||||
// Create a new 2D array 'image_rotate' to store the rotated image
|
||||
PixelGray** image_rotate = rotate(image_original, &width, &height);
|
||||
//write the image data as "rotate.pgm"
|
||||
writePGM("rotate.pgm", image_rotate, &width, &height);
|
||||
|
||||
image_rotate = rotate(image_rotate, &width, &height);
|
||||
//write the image data as "rotate_again.pgm"
|
||||
writePGM("rotate_again.pgm", image_rotate, &width, &height);
|
||||
|
||||
// Free the allocated memory when you're done
|
||||
for (int i = 0; i < height; ++i) {
|
||||
free(image_original[i]);
|
||||
free(image_thresh[i]);
|
||||
free(image_rotate[i]);
|
||||
}
|
||||
free(image_original);
|
||||
free(image_thresh);
|
||||
free(image_rotate);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
|
||||
|
||||
int titleToNumber(char* columnTitle) {
|
||||
int len = strlen(columnTitle);
|
||||
|
||||
int factor = 1;
|
||||
int num = 0;
|
||||
|
||||
// Loop from the beginning
|
||||
int i = len-1;
|
||||
while (1) {
|
||||
// Get char
|
||||
char c = columnTitle[i];
|
||||
|
||||
// Convert char to int by using ASCII table and subtracting A=65
|
||||
// so 64
|
||||
int cNum = c - 64;
|
||||
|
||||
// Add sum
|
||||
num += cNum*factor;
|
||||
|
||||
// Perform check before increasing factor
|
||||
i--;
|
||||
if (i < 0) break;
|
||||
factor *= 26;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
// Has a max length of "FXSHRXW," so allocate buffer of 8.
|
||||
char out[8] = {0};
|
||||
|
||||
// Performs a counter with letters.
|
||||
void doInc(char* str) {
|
||||
(*str)++;
|
||||
if (*str == '[') {
|
||||
*str = 'A';
|
||||
return doInc(str - sizeof(char));
|
||||
}
|
||||
}
|
||||
|
||||
char* convertToTitle(int number) {
|
||||
|
||||
char* str = &out[6];
|
||||
// Fill array with default values
|
||||
strcpy(out, "@@@@@@@");
|
||||
while(number > 0) {
|
||||
// Increment first
|
||||
doInc(str);
|
||||
number--;
|
||||
}
|
||||
|
||||
str = out;
|
||||
while(*str == '@') str++;
|
||||
|
||||
// Return pointer to array starting from first character, 7-i.
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("%d\n", titleToNumber("A"));
|
||||
printf("%d\n", titleToNumber("B"));
|
||||
printf("%d\n", titleToNumber("C"));
|
||||
printf("%d\n", titleToNumber("Z"));
|
||||
printf("%d\n", titleToNumber("AA"));
|
||||
printf("%d\n", titleToNumber("AB"));
|
||||
printf("%d\n", titleToNumber("ZZ"));
|
||||
|
||||
printf("%s\n", convertToTitle(1));
|
||||
printf("%s\n", convertToTitle(2));
|
||||
printf("%s\n", convertToTitle(3));
|
||||
printf("%s\n", convertToTitle(26));
|
||||
printf("%s\n", convertToTitle(27));
|
||||
printf("%s\n", convertToTitle(28));
|
||||
printf("%s\n", convertToTitle(701));
|
||||
printf("%s\n", convertToTitle(2147483647));
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
#include "week3.c"
|
||||
#include<stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("%s", addBinary("11", "1"));
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
|
||||
size_t max(size_t a, size_t b) {
|
||||
if (a > b) return a;
|
||||
return b;
|
||||
}
|
||||
|
||||
char* addBinary(char *a, char *b) {
|
||||
// Get length of strings
|
||||
size_t l_a = strlen(a)+1;
|
||||
size_t l_b = strlen(b)+1;
|
||||
|
||||
// Final string length
|
||||
size_t l_x = max(l_a, l_b)+1;
|
||||
|
||||
// Create buffer
|
||||
char* outString = malloc(l_x);
|
||||
|
||||
// String end
|
||||
outString[l_x-1] = 0;
|
||||
// Index variable from end
|
||||
size_t i = 1;
|
||||
|
||||
// Index variables of a, b
|
||||
size_t ai = 1;
|
||||
size_t bi = 1;
|
||||
|
||||
uint8_t carry = 0;
|
||||
uint8_t tempSum = 0;
|
||||
|
||||
// Do a sum
|
||||
do {
|
||||
// Increment all our indices
|
||||
ai++;
|
||||
bi++;
|
||||
i++;
|
||||
|
||||
// Full adder logic for a_i, b_i, carry.
|
||||
char c_a = a[l_a-ai];
|
||||
char c_b = b[l_b-bi];
|
||||
|
||||
// Use XOR operator to add bits.
|
||||
tempSum = c_a^c_b^carry;
|
||||
|
||||
// Calculate carry (we only care about the 1st bit)
|
||||
carry = ((c_a^c_b)&carry) | (c_a&c_b);
|
||||
|
||||
// Create char that holds sum.
|
||||
char c = (tempSum & 1) ^ '0';
|
||||
|
||||
// Store into output
|
||||
outString[l_x-i] = c;
|
||||
} while (ai < l_a && bi < l_b);
|
||||
|
||||
// Still more in a.
|
||||
while (ai < l_a) {
|
||||
ai++;
|
||||
i++;
|
||||
|
||||
// Half adder
|
||||
char ca = a[l_a-ai];
|
||||
tempSum = ca^carry;
|
||||
carry = ca & carry;
|
||||
|
||||
char c = (tempSum & 1) ^ '0';
|
||||
outString[l_x-i] = c;
|
||||
}
|
||||
|
||||
// Still more in b.
|
||||
while (bi < l_b) {
|
||||
bi++;
|
||||
i++;
|
||||
|
||||
// Half adder
|
||||
char cb = b[l_b-bi];
|
||||
tempSum = cb^carry;
|
||||
carry = cb & carry;
|
||||
char c = (tempSum & 1) ^ '0';
|
||||
outString[l_x-i] = c;
|
||||
}
|
||||
|
||||
// Include the final carry bit.
|
||||
if (carry&1) {
|
||||
i++;
|
||||
outString[l_x-i] = '1';
|
||||
}
|
||||
|
||||
// Return string.
|
||||
return &outString[l_x-i];
|
||||
}
|
||||
|
Loading…
Reference in New Issue