You are on page 1of 10

Kevin Kim 8481082 07/10/13

COMP 2130 Intro. to Computer Systems


Assignment #2

1.

Kevin Kim 8481082 07/10/13 (3 marks) Write a function that makes a binary number of a given integer in the form of a character string, and saves the string into the array so that the caller function can print. This function should not print the binary number.

#include <stdio.h> char *toBinary(int n); /** * Kevin Kim * 9491082 */ int main() { printf("2147483647 to binary is: %s\n", toBinary(2147483647)); //largest possible signed int printf("1024 to binary is : %s\n", toBinary(1024)); printf("Kevin Kim, 9491082"); return 0; } /** * Converts integer value to binary string * @param integer * @return binary char[] */ char *toBinary(int n) { char *ptr = (char *)malloc(33); //Allocate 33 byes of memory since integer //contains 32 bits plus we require null terminating character *(ptr + 32)= '\0'; //For loop to fill the array with corresponding binary values int i; for (i = 0; i < 32; ++i) { *(ptr + (31 - i)) = (n & 1)? '1' : '0'; n = n >> 1; } //Returns pointer of first element of the array return ptr; }

Output

2.

Kevin Kim 8481082 07/10/13 (3 marks) Write a function that sets the target bit of an integer to 1, and returns the new value. For example, th when integers x and y are given to the function, the y bit in x should be set to 1. Note that y should be between 0 and 31 inclusively.

#include<stdio.h> int target(int x, int y); int main(){ printf("Setting fourth bit of integer 7: %d\n", target(7, 4)); printf("Setting 8th bit of integer 1: %d\n", target(1, 8)); printf("Kevin Kim 9491082\n"); return 0; } /** * Will alter the target bit of a given integer to 1 * @param int Number, int TargetBit * @return integer value with target bit flipped to 1 */ int target(int x, int y) { y--; //since we wish to flip the target bit //and not the bit after our intended target int targetNum = (y) ? 1 : 0; //Incase the inputted target is 0; //Will formulate an integer value of our desired target bit while (y > 0) { targetNum *= 2; y--; } x = x | targetNum;//Bitwise OR to set target bit to 1 return x; }

Output

3.

Kevin Kim 8481082 07/10/13 (3 marks) Write a C code to be able to list the contents of a text fil pagewise. Use command line arguments to be able to enter the file name. $display try.txt // should the list of contents of the try.txt page by page(it should show More-- at the end of the page)

#include<stdio.h> /** * @author Kevin Kim * @student 9491082 */ int main(int argc, char *argv[]) { //char buffer to read in characters char buffer[300]; FILE *fptr = fopen(argv[1], "rb"); int count = 0; //loops until fgets is null while (fgets(buffer, 300, fptr) != NULL) { printf("%s", buffer); count++; //If 20 lines are printed and more lines are left, will promt user with "MORE--" if (count == 20) { printf("MORE--"); getchar(); system("clear"); count = 0; } } printf("\n"); fclose(fptr); return 0; }

Output

Kevin Kim 8481082 07/10/13 4. (3 marks) Write a menu driven C code to create, read all and read specific record from the binary file. The structure is: struct book { int book_id; char book_name[25]; char author[25]; int price; }

creating book binary file


/** * Kevin Kim * 9491082 */ #include<stdio.h> struct book { int book_id; char book_name[25]; char author[25]; int price; }; int main() { FILE *file_ptr; struct book books; //Open and create binary file "test1.bin" file_ptr = fopen("test1.bin", "wb"); //Create first book record and write it to binary file books.book_id = 1111; strcpy(books.book_name, "Lord of the Rings"); strcpy(books.author, "Tolkien"); books.price = 15; fwrite(&books, sizeof(struct book), 1, file_ptr); //Create second book record and store to binary file books.book_id = 2222; strcpy(books.book_name, "Way of Zen"); strcpy(books.author, "Alan W Watts"); books.price = 13; fwrite(&books, sizeof(struct book), 1, file_ptr); //Create third book record and store to binary file books.book_id = 3333; strcpy(books.book_name, "Being in Time"); strcpy(books.author, "Heideggar"); books.price = 13; fwrite(&books, sizeof(struct book), 1, file_ptr); //Close file fclose(file_ptr); return 0; }

Kevin Kim 8481082 07/10/13 Eventdriven C program


/** * Kevin Kim * 9491082 */ #include<stdio.h> struct book { int book_id; char book_name[25]; char author[25]; int price; }; int main() { FILE *fptr; struct book books; //Open binary file at set it to file pointer fptr = fopen("test1.bin", "rb"); //Clear user screen system("clear"); printf("Welcome to the Library. Select option below\n"); printf("1 book1\n2 book2\n3 book3\n4 read all"); printf("\noption: "); int booknum; scanf("%d", &booknum); if (booknum < 4) printf("You have selected book number %d\n", booknum); else printf("Listing all the books: \n"); switch(booknum) { //option 1 will set books struct to first record of binary file // then print out its contents case 1: fread(&books, sizeof(struct book), 1, fptr); printf("ID: %d, Title: %s, Author: %s, $%d\n", books.book_id, books.book_name, books.author, books.price); break; //Option two will set books struct to second record of binary file // via fseek() case 2: fseek(fptr, sizeof(struct book), SEEK_SET); fread(&books, sizeof(struct book), 1, fptr); printf("ID: %d, Title: %s, Author: %s, $%d\n", books.book_id, books.book_name, books.author, books.price); break; //Set books to third record of binary file via fseek() case 3 : fseek(fptr, sizeof(struct book)*2, SEEK_SET); fread(&books, sizeof(struct book), 1, fptr); printf("ID: %d, Title: %s, Author: %s, $%d\n", books.book_id, books.book_name, books.author, books.price); break;

Kevin Kim 8481082 07/10/13


case 4: { int i; //For while loop to print all the records while (fread(&books, sizeof(struct book), 1, fptr) == 1) { printf("ID: %d, Title: %s, Author: %s, $%d\n", books.book_id, books.book_name, books.author, books.price); } break; } } printf("\n"); return 0; }

Output:

5.

Kevin Kim 8481082 07/10/13 (3 marks) Write a function that sets a certain number of bits in an integer to 1 and all the other bits to 0. The value should be returned from the function. For example, when integers x = 3 and y = 8 are passed to the function, the function should return the integer that has a binary number of 00000000 00000000 00000111 11111000.

#include<stdio.h> int xybinary(int x, int y); /** * Kevin Kim 9491082 */ int main() { printf("%d\n", xybinary(3, 8));//output of test will be 2040 printf("%d\n", xybinary(4, 1));//output of test will be 16 return 0; } /** * will set y bits to 1 and shift x amount to the left * @param int x, int y * @return modified integer */ int xybinary(int x , int y) { int output = 0; int ones = (y == 0) ? 0 : 1;//if y = 0, no bits will set to 1 //Will calculate corresponding binary value of y 1's while (y > 0) { ones *= 2; y--; } //subtract 1 to get y 1's then shift x amount to the left output = (ones - 1) << x; return output; }

Output

6.

Kevin Kim 8481082 07/10/13 (3 marks) Write a function that extracts a certain byte from a given integer and returns the extracted byte as nd an integer. For example, when two integers x and 2 are passed to the function, the function returns the 2 byte in x. This function should use the function in (5).

#include<stdio.h> #include"xybinary.h" int extract(int x, int y); int main() { printf("%d\n", extract(14790400, 2)); //Will output 175 printf("%d\n", extract(1023, 1));//Will output 255 printf("Kevin Kim 9491082"); return 0; } /** * Returns an intger value with the specified byte extracted * @param int num, int specified byte * @return extracyed byte as int */ int extract(int x, int y) { if (y == 0)//Will return -1 if the specified byte is 0 return -1; //set the extraction number set number of bytes int extractnum = xybinary((y - 1)*8, 8); //logical & to extract byte, then shift n bytes to the right return (x & extractnum) >> (y - 1)*8; }

Output

7.

Kevin Kim 8481082 07/10/13 (2 marks) Write a function to check even-parity of a given short integer. That is that the function returns 1 when the number of bit 1's is even, otherwise 0.

#include<stdio.h> short int parity(short int num); int main(){ printf("parity(8) = %d\n", parity(8)); printf("parity(7) = %d\n", parity(7)); printf("parity(3) = %d\n", parity(3)); printf("parity(15) = %d\n", parity(15)); printf("Kevin Kim 9491082\n"); return 0; } /** * function to check even parity of short integer type * @param short int number * @return int value of 1 or 0 (true/false) */ short int parity(short int num) { int count = 0; while (num > 0) { if (num & 1 == 1) ++count; num = num >> 1; } return (count % 2) ? 0 : 1; }

Output

You might also like