Commit 0c892853 by Yongshan Ding

Initial commit

parents
File added
CCBIN=/usr/bin/gcc
CC=$(CCBIN) -Wall -Wextra -std=c99 -pedantic -g
default: cyclic
cyclic: cyclic.h cyclic.c cyclic_test.c
$(CC) -O3 -o cyclic cyclic.c cyclic_test.c
puzzles: puzzles.h puzzles.c puzzles_test.c
$(CC) -O3 -o puzzles puzzles.c puzzles_test.c
.PHONY: clean
clean:
rm -Rf *.o cyclic puzzles *.dSYM
.PHONY: package
package:
tar -cvzf lab0-handin.tgz cyclic.c cyclic.h cyclic_test.c puzzles.c puzzles.h puzzles_test.c
\ No newline at end of file
/* CMSC 16200 - Lab 0
* File: cyclic.c
*
* Name: (YOUR NAME HERE)
* CNet: (YOUR CNET ID HERE)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "cyclic.h"
/* countDigits:
* counts the number of digits in a number
*/
int countDigits(int num) {
// Implement this function
return 42;
}
/* isFullyRepeated:
* determines if the number consists of fully repeated pattern
*/
bool isFullyRepeated(int num) {
// Implement this function
return false;
}
/* rightRotateDigit:
* rotates the digits in num by d places to the right
*/
int rightRotateDigit(int num, int d) {
// Implement this function
return 42;
}
/* leftRotateDigit:
* rotates the digits in num by d places to the left
*/
int leftRotateDigit(int num, int d) {
// Implement this function
return 42;
}
/* isCyclicBad:
* naive implementation for determining if a number is a cyclic number
*/
bool isCyclicBad(int num) {
// Implement this function
return false;
}
/* isPrime:
* determines if a number is a prime
*/
bool isPrime(int num) {
// Implement this function
return false;
}
/* kthCyclic:
* finds the kth cyclic number using Fermat's quotient form
*/
int kthCyclic(int k, int flag) {
// Implement this function
return 42;
}
/* CMSC 16200 - Lab 0
* File: cyclic.h
*/
#ifndef _CYCLIC_H_
#define _CYCLIC_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* countDigits:
* counts the number of digits in a number
*/
int countDigits(int num);
/* isFullyRepeated:
* determines if the number consists of fully repeated pattern
*/
bool isFullyRepeated(int num);
/* rightRotateDigit:
* rotates the digits in num by d places to the right
*/
int rightRotateDigit(int num, int d);
/* leftRotateDigit:
* rotates the digits in num by d places to the left
*/
int leftRotateDigit(int num, int d);
/* isCyclicBad:
* naive implementation for determining if a number is a cyclic number
*/
bool isCyclicBad(int num);
/* isPrime:
* determines if a number is a prime
*/
bool isPrime(int num);
/* kthCyclic:
* finds the kth cyclic number using Fermat's quotient form
*/
int kthCyclic(int k, int flag);
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "cyclic.h"
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: ./cyclic <k>\n");
return 1;
}
// Extract information from command line
int k = atoi(argv[1]);
// Begin of tests
assert(countDigits(123) == 3);
assert(isFullyRepeated(123123) == true);
assert(isFullyRepeated(12345123) == false);
assert(isFullyRepeated(333) == true);
assert(rightRotateDigit(12345, 2) == 45123);
assert(leftRotateDigit(12345, 2) == 34512);
assert(isCyclicBad(142857) == true);
assert(isPrime(2) == true);
assert(isPrime(15) == false);
assert(kthCyclic(1, 0) == 7); // for 142857
assert(kthCyclic(2, 0) == 17); // for 0588235294117647
// End of tests
kthCyclic(k, 1); // Print cyclic number to stdout
return 0;
}
/* CMSC 16200 - Lab 0
* File: puzzles.c
*
* Name: (YOUR NAME HERE)
* CNet: (YOUR CNET ID HERE)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
#include "puzzles.h"
/* Begin of Puzzle 1 */
/* VOL computes the product of its three arguments */
#define VOL(X,Y,Z) X*Y*Z
int volume() {
int v = VOL(1+1,2+2,3+3);
printf("volume = %d\n", v);
return v;
}
int puzzle1() {
volume();
return 0;
}
/* End of Puzzle 1 */
/* Begin of Puzzle 2 */
/* swap function swaps the values of a and b */
void swap(int *x, int *y) {
int *temp;
temp = x;
x = y;
y = temp;
return;
}
int puzzle2() {
int a = 2;
int b = 3;
printf("Before: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After : a = %d, b = %d\n", a, b);
return 0;
}
/* End of Puzzle 2 */
/* Begin of Puzzle 3 */
void add1(int *p) {
printf("Before: %d\n", *p);
*p = *p + 1;
printf("After : %d\n", *p);
}
int puzzle3() {
int* p = malloc(sizeof(int));
add1(p);
free(p);
return 0;
}
/* End of Puzzle 3 */
/* Begin of Puzzle 4 */
/* test_safe_add function can add any two integers without error */
bool test_safe_add(int a, int b) {
if (a > 0 & b > 0) {
return a + b > 0;
}
if (a < 0 & b < 0) {
return a + b < 0;
}
return true;
}
int puzzle4() {
int a = INT_MAX - 3;
int b = 5;
if (!test_safe_add(a,b)) {
printf("Unsafe addition detected: %d + %d\n", a, b);
return 1;
} else {
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}
}
/* End of Puzzle 4 */
/* Begin of Puzzle 5 */
/* test_safe_div function divides any two integers without error */
bool test_safe_div(int a, int b) {
return b != 0;
}
int puzzle5() {
int a = 42;
int b = 0;
if (!test_safe_div(a,b)) {
printf("Unsafe division detected: %d / %d\n", a, b);
return 1;
} else {
printf("%d / %d = %d\n", a, b, a / b);
return 0;
}
}
/* End of Puzzle 5 */
/* Begin of Puzzle 6 */
/* set_vector function fills a length-n array with integer v */
void set_vector(int *vec, int n, int v) {
for (int i = 0; i <= n; i++) {
vec[i] = value;
}
return;
}
int puzzle6() {
int* vec_i = malloc(3*sizeof(int));
int value_i = 0;
if (vec_i == NULL) {
printf("malloc failed.\n");
return 1;
} else {
set_vector(vec_i, 3, value_i);
printf("A vector of integers: %d\n", vec_i[0]);
free(vec_i);
}
return 0;
}
/* End of Puzzle 6 */
/* CMSC 16200 - Lab 0
* File: puzzles.h
*/
#ifndef _PUZZLES_H_
#define _PUZZLES_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int puzzle1();
int puzzle2();
int puzzle3();
int puzzle4();
int puzzle5();
int puzzle6();
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "puzzles.h"
int main(void) {
printf("== puzzle 1 ==\n");
puzzle1();
printf("== puzzle 2 ==\n");
puzzle2();
printf("== puzzle 3 ==\n");
puzzle3();
printf("== puzzle 4 ==\n");
puzzle4();
printf("== puzzle 5 ==\n");
puzzle5();
printf("== puzzle 6 ==\n");
puzzle6();
return 0;
}
\newcommand{\labname}{Lab 0}
\documentclass[11pt]{article}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{graphicx}
\usepackage{fancyhdr}
% TODO: CHANGE THESE !!!
\newcommand{\myname}{Change Me} % Full Name
\newcommand{\mycnetid}{changeme} % CNetID
% header
\pagestyle{fancy}
\fancyhf{}
\lhead{CMSC 16200 \labname}
\chead{\myname{} (\mycnetid)}
\rhead{Page \thepage{} of \pageref{mylastpagelabel}}
% document starts
\begin{document}
\paragraph{Task XXX}
% Answer for Task XXX goes in here:
% This label is used in the header to keep track of the page count.
\label{mylastpagelabel}
\end{document}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment