Solutions to Assessment

Similar documents
Library Functions. General Questions

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

BİL200 TUTORIAL-EXERCISES Objective:

Technical Questions. Q 1) What are the key features in C programming language?

ALGORITHM 2-1 Solution for Exercise 4

C programming basics T3-1 -

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Intermediate Programming, Spring 2017*

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Engineering program development 7. Edited by Péter Vass

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

CS 0449 Sample Midterm

Strings. Daily Puzzle

OBJECTIVE QUESTIONS: Choose the correct alternative:

M.CS201 Programming language

today cs3157-fall2002-sklar-lect05 1

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

M1-R4: Programing and Problem Solving using C (JAN 2019)

UNIT IV-2. The I/O library functions can be classified into two broad categories:

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];


Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Computer Programming Unit v

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Variation of Pointers

Model Viva Questions for Programming in C lab

Strings and Library Functions

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Standard File Pointers

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Approximately a Final Exam CPSC 206

Lecture 03 Bits, Bytes and Data Types

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

C mini reference. 5 Binary numbers 12

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

CSI 402 Lecture 2 Working with Files (Text and Binary)

Computational Methods of Scientific Programming Fall 2007

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Lectures 5-6: Introduction to C

File I/O. Last updated 10/30/18

Chapter 8 C Characters and Strings

CS16 Exam #1 7/17/ Minutes 100 Points total

Preview from Notesale.co.uk Page 2 of 79

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Multiple Choice Questions ( 1 mark)

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Stream Model of I/O. Basic I/O in C

File I/O. Arash Rafiey. November 7, 2017

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Procedural Programming

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Today s Learning Objectives

Binghamton University. CS-220 Spring Includes & Streams

C Programming Language Review and Dissection III

Advanced C Programming Topics

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

C introduction: part 1

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

C Programming Multiple. Choice

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

PROGRAMMAZIONE I A.A. 2017/2018

Q 1. Attempt any TEN of the following:

A Crash Course in C. Steven Reeves

Chapter 8 Character Arrays and Strings

Lectures 5-6: Introduction to C

UNIT-I Input/ Output functions and other library functions

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI 171 Chapter Outlines

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

SAE1A Programming in C. Unit : I - V

Preprocessor Directives

Advanced C Programming and Introduction to Data Structures

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Computers Programming Course 5. Iulian Năstac

C for C++ Programmers

C Basics And Concepts Input And Output

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

COP 3223 Final Review

Problem Solving and 'C' Programming

Assignment #5 Answers

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Programming refresher and intro to C programming

(3-1) Functions II H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (September 5, 2018) Washington State University

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

High Performance Programming Programming in C part 1

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

CS201- Introduction to Programming Current Quizzes

Final Intro to C Review

Transcription:

Solutions to Assessment 1. In the bad character rule, what are the values of R(P) for the given pattern. CLICKINLINK (Please refer the lecture). Ans: a) C-1,K-10,N-9,I-8,L-2 b) C-1,K-10,N-9,I-8,L-7 c) C-4,K-10,N-9,I-8,L-7 d) C-1,K-5,N-9,I-8,L-7 Ans: c 2. What is the preprocessing time and space complexity using bad character rule in Boyer Moore algorithm. a) O(n) time and O(K) space, where n is length of pattern and K is size of alphabet set. b) O(log n) time and O(K) space, where n is length of pattern and K is size of alphabet set. c) O(n+K) time and O(n+K) space, where n is length of pattern and K is size of alphabet set. d) O(K) time and O(n) space, where n is length of pattern and K is size of alphabet set. Ans: a 3. What is the worst case time complexity of Boyer Moore string matching algorithm when the length of pattern is n and length of the text is m. a) O(n) b) O(m) c) O(n*m) d) (On+m) Ans: c 4. What is the time complexity of Boyer Moore algorithm when all the characters in the text and pattern are same. For e.g. T = AAAAAAAAAAAAAAAAAA, P= AAAAA. Length of T is m and length of P is n. a) O(m) b) O(n) c) O(n*m) d) O(n+m) Ans: c 5. Boyer moore is a left to right comparison algorithm. a) True

b) False 6. Which of the following is true about the following declaration? FILE *fp; a) FILE is a keyword in C for representing files and fp is a variable of FILE type b) FILE is a structure and fp is a pointer to the structure of FILE type c) FILE is a stream d) FILE is a buffered stream 7. When fopen() is not able to open a file, it returns a) EOF b) NULL c) Runtime Error d) Some compiler dependent value 8. If you create a file with the same name as an existing file, you will be prompted to rename the file. TRUE or FALSE? a) True b) False 9. What is the output of this C code? #include <stdio.h> int main() char buf[12]; stderr = stdin; fscanf(stderr, "%s", buf); printf("%s\n", buf); a) Compilation Error b) Undefined behavior c) Whatever the user types as input to the program

d) None of the above Ans: c 10. Files of which of the following types can be created? a) Text b) Binary oth 11. Which one of the following is valid for opening a read-only ASCII file? a) fileopen (filename, "r"); b) fileopen (filename, "read"); c) fopen (filename, "readonly"); d) fopen (filename, "r"); Ans: d 12. Which of the following function declarations indicate a bad design? a) int add(int,int); b) int add(float, int); c) float add(int,int); d) float add(float,int); and c 13. void funct(void) The above function declaration indicates a) it returns nothing and takes no arguments b) it returns nothing and takes arguments c) it returns a value and takes arguments d) it returns a value and takes no arguments Ans: a 14. If the number of actual arguments to a function call does not match with formal arguments, it will result in a) no error b) compilation error c) logical error d) syntax error

15. Any function can be called from any other function. TRUE or FALSE? a) True b) False Ans: a 16. strcmp() is a function used for: a) Copying one string to another b) Comparing two strings c) Concatenating two strings d) None of these 17.Input/output function prototypes and macros are defined in which header file? a) conio.h b) stdlib.h c) stdio.h d) math.h Ans: C 18. Which of the following library functions can be used to convert a string to a integer? a) itoa b) atoi c) ltoa d) ultoa

Solutions to Programming Assignments Question 1: Given a text T[0..n-1] and a pattern P[0..m-1]. Print the starting index of all the occurrences of pattern P in text T. If pattern P doesn t occur in T, then print '-1'. You are supposed to implement Boyer Moore algorithm using bad character rule as explained in the lecture. Input: First line contains the text T. Second line contains the pattern P Output: Index of each occurrence of pattern P in small brackets as shown in the examples below. Constraints: 1 <= length(p) <= length(t) < 100 Examples: Input 1: ABABABBAC ABA Ouput 1: (0)(2) Input 2: CCDCCDC DC Output 2: (2)(5) Input 3: XYZYZ ZX Output 3:

-1 Test Cases: Public test cases: Input ABABABBBBABDBA BAB XYXYYXYYXYYY XYYX AAAAAAAAAAA AAAAAAAAAA XYAZZJJA NNB ABABABABAB AB Output (1)(3)(8) (2)(5) (0)(1) -1 (0)(2)(4)(6)(8) Private test cases: Input XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X ABCDABCDAABBAABBACDACDYAAB Y ABCDEFGHIJKLMNOPQRST JKL Output (0)(1)(2)(3)(4)(5)(6)(7)(8) (9)(10)(11)(12)(13)(14)(15) (16)(17)(18)(19)(20)(21)(22 )(23)(24)(25)(26)(27)(28)(2 9)(30)(31)(32)(33)(34)(35) (22) (9) THISISFOO THISISFOO XXYYXYXYZZYZZAAB CCC (0) -1 Solution:

#include<limits.h> #include<string.h> #include<stdio.h> #define CHAR_SET_SIZE 256 /* A function to get maximum of two integers */ int maximum (int a, int b) return (a > b)? a: b; /* Preprocessing function for Boyer Moore's bad character rule */ /* R[i] is the last occurrence of the character 'str[i]' in the pattern 'str'.(refer lecture) */ void preprocess(char *str, int size, int R[CHAR_SET_SIZE]) int i; for(i = 0; i < CHAR_SET_SIZE; i++) R[i] = -1; for(i = 0; i < size; i++) R[(int) str[i]] = i; /* Complete this function using Boyer Moore's bad character rule */ /* For pre processing use the function preprocess() defined above */ /* You also have to print the output in this function */ void patternmatcher( char *T, char *P) int m = strlen(p); int n = strlen(t); int countnum=0; int R[CHAR_SET_SIZE]; //Bad Character Array preprocess(p, m, R); // Fill R by calling preprocess() for the given Pattern P int s = 0; // s is shift of the Pattern P with respect to text T while(s <= (n - m))

int j = m-1; /* Decrease index j of P while characters of P and T match at this shift s */ while(j >= 0 && P[j] == T[s+j]) j--; /* If P matches at current shift, then j becomes -1 after the above loop */ /* Shift P so that the next character of T aligns with the last occurrence of it in P */ if (j < 0) printf("(%d)", s); countnum++; s += (s+m < n)? m-r[(int)t[s+m]] : 1; //tests if P occurs at the end of T else /* Shift P so that the bad character in T aligns with the last occurrence of it in P.*/ s += maximum(1, j - R[(int)T[s+j]]); if(countnum == 0) printf("-1"); //end of patternmatcher() /** Main function */ int main() char T[100]; char P[100]; scanf("%s%s",t,p); patternmatcher(t, P); return 0;