0% found this document useful (0 votes)
389 views10 pages

CZ1007 Assignment 1

CZ1007 Data Structures Assignment School of Computer Engineering Nanyang Technological University (C) 2014 Kenrick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
389 views10 pages

CZ1007 Assignment 1

CZ1007 Data Structures Assignment School of Computer Engineering Nanyang Technological University (C) 2014 Kenrick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT

CZ1007 Data Structure Assignment 1 Report


Details
Name: KENRICK Lab group: FSP1 Matriculation number: U1320141J

Program Listings
/* Author and Lab Group: KENRICK FSP1 Program name: FSP1_Kenrick.c Date: 03 March 2014 Purpose: Implementing the required functions for Assignment 1 */ #define _CRT_SECURE_NO_WARNINGS // to enable scanf at Visual Studio Professional 2013 #include <stdio.h> #include <string.h> #include <math.h> #define SIZE 4 #define SIZE1 5 #define SIZE2 10 struct student{ char name[20]; /* student name */ double testScore; /* test score */ double examScore; /* exam score */ double total; /* total score = test+exam scores */ }; typedef struct { double x; double y; } Point; typedef struct { Point topLeft; /* top left point of rectangle */ Point botRight; /* bottom right point of rectangle */ } Rectangle; /* function prototypes */ void readMatrix(int matrix[SIZE][SIZE]); void displayMatrix(int matrix[SIZE][SIZE]); void computeTotal(int matrix[SIZE][SIZE]); void compress(char data[SIZE1][SIZE2]); void squeeze(char str[], char c); char *strrchr2(char *s, char ch); void findWord(char word[][20], char *first, char *last); double computeArea(Rectangle *r); void computeScore(); int fun(int n); int countZeros(int num); void reverseAr(char ar[], int n);

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT

int main() { int choice; /* declare your other variables here */ int i, j; int q1[SIZE][SIZE]; char q2[SIZE1][SIZE2]; char q3_target, q3_str[500]; char q4_str[500], q4_target; char q5_str[5][20], q5_first[20], q5_last[20]; Rectangle q6_input; int q8_inp; int q9_inp; char q10_inp[500];

printf("\nPerform the following functions ITERATIVELY:\n"); printf("1: computeTotal()\n"); printf("2: compress()\n"); printf("3: squeeze()\n"); printf("4: strrchr2()\n"); printf("5: findWord()\n"); printf("6: computeArea()\n"); printf("7: computeScore()\n"); printf("8: fun()\n"); printf("9: countZeros()\n"); printf("10: reverseAr()\n"); printf("11: quit\n"); do { printf("\nEnter your choice: "); scanf("%d", &choice); fflush(stdin); switch (choice) { /* add your code here */ case 1: // input matrix readMatrix(q1); // print before computeTotal() printf("Before computeTotal():\n"); displayMatrix(q1); computeTotal(q1); // print after computeTotal() printf("After computeTotal():\n"); displayMatrix(q1); break; case 2: // input data printf("Enter your data (5x10 of characters):\n"); for (i = 0; i < SIZE1; i++) { for (j = 0; j < SIZE2; j++) { scanf("%c", &q2[i][j]); } fflush(stdin); }

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT


// output printf("The compression output:\n"); compress(q2); break; case 3: // Input string printf("Enter a string : "); scanf("%s", q3_str); fflush(stdin); // Input char printf("Enter a char : "); scanf("%c", &q3_target); fflush(stdin); // "squeeze" it then print it out squeeze(q3_str, q3_target); printf("Squeezed string = %s\n", q3_str); break; case 4: // Input string printf("Enter a string : "); scanf("%s", q4_str); fflush(stdin); // Input target char printf("Enter the target char in the string : "); scanf("%c", &q4_target); fflush(stdin); // Print a string starting from last occurence of "target char" printf("Resultant string = %s\n", strrchr2(q4_str, q4_target)); break; case 5: // Input 5 words printf("Enter 5 words separated by space: "); for (i = 0; i < 5; i++) { scanf("%s", q5_str[i]); } fflush(stdin); // find the first & last word (variable passed by reference) findWord(q5_str, q5_first, q5_last); printf("First word = %s, Last word = %s\n", q5_first, q5_last); break; case 6: // Input top-left point printf("Enter top left point: "); scanf("%lf %lf", &q6_input.topLeft.x, &q6_input.topLeft.y); fflush(stdin); // Input bottom-right point printf("Enter bottom right point: "); scanf("%lf %lf", &q6_input.botRight.x, &q6_input.botRight.y); fflush(stdin); // print top-left point

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT


printf("Top Left x = %lf", q6_input.topLeft.x); printf("y: %lf\n", q6_input.topLeft.y); // print bottom-right point printf("Bottom Right x = %lf", q6_input.botRight.x); printf("y: %lf\n", q6_input.botRight.y); // Print the area printf("Area = %lf\n", computeArea(&q6_input)); break; case 7: // Input-output inside computeScore() computeScore(); break; case 8: // Input number printf("Enter a number : "); scanf("%d", &q8_inp); // Output result printf("Result = %d\n", fun(q8_inp)); break; case 9: // Input number printf("Enter a number : "); scanf("%d", &q9_inp); // Output result printf("number of zeros = %d\n", countZeros(q9_inp)); break; case 10: // Input array of character printf("Enter an array of characters : "); scanf("%s", q10_inp); fflush(stdin); // Reverse the array reverseAr(q10_inp, strlen(q10_inp)); // Prints reversed array printf("The reversed array of characters is = %s\n", q10_inp); break; } } while (choice < 11); return 0; }

void readMatrix(int matrix[SIZE][SIZE]) { int i, j; printf("Enter matrix (4x4): \n"); for (i = 0; i<SIZE; i++) for (j = 0; j<SIZE; j++) scanf("%d", &matrix[i][j]); printf("\n"); }

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT

void displayMatrix(int matrix[SIZE][SIZE]) { int i, j; printf("Your matrix (4x4): \n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { printf("%d", matrix[i][j]); // print space if (j != SIZE - 1) printf(" "); } printf("\n"); } printf("\n"); }

/* Question 1 */ void computeTotal(int matrix[SIZE][SIZE]) { /* add your code here */ int i,j; for (i = 0; i < SIZE; i++) { // initialize rightmost column matrix[i][SIZE - 1] = 0; for (j = 0; j < SIZE - 1; j++) { // sum up to rightmost column matrix[i][SIZE - 1] += matrix[i][j]; } } }

/* Question 2 */ void compress(char data[SIZE1][SIZE2]) { /* add your code here */ char ch = 0; // contain previous character int ch_count = 0; //contain previous character count int i, j; for (i = 0; i < SIZE1; i++) { for (j = 0; j < SIZE2; j++) { if (ch != data[i][j]) { // if previous != current character if (ch_count > 0) printf("%d", ch_count); // make sure not to print right after initialization ch = data[i][j]; // change prev character to current ch_count = 1; // reset character count printf("%c", ch); } else { // if previous == current character ch_count++; } } printf("%d\n", ch_count); //end of row: print character count

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT


ch_count = 0; } } // and reset character count

/* Question 3 */ void squeeze(char str[], char c) { /* add your code here */ int i = 0, j = 0; // i: index of str (from input) // j: index of temp_str (for return) char temp_str[500]; while (str[i]) { // loop while string haven't reached // null character (terminating character) if (str[i] != c) { // if the current character is not c, add it to temp_str temp_str[j] = str[i]; j++; } i++; } temp_str[j] = '\0'; // append null character to return // copies temp_str to str i = 0; while (temp_str[i]) { str[i] = temp_str[i]; i++; } str[i] = '\0'; // append null character }

/* Question 4 */ char *strrchr2(char *s, char ch) { /* add your code here */ int i = 0; char *s_temp, *ret; // initialization ret = 0; s_temp = s; while (*s_temp) {

// points ret to null pointer // loop while value of s_temp is not null character

// if current character is ch, update ret to point to the current pointer if (*s_temp == ch) ret = s_temp; s_temp++; } return ret; }

/* Question 5 */ void findWord(char word[][20], char *first, char *last) { /* add your code here */ int i = 0;

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT


// initialization strcpy(first, word[0]); strcpy(last, word[0]); // find max & min string // like finding max & min number for (i = 0; i < 5; i++) { if (strcmp(first, word[i]) > 0) strcpy(first, word[i]); if (strcmp(last, word[i]) < 0) strcpy(last, word[i]); } }

/* Question 6 */ double computeArea(Rectangle *r) { /* add your code here */ // area = (absolute difference of x) * (absolute difference of y) return fabs(r->topLeft.x - r->botRight.x) * fabs(r->topLeft.y - r->botRight.y); }

/* Question 7 */ void computeScore() { /* add your code here */ // initialization struct student db[50]; int n = 0; double total = 0, avg = 0; char temp_str[20]; while (1) { // break only when END is at input // Input student name printf("Enter student name: "); gets(temp_str); fflush(stdin); if (strcmp(temp_str, "END") == 0) // break only when END is at input break; else { // input temp_str to database n++; strcpy(db[n].name, temp_str); } // Input test score printf("Enter test score: "); scanf("%lf", &db[n].testScore); fflush(stdin); // Input exam score printf("Enter exam score: "); scanf("%lf", &db[n].examScore); fflush(stdin); // compute total score db[n].total = (db[n].testScore + db[n].examScore) / 2.0; total += db[n].total; //for computing overall average

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT


// print current student's total score printf("Student %s total = %lf\n", db[n].name, db[n].total); } //compute overall average & prints it avg = total / n; printf("Overall average: %lf\n", avg); }

/* Question 8 */ int fun(int n) { /* add your code here */ // directly from problem specification if (n <= 1) return 1; else if (n % 2 == 0) // if n is even return fun(n / 2); else // if n is odd return 2 * fun((n - 1) / 3); }

/* Question 9 */ int countZeros(int num) { /* add your code here */ // each recursion: check last digit // note: (num % 10 == 0) returns 1 if true, 0 if false; if (num < 10) return else return } // if checking the ones, don't recurse anymore (num % 10 == 0); // else: recurse (num % 10 == 0) + countZeros(num / 10);

/* Question 10 */ void reverseAr(char ar[], int n) { /* add your code here */ // initialization char temp = '\0'; if (n > 1) { // if n == 1 or 0, no need to reverse anymore // swap front character with the back character temp = ar[0]; ar[0] = ar[n - 1]; ar[n-1] = temp; reverseAr(++ar, n - 2); // ++ar: increment the string pointer, to point to the next // character in the front which will be swapped // n - 2: points to the next character at the back to be swapped // with the character in the front; // because of ++ar, the string length decrement by 1, // hence (n - 2) instead of (n - 1) } }

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT

Test Cases
Problem 1

Problem 2

Problem 3

Problem 4

Problem 5

Problem 6

KENRICK (FSP1) U1320141J CZ1007 DATA STRUCTURE ASSIGNMENT 1 REPORT

Problem 7

Problem 8

Problem 9

Problem 10

10

You might also like