(IX) Ch-19 Functions in QBASIC

1. Fill in the gaps: a) Two types of functions supported by QBASIC are user-defined and Built-in or Library functions. b) All functions manipulate data and return a value. c) MID$ function is used to extract the specified characters from the left most character of a string. d) LEN function is used to find the length of the string. e) CHR$ function is used to find the character using ASCII character. f) The SQR function returns the square root of any positive number. g) SGN function is used to return the sign of a number. h) DATE$ function returns system date. 2. True of false a. A function must returns multiple values at a time. False b. LEN is a string processing function, which returns the length of a string excluding blank space. False c. Built-inf functions make the programmer's work easy. True d. The MID$() function finds the position of the first character counting from the right side. False e. The SQR function processes only the positive numbers. True f. String() converts a numeric expression to its string representation. False g. INKEY$() function stores a single character pressed by the user. True h. TIME$ function returns system time of a computer . True
3. Write the purpose, syntax and example of the following:
a. LEFT$( ) Purpose of LEFT$(): It is used to extract the specific number of characters beginning from the left-most character of the string. Syntax of LEFT$(): LEFT$(string expression,n) where integer number. Example of LEFT$(): LEFT$("NEPAL",1) b. RIGHT$ Purpose of RIGHT$(): It is used to extract the specific number of characters beginning from the right-most character of the string. Syntax of RIGHT$(): RIGHT$(string expression,n) where integer number. Example of RIGHT$(): RIGHT$("NEPAL",1) c. MID$() Purpose of MID$(): It returns a specific number of characters from a string. Syntax of MID$(): LEFT$(string expression,start,length) Example of MID$(): MID$("NEPAL",1,1) d. LTRIM$() Purpose of LTRIM$(): It is used to remove leading blank space from the left side of the string expression. Syntax of LLTRIM$(): LRTRIM$(string expression) Example of LTRIM$(): LTRIM$(" NEPAL ") e. LEN() Purpose of LEN(): It is used to return the number of characters in a string. Syntax of LEN(): LEN(string variable) Example of LEN(): LEN("NEPAL") f. SIN() Purpose of SIN(): To obtain the sine of an angle Syntax of SIN(): SIN(x) where, x ia an angle expreessed in radians. Example of SIN(): SIN(90) g. INPUT$() Purpose of INPUT$():The INPUT$ function halts execution until a specified number of characters are read from the keyboard or a disk file. ENTER does not need to be pressed after the last character is typed. Syntax of INPUT$():Var$ = INPUT$(n) [, #filenumber] Example of INPUT$():Z$ = INPUT$(5) h. DATE$() Purpose of DATE$():The DATE$ function returns the current system date of computer. It returns date in a string format in the form of DD/MM/YYYY. Syntax of DATE$():DATE$ Example of DATE$(): PRINT DATE$ i. TIME$() Purpose of TIME$():The TIME$ function returns the current system time of computer. It returns time in the string format in the form of HH: MM: SS. Syntax of TIME(): TIME$ Example of TIME$(): PRINT TIME$ j. SGN() Purpose of SGN(): It is used to return the sign of a number. Syntax of SGN(): SGN ( x ) Where, x is any numeric expression. Note: If x is positive, SGN ( x ) returns 1. If x is zero, SGN ( x ) returns 0.If x is negative, SGN ( x ) returns -1. Example of SGN(): SGN(2)
4. Differentiate the following:
a. LCASE$() and UCASE$()
LCASE$() UCASE$()
(i) Convert all uppercase characters in lowercase. (i) Convert string to uppercase.
(ii) Syntax:
LCASE$ ( string expression )
(ii) Syntax:
UCASE$ ( string expression )
b. ASC() and CHR$()
ASC() CHR$()
(i)ASC function converts a character or a string variable to its corresponding ASCII code. (i)CHR$ function is used to retrieve the single character represented by the decimal ASCII number.
(ii) Syntax:
ASC (string expression)
(ii) Syntax:
CHR$ ( n ) Where, n is any number or variable within the character code range from 0 to 255
c. TIME$() function and TIME$ statement
TIME$() function TIME$ statement
(i)The TIME$ function returns the current system time of computer. (i)The TIME$ statement allows to set the current system time in computer.
(ii) Syntax:
TIME$
(ii) Syntax:
TIME$ = String Expression
d. INPUT$() function and INPUT statement
INPUT$() INPUT()
(i)The INPUT$ function halts execution until a specified number of characters are read from the keyboard or a disk file. ENTER does not need to be pressed after the last character is typed. (i)INPUT() is used for Execution pauses, a user can enter data from the keyboard and execution continues.
(ii) Syntax:
Var$ = INPUT$(n) [, #filenumber]
(ii) Syntax:
INPUT ["input prompt" ] [; |,] Variable, Variable....
5. Answer the following questions:
a. Define functions. How do you classify functions? Answer: Functions in QBASIC are ready-made programs, which take some data, manipulate them and return a value, which may be a string or a numeric type. QBASIC supports two types of functions: (i) Built in or Library Function (ii) User Defined Function b. Explain function arguments with an example. Answer: While calling a function, we have to pass some data or values required by the function, which are called arguments. The process of passing data to the function is called passing arguments. For example: A = INT(3.14) Here, INT is a function name and 3.14 is an argument, which is processed by the function that returns a numeric value to the variable "A". c. What are user-defined and built-in functions? Answer: The user defined function is written and stored by the programmer to do the specific task. The programmer defines this type of function, if QBASIC does not provide a built-in function to solve any problem required by the programmer. FUNCTION – END FUNCTION statement can be used to define a user-defined function. Built-in or library functions are the functions provided by the QBASIC system. They allow the programmer to use them according to the requirement. Built-in functions require to be called by the programmer to use them in a program. Some common built-in functions are LEN, LEFT$, RIGHT$ etc. These functions are also called library functions. d. Define numeric and string functions with some examples. Numeric or Mathematical Functions Functions, which are used to process numeric data and solve problems, are called mathematical functions. Some mathematical functions of QBASIC are ABS, INT, SQR,COS, SIN etc. String Functions String functions are used to manipulate strings. A string can be defined as a set of alphanumeric characters. String functions are also called string manipulators since they are used to manipulate strings. Some common functions, which deal with strings are LEFT$, RIGHT$, STR$, MID$ etc.
6. Write the output of the following programs:
a. FOR I = 1 TO 5 READ W$ X$ = UCASE$(W$) IF X$ = W$ THEN PRINT W$;" is in uppercase" ELSE PRINT W$;" is in lowercase" END IF NEXT I DATA NEPAL, nepal, KATHMANDU, kathmandu, BHUTAN END Answer: NEPAL is in uppercase nepal is in lowecase KATHMANDU is in uppercase kathmandu is in lowercase BHUTAN is in uppercase b. FOR A = 65 TO 70 PRINT A; "="; CHR$(A) NEXT A END Answer: 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F c. N$ = "BASIC" FOR I=1 TO LEN(N$) C$=MID$(N$, I, 1) PRINT C$;"="; ASC(C$) NEXT I END Answer: B= 66 A=65 S=73 I=73 C=67 d. N = 12345 S$ = STR$(N) FOR I=1 TO LEN(S$) PRINT LEFT$(S$, I) NEXT I END Answer: 1 12 123 1234 12345
7. Debug the following programs:
a. REM To print string in pattern X = NEPAL FOR I=5 TO 1 PRINT LEFT$(X, I ) NEXT I END
Corrected Program is : REM To print string in pattern X$ = "NEPAL" FOR I = 5 TO 1 STEP-1 PRINT LEFT$(X$, I ) NEXT I END
b. REM Program with STR$ function N = "123" S$=STR$(N) PRINT "The number ";N; "is changed to string" END
Corrected Program is : REM Program with STR$ function N = 123 S$=STR$(N) PRINT "The number ";N; "is changed to string" END
c. INPUT "Enter a number"; N$ S = SIGN(N$) SELECT CASE S CASE S PRINT "Number is positive" CASE -1 PRINT "Number is Negative" CASE 0 PRINT "Number is zero" SELECT END END
Corrected Program is : INPUT "Enter a number";N S = SGN(N) SELECT CASE S CASE 1 PRINT "Number is positive" CASE -1 PRINT "Number is Negative" CASE ELSE PRINT "Number is zero" END SELECT END
d. REM To count vowels in a sentence INPUT "Enter String::::"; S L = LEN$(S) FOR K = 1 TO L C$ = MID(S$, 1, K) SELECT CASE C$ CASE A, E, I, O, U COUNT = COUNT + 1 SELECT END NEXT PRINT "Supplied Sentence"; S$ PRINT "Total Vowels:::::"; C END
Corrected Program is : REM To count vowels in a sentence INPUT "Enter String::::";S$ L = LEN(S$) FOR K = 1 TO L C$ = MID$(S$, K, 1) SELECT CASE C$ CASE "A", "E", "I", "O", "U" COUNT = COUNT + 1 END SELECT NEXT K PRINT "Supplied Sentence"; S$ PRINT "Total Vowels:::::";COUNT END
8. Write the programs to print the following patterns:
(a) N NE NEP NEPA NEPAL CLS X$="NEPAL" FOR I = 1 TO LEN(X$) PRINT LEFT$(X$,I) NEXT I END
(b) *NEPAL* *NEPA* *NEP* *NE* *N* CLS X$ = "NEPAL" A$ = "*" FOR I = LEN(X$) TO 1 STEP-1 B$ = LEFT$(X$, I) PRINT A$+B$+A$ NEXT I END
(c) NEPAL EPAL PAL AL LCLS X$ = "NEPAL" A = 1 FOR I = LEN(X$) TO 1 STEP-1 PRINT TAB(A);RIGHT$(X$, I) A = A + 1 NEXT I END
(d) KATHMANDU ATHMAND THMAN HMA M CLS X$ = "KATHMANDU" A = 1 B = 9 FOR I = 1 TO 5 PRINT TAB(A);MID$(X$,A,B) A = A + 1 B = B - 2 NEXT I END
(e) M HMA THMAN ATHMAND KATHMANDU CLS X$ = "KATHMANDU" A = 5 B = 1 FOR I = 1 TO 5 PRINT TAB(A);MID$(X$,A,B) A = A - 1 B = B + 2 NEXT I END
(f) * * * * * * * * * * * * * * * * * * * * * * * * * CLS X$ = " * " FOR I = 1 TO 9 STEP 2 FOR J = 1 TO I PRINT X$; NEXT J PRINT NEXT I END
(g) * NEPAL * * * NEPAL * * * * * NEPAL * * * * * * * NEPAL * * * * * * * * * NEPAL * * * * * CLS A$ = "NEPAL" T = 5 FOR I = 1 TO 5 B$ = STRING$(I, 42) PRINT TAB(T);B$; A$ ;B$ T = T - 1 NEXT I END
(h) 1 121 12321 1234321 123454321 CLS A# = 1 FOR I = 5 TO 1 STEP-1 PRINT TAB(I);A# ^ 2 A# = A# * 10 + 1 NEXT I END
(i) 123454321 1234321 12321 121 1 CLS A# = 11111 FOR I = 1 TO 5 PRINT TAB(I);A#^2 A# = A# \ 10 NEXT I END
9. Write the programs to solve the following problems:
a. Write a program to supply a sentence and count the alphabets "A" and "S" in the sentence.
CLS INPUT " Enter any string : " ; S$ S$ = UCASE$(S$) FOR I = 1 TO LEN(S$) C$ = MID$(S$, I, 1) SELECT CASE C$ CASE "A","S" COUNT = COUNT + 1 END SELECT NEXT I PRINT " Total alphabets A and S : "; COUNT END
b. Write a program to read ten different names and print all names starting with "A" or "B" or "M". (use READ-DATA Statement)
CLS FOR I = 1 TO 10 READ N$ N$ = UCASE$(N$) C$ = LEFT$(N$, 1) SELECT CASE C$ CASE "A","B","M" PRINT N$ END SELECT NEXT I DATA Aryan,Binesh,Nanu, Kritika,ImonS DATA Bibodh, Mahendra, Ashlesa, Gracy,Sampada END
c. Write a program to supply a sentence and count vowels (A, E, I, O, U) separately.
CLS INPUT " Enter any string : " ; S$ S$ = UCASE$(S$) FOR I = 1 TO LEN(S$) C$ = MID$(S$, I, 1) SELECT CASE C$ CASE "A","E","I","O","U" COUNT = COUNT + 1 END SELECT NEXT I PRINT " Total vowels : " ; COUNT END
d. Write a program to supply a string and print whether the string is supplied in uppercase or lowercase.
CLS INPUT " Enter any string :"; W$ X$ = UCASE$(W$) IF X$ = W$ THEN PRINT W$;" is in uppercase" ELSE PRINT W$;" is in lowercase" END IF END
e. Write a program to read a sentence from the keyboard and print it in the title case. (Suppose, Input is, HE IS HARI, Output should be, He Is Hari)
CLS INPUT "Enter any string : ";S$ B$ = UCASE$(MID$(S$, 1, 1)) W$ = W$ + B$ FOR I = 2 TO LEN(S$) IF RIGHT$(W$,1)=" " THEN B$ = UCASE$(MID$(S$, I, 1)) ELSE B$ = LCASE$(MID$(S$, I, 1)) END IF W$ = W$ + B$ NEXT I PRINT "String in title case : ";W$ END
f. Write a program to supply the first, middle and the last name of a person and print the output as given below: If the input is RAM KUMAR SHARMA the output should be R. K. S.
CLS INPUT " Enter first , middle and the last name : " , N$ N$=UCASE$(N$) B$ = LEFT$(N$, 1) FOR J = 1 TO LEN(N$) C$ = MID$(N$, J, 1) IF C$ = " " THEN B$ = B$ + "." + MID$(N$, J + 1, 1) END IF NEXT J PRINT B$ + "." END
g. Write a program to supply a string and print it in the reverse form.
CLS INPUT " Enter string : " ; S$ FOR I = LEN(S$) TO 1 STEP -1 C$ = MID$(S$, I, 1) R$ = R$ + C$ NEXT I PRINT " Supplied string : " ; S$ PRINT " Reversed string :: " ; R$ END
h. Write a program to supply a word and check whether the supplied word is a palindrome or not. (Palindrome is word having the same spelling from both sides. Example: MADAM, NOON are palindromes)
CLS INPUT " Enter string : " ; S$ FOR I = LEN(S$) TO 1 STEP -1 C$ = MID$(S$, I, 1) R$ = R$ + C$ NEXT I IF S$=R$ THEN PRINT S$ ; " is Palindrome" ELSE PRINT S$ ; " is not Palindrome" END IF END
i. Write a program to print the square roots of all numbers from 50 to 100.
CLS FOR I = 50 TO 100 PRINT SQR(I) , NEXT I END
j. Write a program to print all characters represented by ASCII codes from 0 to 255
CLS FOR I = 0 TO 255 PRINT CHR$(I) NEXT I END
k. Write a program to change the system time by "10 : 30 A.M".
CLS INPUT " Enter new time in the format of hh:mm:ss " ; T$ TIME$ = T$ PRINT " New time is " ; TIME$ END In the above program, it displays the current time of computer. If the current time is not exact, then the user can change a new time supplying from the keyboard, which is accepted and changed by the system variable TIME$
l. Write a program to print the system date in the given format. Today‟s date is 05/10/23 (YY/MM/DD).
CLS D$ = DATE$ A$=RIGHT$(D$,2)+"/"+LEFT$(D$,2)+"/"+MID$(D$,4,2) PRINT " Today's date is : " ; A$ END
m. Write a program to input an angle in degree and find its Sin value.
CLS INPUT "Enter an angle in degree"; A R = A * 3.1416 / 180 S = SIN(R) PRINT "Sine of "; A; " degree "; " is "; S END
n. Write a program to supply a word and find the position of character "I" using the INSTR() function.
CLS INPUT " Enter any string : " ; S$ S$ = UCASE$(S$) P = INSTR(1, S$, "I") PRINT " Position of I in given string is " ; P END
o. Write a program to read three different strings and print the longest string (Hint: Use LEN function to find the length of strings)
CLS INPUT " Enter three different strings : " ; P$ , L$ , K$ A = LEN(P$) B = LEN(L$) C = LEN(K$) IF A > B AND A > C THEN G$ = P$ ELSEIF B > A AND B > C THEN G$ = L$ ELSE G$ = K$ END IF PRINT " Longest string is " ; G$ END
p. Write a program to read ten different words using read-data statement. Print the longest and the shortest word.
CLS READ A$ L$ = A$ S$ = A$ FOR I = 2 TO 10 READ N$ IF LEN(N$)>LEN(L$) THEN L$=N$ IF LEN(N$)<LEN(S$) THEN S$=N$ NEXT I DATA Aryan,Binesh,Nanu, Kritika,ImonS DATA Bibodh, Mahendra, Ashlesa, Gracy,Sampada PRINT " Longest String is : " ; L$ PRINT " Shortest String is : " ; S$ END
q. Write a program to read system date of your computer then print the month in the given format. (If system date is "03/23/2012" then print "Month is March").
CLS M$ = LEFT$(DATE$, 2) MONTH = VAL(M$) SELECT CASE MONTH CASE 01 ANS$ = "January" CASE 02 ANS$ = "February" CASE 03 ANS$ = "March" CASE 04 ANS$ = "April" CASE 05 ANS$ = "May" CASE 06 ANS$ = "June" CASE 07 ANS$ = "July" CASE 08 ANS$ = "August" CASE 09 ANS$ = "September" CASE 10 ANS$ = "October" CASE 11 ANS$ = "November" CASE 12 ANS$ = "December" END SELECT PRINT " Month is " ; ANS$ END
r. Write a program to input a string then count vowels and consonents separately.
CLS INPUT " Enter any string : " ; S$ S$ = UCASE$(S$) FOR I = 1 TO LEN(S$) C$ = MID$(S$, I, 1) SELECT CASE C$ CASE "A","E","I","O","U" VCOUNT = VCOUNT + 1 CASE ELSE CCOUNT = CCOUNT + 1 END SELECT NEXT I PRINT " Total Vowels : " ; VCOUNT PRINT " Total consonents : " ; CCOUNT END
s. Write a program to input a string then print it in alternate capital letters.
CLS INPUT " Enter any string : "; S$ FOR I= 1 TO LEN(S$) C$ = MID$(S$ , I , 1) IF I MOD 2 =1 THEN W$ = W$ + UCASE$(C$) ELSE W$ = W$ + LCASE$(C$) END IF NEXT I PRINT " String in alternate Capital Letters : " ; W$ END
t. Write a program to input a string them print vowel characters in uppercase and consonents in lowercase.
CLS INPUT " Enter any string : " ; S$ S$ = UCASE$(S$) FOR I = 1 TO LEN(S$) C$ = MID$(S$, I, 1) SELECT CASE C$ CASE "A","E","I","O","U" R$ = R$ + UCASE$(C$) CASE ELSE R$ = R$ + LCASE$(C$) END SELECT NEXT I PRINT " Converted String : " ; R$ END
End of Chapter-19