You are on page 1of 14

Part 1

1.1.1 C Programming Code

/****************************************************************

***** Name : Chong Jia Zheng ******

***** SID : 09ACB02875 ******

***** Practical : P9; ******

******************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int ackermann(int m,int n); //function for ackermann

static unsigned int call; //number of function call

int main(void)

int m_value, n_value, acker_value;

double diff;

time_t start,end;

for(m_value=0;m_value<5;m_value++)

for(n_value=0;n_value<10;n_value++)

call=0;

if (m_value ==4 &&n_value==1){

printf("**********END***********");

break;

time(&start); //start of the time

1
acker_value = ackermann ( m_value, n_value);

time(&end); //end of the time

printf(" m : %-2d n : %-3d Ackermann value : %-5d\t",m_value,n_value, acker_value);

printf("call : %-8u\t",call); //time of function call

diff=difftime( end , start); //the time taken of function

printf("Time:%.2f\n",diff);

getchar();

return 0;

int ackermann(int m,int n) //function for ackermann

call++;

if ( m == 0 ){

return ( n+1 ); // if m = 0 , then n+1

}else if ( ( m > 0 ) && ( n == 0 ) ){

return ( ackermann( (m-1 ),1) );

// if m >0 and n =0 , then A(m - 1,1)

} else if ( ( m > 0 ) && ( n > 0 ) ){

return ( ackermann ( ( m - 1 ) , ackermann ( m , n-1 ) ) );

// if m > 0 and n > 0 , then A(m-1),A(m,n-1)

2
1.1.2 Print Screen

3
1.2 Ackermann Value Table
m n Ackermann Number of Time Taken
value Function to Run the
Called Function
0 0 1 1 0.00

0 1 2 1 0.00

0 2 3 1 0.00

0 3 4 1 0.00

0 4 5 1 0.00

0 5 6 1 0.00

0 6 7 1 0.00

0 7 8 1 0.00

0 8 9 1 0.00

0 9 10 1 0.00

1 0 2 2 0.00

1 1 3 4 0.00

1 2 4 6 0.00

1 3 5 8 0.00

1 4 6 10 0.00

1 5 7 12 0.00

1 6 8 14 0.00

1 7 9 16 0.00

1 8 10 18 0.00

1 9 11 20 0.00

2 0 3 5 0.00

2 1 5 14 0.00

2 2 7 27 0.00

2 3 9 44 0.00
4
2 4 11 65 0.00

2 5 13 90 0.00

2 6 15 119 0.00

2 7 17 152 0.00

2 8 19 189 0.00

2 9 21 230 0.00

3 0 5 15 0.00

3 1 13 106 0.00

3 2 29 541 0.00

3 3 61 2432 0.00

3 4 125 10307 0.00

3 5 253 42438 0.00

3 6 509 172233 0.00

3 7 1021 693964 0.00

3 8 2045 2785999 0.00

3 9 4093 11164370 1.00

4 0 13 107 0.00

4 1 Over run Over run Over run

1.3. Based on the Ackermann value table, when the m = 3 and n= 9, the
Ackermann values start to have a drastic difference. It is because when the m=3,
the value of the Ackermann is equal to 5+8*(2^n-1) which is power 2 to the
value of n. Therefore, the Ackermann values are m = 3 series will increase
dramatically as ‘n’ increase. Therefore, when m = 3 and n=9, Ackermann values
which has been increased 2048 consider to have a drastic difference.

5
PART 2
2.1 C Programming Code
/****************************

Name : Chong Jia Zheng

SID : 09ACB02875

Practical : P9;

******************************/

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <malloc.h>

#include <ctype.h>

#define length 100 //memory allocation

void EnterName(); //allow user to enter name

void SortNameInAscending(char *StuName[],int size); //bubble sort the Name

void PrintName(char *StuName[],int size); //print name after sort

int SearchName(char *StuName[],int size); //user search name

//variable

int i ; //for all function loop

6
int main()

printf("\t\t*********************************************\n");

printf("\t\t** ASSESSMENT 1 Part 2 **\n");

printf("\t\t*********************************************\n");

printf("\t\t** NAME : CHONG JIA ZHENG **\n");

printf("\t\t** SID : 09ACB02875 **\n");

printf("\t\t** PRACTICAL : P9 **\n");

printf("\t\t*********************************************\n");

printf("\n\t\t\tPress Any Key To Continue");

getchar(); //pause and continue when user click in

fflush(stdin); //clear the extra character

system("cls");

EnterName(); //forward to EnterName function

printf("\n\n\nPress Any Key To Close Program");

getchar(); //close the program

return 0;

void EnterName()

7
char *StuName[10]; //max 10 student in program

FILE *document; //file for the student name

char name[10][30]; //student name size

int size; //number of student name input

do{

printf("How Many Name You want(Maximum 10 Student)?");

scanf("%d",&size); //get the size of user input

fflush(stdin);

}while(size >=11 || size <=0);

for (i=0;i<size;i++)

StuName[i]=(char*)malloc(length); //memory allocation

printf("Please Enter Student Name \n ");

printf("------------------------------\n");

document = fopen("name.txt","r"); //open the name list file

/*if no name list then allow user input, else use the name list */

if (document == NULL){

for(i=0;i<size;i++){

printf("Student %2d : ",i+1);

scanf("%[^\n]s",&name[i]);

fflush(stdin);

StuName[i]=name[i];

8
}

}else{

for(i=0;i<size;i++){

fscanf(document," %[^\n]s",name[i]);

StuName[i]=name[i];

fclose(document);

/*print the Student Name*/

for(i=0;i<size;i++){

printf("Student %2d : %s\n",i+1,StuName[i]);

SortNameInAscending(StuName,size);

PrintName(StuName,size);

SearchName(StuName,size);

/*use bubble sort to sort the name list*/

void SortNameInAscending(char *StuName[],int size)

int j ;

char *temp;

for(i = 0; i < size; i++){

for(j = i + 1; j < size; j++){

if(strcmp(StuName[i], StuName[j]) > 0){

9
temp = StuName[i];

StuName[i] = StuName[j];

StuName[j] = temp;

/*Print the Name list After Sort*/

void PrintName(char *StuName[],int size)

printf("\nSort Accoding Alphabetical order \n");

printf("----------------------------------\n");

for(i = 0; i < size; i+=1){

printf("Sort [%3d] : %s\n",i+1, StuName[i]);

int SearchName(char *StuName[],int size)

char SName[30];

int search[10];

printf("\nPlease Enter A NAME TO SEARCH: ");

scanf("%[^\n]s",&SName);

fflush(stdin);

//convert all character to lower case if user input uppercase

for(i=0;SName[i];i++)

SName[i] = tolower(SName[i]);

10
}

for(i=0;i<size;i++){

search[i] = strcmp(StuName[i],SName);

if (search[i]==0){

break;

//print Name and Number of Student if found

//return to main if not found the name

if (search[i] == 0){

printf("Name Found : %s\n",SName);

printf("Location : Sort %d",i+1);

}else{

printf("Name Not Found");

return -1;

2.2 Print Screen


Open Program

11
Allow User Enter Student Number

Enter Student Name

12
Sort the name and print in alphabetical order

User search Name

13
14

You might also like