Вы находитесь на странице: 1из 8

PROGRAMMING FUNDAMENTALS

DCT 1083 1ST SEMESTER SESSION 2010/2011 ASSIGNMENT #3 Write a program of managing memory allocation and string PREPARED FOR: MDM. AINI ZURIATY DONE BY: TENGKU RABIATULADAWIYAH BTE TENGKU AHMAD ROSDI 10A05068 NOORUL HETTY SULYIANA BTE AHMAD BASRI 10A05054 1ST YEAR NETWORKING (NT)

SIGNITURE: .. SIGNITURE: .. DATE PRINTED:


1| Page

Part A [20 Marks] Memory Allocation 1. Explain What static And dynamic memory is.

With static memory, you define the maximum amount of space required. char a[1000]; There no way to change amount of memory in runtime. Dynamic memory comes in blocks without names, just address. 2. Malloc() function is used to requesta block of certain size (in byte). Show the example. int memsize; // memsize get a value at runtime x=malloc(memsize); 3. Explain what type of cast is. The void pointer variable allows you to store and retrieve any kind of address (integer, float, double, string etc). Since the pointer can point to anything, the system cannot know if it should read or write a 1-byte or a 4-byte integer, or something completely different. System need to be informed- what kind of data is stored. Void *x; (int *)x; It is called type-cast. 4. Define resizing block of memory. Sometime you have to resize a block of dynamic memory after you have already stored data in it. The C++ library provides the realloc() function for this purpose. void *x *y; x=malloc(1000); // initial block Y= realloc(x,2000);// doubled realloc() attempts to grow the original size to accommodate the new requested size. If successful:- it returns the blocks unchanged. If it not possible to grow the old block (not enough adjacent free space in memory):2| Page

realloc() will instead allocate a new block in a different part of memory; copy all over of the old data to the new block and return the address of that new block. Upon failure, the realloc() returns the NULL pointer instead. 5. What is used of free() function. free() function is used to return it (memory) to the pool. free(x); Every call to malloc() must be balanced by a call to free(). String Explain the purpose of each following function. Provide a sample code and output where necessary. No 0 Function strcpy Purpose Copies the string pointed by source into the array pointed by destination, including the terminating null character. Return the length of the string s, excluding the null character. Return a value<0 if s 1 is less than s2.Return 0 if s1 and s2 are the same.Return a value >0 if s1 is greater than s2. Copies the string pointed by source into the array pointed by destination, including the terminating null character. Copies the first num characters of source to destination. If the end of the source C string is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed Code Char str1[]="ABC"; char str2[]=XYZ; strcpy (str2,str1); cout<<str1,str2; cout<<strlen(ABC); cout<<strcmp(kim, pue) cout<<strcpy(kim) Output ABCABC

ABC kim, pue

strlen(s)

strcmp(s1, s2)

kim

strcpy

strncpy

char str1[]= "To be or not to be"; char str2[6]; strncpy (str2,str1,5); str2[5]='\0'; puts (str2); char str[80]; strcpy (str,"these "); strcat (str,"strings "); strcat (str,"are "); strcat (str,"concatenated."); puts (str);

To be

strcat

these strings are concatenated.

3| Page

by the concatenation of both in destination. Checks if parameter c is either a decimal digit or an uppercase or lowercase letter. The result is true if either isalpha or isdigit would also return true int i; char str[]="c3po..."; i=0; while (isalnum(str[i])) i++; printf ("The first %d characters are alphanumeric.\n",i); int i=0; char str[]="Test String.\n"; char c; while (str[i]) { c=str[i]; if (islower(c)) c=toupper(c); putchar (c); i++; int c; int i=0; char str[]="Example sentence to test isspace\n"; while (str[i]) { c=str[i]; if (isspace(c)) c='\n'; putchar (c); i++; } int i=0; char str[]="Test String.\n"; char c; while (str[i]) { c=str[i]; putchar (tolower(c)); i++; } int i=0; char str[]="Test String.\n"; The first 4 characters are alphanumeric .

isalnum

islower

Checks if parameter c is a lowercase alphabetic letter. Notice that what is considered a letter may depend on the locale being used; In the default C locale, a lowercase letter is any of: a b c d e f g h i j k l
m n o p q r s t u v w x y z.

TEST STRING.

isspace

Checks if parameter c is a whitespace character. Specific compiler implementations or locales may define additional white-space characters in the extended character set

Example sentence to test isspace

test string.

tolower

Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged. Converts parameter c to its uppercase equivalent if c is a lowercase letter and has an

10

toupper

TEST STRING.

4| Page

uppercase equivalent. If no such conversion is possible, the value returned is c unchanged.

char c; while (str[i]) { c=str[i]; putchar (toupper(c)); i++; }

5| Page

Part B [30 marks] Instruction: You are required to prepared (i) pseuedocode and (ii) c++ program for each of the following problems. 1. Find the length of network that complementary strands are represented as strings. The strand size given is 100.

(i)

C++ programming

(ii) pseudocode Prompt p7923lease enter 100 characters Input getline Display your string Show character long While strlen 6| Page

Prompt done

2. The program should Reads firstname and lastname and then prints it with combination of two names using string methods.. Input: Abu Bakar Output: (i) Abu Bakar

C++ programming

ii) pseudocode Prompt please enter 10 first and last name Print first name Show first name Display lastname Get lastname Prompt fullname 7| Page

3.Given data molloc(1000). Print the starting addresses that return the block into pool. (i) c++ programming

(ii)Psuedocode Prompt void function Display starting block

8| Page

Вам также может понравиться