Monday, 30 December 2024

grade-10 ch-10 Review of built in functions in qbasic

 Chapter 10: Review of built-in-functions in QBASIC

1. Fill in the blanks:

a. data    b. user-defined    c. built-in or library     d. string    e. ANSCII    f. system 

2. State whether each of the following statements is Ture or False.

a. true    b. true    c. true    d. false    e. true

3. Write the purpose and syntax of the following:

a. LEN()

  • Purpose: Returns the length of a string (i.e., the number of characters in the string).
  • Syntax:
    LEN(string)
    
  • Example:
    PRINT LEN("Hello")  ' Outputs: 5
    

b. MID$()

  • Purpose: Extracts a substring from a string, starting at a specified position and with a specified length.
  • Syntax:
    MID$(string, start, length)
    
  • Example:
    PRINT MID$("Hello", 2, 3)  ' Outputs: "ell"
    

c. LEFT$()

  • Purpose: Returns the leftmost characters from a string up to the specified number of characters.
  • Syntax:
    LEFT$(string, length)
    
  • Example:
    PRINT LEFT$("Hello", 3)  ' Outputs: "Hel"
    

d. VAL()

  • Purpose: Converts a string representing a number into its numeric value.
  • Syntax:
    VAL(string)
    
  • Example:
    PRINT VAL("123.45")  ' Outputs: 123.45
    

e. INT()

  • Purpose: Returns the integer portion of a number by rounding down the value.
  • Syntax:
    INT(number)
    
  • Example:
    PRINT INT(9.75)  ' Outputs: 9
    

f. ABS()

  • Purpose: Returns the absolute value of a number (removes the sign).
  • Syntax:
    ABS(number)
    
  • Example:
    PRINT ABS(-10)  ' Outputs: 10
    

g. STR$()

  • Purpose: Converts a number to a string.
  • Syntax:
    STR$(number)
    
  • Example:
    PRINT STR$(123)  ' Outputs: "123"
    

h. SQR()

  • Purpose: Returns the square root of a number.
  • Syntax:
    SQR(number)
    
  • Example:
    PRINT SQR(16)  ' Outputs: 4
    

i. UCASE$()

  • Purpose: Converts a string to uppercase.
  • Syntax:
    UCASE$(string)
    
  • Example:
    PRINT UCASE$("hello")  ' Outputs: "HELLO"
    

j. INSTR()

  • Purpose: Returns the position of the first occurrence of a substring within a string. Returns 0 if not found.
  • Syntax:
    INSTR(start_position, string1, string2)
    
  • Example:
    PRINT INSTR("Hello, World!", "World")  ' Outputs: 8
    

k. STRING$()

  • Purpose: Returns a string consisting of a repeated character.
  • Syntax:
    STRING$(length, character)
    
  • Example:
    PRINT STRING$(5, "*")  ' Outputs: "*****"
    

l. TAN()

  • Purpose: Returns the tangent of an angle (in radians).
  • Syntax:
    TAN(angle)
    
  • Example:
    PRINT TAN(1)  ' Outputs: 1.557407
    

4. Differentiate between:

Here is the comparison of the specified QBASIC functions:

a. STR$() vs. VAL()

Function Purpose Syntax Example Output
STR$() Converts a numeric value into a string. STR$(number) STR$(123) "123"
VAL() Converts a string representing a number into its numeric value. VAL(string) VAL("123.45") 123.45

Explanation:

  • STR$(): Converts numbers to string representation, including the decimal point for floats.
  • VAL(): Converts a numeric string into an actual numeric value, removing any non-numeric characters after the number.

b. ASC() vs. CHR$()

Function Purpose Syntax Example Output
ASC() Returns the ASCII code of a character. ASC(character) ASC("A") 65
CHR$() Returns the character corresponding to an ASCII value. CHR$(ASCII_value) CHR$(65) "A"

Explanation:

  • ASC(): Converts a character to its corresponding ASCII numeric value.
  • CHR$(): Converts an ASCII numeric value into its corresponding character.

c. RIGHT$() vs. LEFT$()

Function Purpose Syntax Example Output
RIGHT$() Extracts the rightmost characters from a string. RIGHT$(string, length) RIGHT$("Hello", 3) "llo"
LEFT$() Extracts the leftmost characters from a string. LEFT$(string, length) LEFT$("Hello", 3) "Hel"

Explanation:

  • RIGHT$(): Extracts a specified number of characters starting from the right end of the string.
  • LEFT$(): Extracts a specified number of characters starting from the left end of the string.

Summary:

  • STR$() converts numbers to strings, while VAL() converts numeric strings to actual numbers.
  • ASC() gives the ASCII value of a character, and CHR$() gives the character from an ASCII value.
  • RIGHT$() extracts characters from the right end of the string, while LEFT$() extracts characters from the left end.

5. Answer the following questions.

a. What do you mean by function? Explain with examples.

A function in QBASIC is a pre-defined operation that takes one or more inputs, performs an operation, and returns a result. Functions make it easier to perform common tasks without having to write the same code repeatedly.

Example:

PRINT SQR(16)  ' Calls the built-in SQR function to find the square root of 16
  • The function SQR() calculates the square root of 16 and returns 4.

b. Differentiate between library and user-defined functions.

Library Function User-Defined Function
These are pre-built functions provided by QBASIC, such as SQR(), LEN(), MID$(), etc. These are functions that the programmer defines to perform specific tasks.
They are ready to use and do not require writing additional code. The programmer defines the logic inside the function.
Examples: SQR(), ABS(), VAL() Example: FUNCTION CalculateArea(radius)

Example of a User-Defined Function:

FUNCTION AddNumbers(a, b)
    AddNumbers = a + b
END FUNCTION

c. What is string concatenation? Give an example.

String concatenation is the process of joining two or more strings together to form a new string. In QBASIC, the + operator is used to concatenate strings.

Example:

PRINT "Hello " + "World!"  ' Outputs: "Hello World!"
  • In this example, "Hello " and "World!" are concatenated into "Hello World!".

d. Differentiate between numeric and string functions.

Numeric Functions String Functions
These functions perform operations on numbers, such as rounding, square roots, and absolute values. These functions operate on strings, such as finding the length, extracting substrings, and converting case.
Examples: SQR(), ABS(), INT(), VAL() Examples: LEN(), MID$(), LEFT$(), UCASE$()

e. "Functions make our work easier". Explain.

Functions simplify programming by reducing redundancy, improving code readability, and encapsulating complex operations into a simple call. Rather than writing the same code multiple times, functions allow developers to reuse code, making programs more efficient and easier to maintain.

Example:

Instead of calculating the square root manually every time, we can use the SQR() function:

PRINT SQR(25)  ' Outputs: 5

This avoids rewriting the logic for square root calculation every time it’s needed.

f. What are string functions? Give some examples.

String functions are built-in functions in QBASIC that allow manipulation or extraction of information from string variables.

Examples:

  • LEN(): Returns the length of a string.
    PRINT LEN("Hello")  ' Outputs: 5
    
  • MID$(): Extracts a substring from a string.
    PRINT MID$("Hello", 2, 3)  ' Outputs: "ell"
    
  • UCASE$(): Converts a string to uppercase.
    PRINT UCASE$("hello")  ' Outputs: "HELLO"
    

g. What are numeric functions? Give some examples.

Numeric functions are built-in functions in QBASIC that perform mathematical operations or return numeric values.

Examples:

  • SQR(): Returns the square root of a number.
    PRINT SQR(9)  ' Outputs: 3
    
  • ABS(): Returns the absolute value of a number.
    PRINT ABS(-10)  ' Outputs: 10
    
  • INT(): Returns the integer part of a number.
    PRINT INT(8.7)  ' Outputs: 8
    

Summary:

  • Function: A function is a pre-built or user-defined operation that performs a specific task and returns a result. For example, SQR() calculates the square root.
  • Library vs. User-Defined Functions: Library functions are built-in (e.g., SQR()), while user-defined functions are created by the programmer (e.g., FUNCTION AddNumbers()).
  • String Concatenation: The process of joining two or more strings together (e.g., "Hello " + "World!").
  • Numeric vs. String Functions: Numeric functions work with numbers (e.g., SQR()), while string functions work with text (e.g., LEN()).
  • Functions: They simplify code by reusing common operations, reducing errors and improving maintainability.
  • String Functions: Operations on text strings, such as LEN() and MID$().
  • Numeric Functions: Operations on numbers, such as SQR() and ABS().

6. Write the output of the following programs:


Here’s the dry run for each of the given programs in tabular form:


a. Program:

N$ = "KATHMANDU"
FOR K = 1 TO 5
    LEFT$(N$, K)
NEXT K
END

Explanation: The program iterates over values of K from 1 to 5, calling the LEFT$() function, but it doesn't display any result, as there’s no PRINT statement.

Iteration (K)

LEFT$(N$, K)

Result

K = 1

LEFT$("KATHMANDU", 1)

"K"

K = 2

LEFT$("KATHMANDU", 2)

"KA"

K = 3

LEFT$("KATHMANDU", 3)

"KAT"

K = 4

LEFT$("KATHMANDU", 4)

"KATH"

K = 5

LEFT$("KATHMANDU", 5)

"KATHM"

  • The program doesn’t print any result, but it processes substrings for K = 1 to K = 5.

b. Program:

FOR A = 97 TO 123
    PRINT A; "="; CHR$(A)
NEXT A
END

Explanation: This program prints the ASCII values (from 97 to 123) and their corresponding characters using CHR$().




























Iteration (A)

CHR$(A)

Output

A = 97

CHR$(97)

"97 = a"

A = 98

CHR$(98)

"98 = b"

A = 99

CHR$(99)

"99 = c"

A = 100

CHR$(100)

"100 = d"

A = 101

CHR$(101)

"101 = e"

A = 102

CHR$(102)

"102 = f"

A = 103

CHR$(103)

"103 = g"

A = 104

CHR$(104)

"104 = h"

A = 105

CHR$(105)

"105 = i"

A = 106

CHR$(106)

"106 = j"

A = 107

CHR$(107)

"107 = k"

A = 108

CHR$(108)

"108 = l"

A = 109

CHR$(109)

"109 = m"

A = 110

CHR$(110)

"110 = n"

A = 111

CHR$(111)

"111 = o"

A = 112

CHR$(112)

"112 = p"

A = 113

CHR$(113)

"113 = q"

A = 114

CHR$(114)

"114 = r"

A = 115

CHR$(115)

"115 = s"

A = 116

CHR$(116)

"116 = t"

A = 117

CHR$(117)

"117 = u"

A = 118

CHR$(118)

"118 = v"

A = 119

CHR$(119)

"119 = w"

A = 120

CHR$(120)

"120 = x"

A = 121

CHR$(121)

"121 = y"

A = 122

CHR$(122)

"122 = z"


  • This prints the ASCII values (97 to 122) along with their respective characters (lowercase letters a to z).

c. Program:

N$ = "HACKER"
FOR X = 1 TO LEN(N$)
    C$ = MID$(N$, X, 1)
    S = S + ASC(C$)
NEXT X
PRINT S
END

Explanation: The program calculates the sum of the ASCII values of each character in the string "HACKER".

Iteration (X)

MID$(N$, X, 1)

ASC(C$)

S (Running Total)

X = 1

MID$("HACKER", 1, 1)

ASC("H") = 72

S = 72

X = 2

MID$("HACKER", 2, 1)

ASC("A") = 65

S = 72 + 65 = 137

X = 3

MID$("HACKER", 3, 1)

ASC("C") = 67

S = 137 + 67 = 204

X = 4

MID$("HACKER", 4, 1)

ASC("K") = 75

S = 204 + 75 = 279

X = 5

MID$("HACKER", 5, 1)

ASC("E") = 69

S = 279 + 69 = 348

X = 6

MID$("HACKER", 6, 1)

ASC("R") = 82

S = 348 + 82 = 430

  • The program prints 430 after summing the ASCII values of "HACKER".

d. Program:

REM Program with functions
S$ = "NEPAL"
L = LEN(S$)
FOR K = 1 TO L
    C$ = MID$(S$, K, 1)
    R$ = C$ + R$
NEXT K
PRINT R$
END

Explanation: This program reverses the string "NEPAL" using a loop and the MID$() function.

Iteration (K) MID$(S$, K, 1) R$ (Running Reverse String)
K = 1 MID$("NEPAL", 1, 1) R$ = "N"
K = 2 MID$("NEPAL", 2, 1) R$ = "EN"
K = 3 MID$("NEPAL", 3, 1) R$ = "PNE"
K = 4 MID$("NEPAL", 4, 1) R$ = "APNE"
K = 5 MID$("NEPAL", 5, 1) R$ = "LAPNE"
  • After completing the loop, the program prints LAPNE, which is the reverse of "NEPAL".

Summary of Outputs:

  • a. No visible output, but it processes substrings of "KATHMANDU".
  • b. Prints the ASCII values from 97 to 122 and their corresponding characters (a to z).
  • c. Prints 430, the sum of ASCII values of the characters in "HACKER".
  • d. Prints "LAPNE", the reverse of the string "NEPAL".

7. Debug the following programs and write in the correct forms:


Here’s a correction for the bugs in each of the provided programs:


a. Program: To print string in a pattern

Original Code:

REM to print string in pattern
X = "NEPAL"
FOR K = 5 TO 1
    PRINT RIGHT$(X$, I)
NEXT I
END

Issues:

  1. The loop variable K is used in the FOR loop, but the loop uses I in the RIGHT$() function, which is incorrect.
  2. The program does not properly decrease the number of characters to print.

Corrected Code:

REM to print string in pattern
X$ = "NEPAL"
FOR I = 5 TO 1 STEP -1
    PRINT RIGHT$(X$, I)
NEXT I
END

Explanation:

  • Use the loop variable I to control how many characters to print.
  • The loop starts from 5 and steps down to 1.
  • RIGHT$(X$, I) prints the rightmost I characters of the string X$.

b. Program: To print string in reverse form

Original Code:

REM to print string in reverse form
INPUT "Enter a string"; N$
L = LEN$(N$)
FOR K = L TO 1
    C$ = MID(N$, K, 1)
    W$ = C$ + W$
NEXT K
PRINT "Reversed string is"; W$
END

Issues:

  1. The MID() function is used incorrectly; it should extract a substring, but the variable K needs to control the position.
  2. There’s no space between the string prompt and the input value in the INPUT statement.

Corrected Code:

REM to print string in reverse form
INPUT "Enter a string: "; N$
L = LEN(N$)
W$ = ""  REM Initialize the result string
FOR K = L TO 1 STEP -1
    C$ = MID$(N$, K, 1)
    W$ = W$ + C$
NEXT K
PRINT "Reversed string is: "; W$
END

Explanation:

  • The program now correctly reverses the input string N$ using MID$() by reading characters from the end to the beginning.
  • We initialize W$ as an empty string to accumulate the reversed characters.

c. Program: To count vowels in a sentence

Original Code:

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 + 1 = COUNT
    SELECT END
NEXT
PRINT "Supplied Sentence"; S$
PRINT "Total Vowels::::"; C
END

Issues:

  1. The MID() function is used incorrectly, it should extract a single character at each position in the string.
  2. The syntax for counting vowels and updating the counter is incorrect.
  3. The SELECT CASE block should use string literals (i.e., "A", "E", etc.).
  4. The COUNT + 1 = COUNT syntax is incorrect.

Corrected Code:

REM to count vowels in a sentence
INPUT "Enter String: "; S$
L = LEN(S$)
COUNT = 0  REM Initialize the vowel counter
FOR K = 1 TO L
    C$ = MID$(S$, K, 1)  REM Extract one character at a time
    SELECT CASE UCASE$(C$)  REM Convert to uppercase for case-insensitivity
    CASE "A", "E", "I", "O", "U"
        COUNT = COUNT + 1
    END SELECT
NEXT K
PRINT "Supplied Sentence: "; S$
PRINT "Total Vowels: "; COUNT
END

Explanation:

  • MID$(S$, K, 1) extracts one character at a time from the string S$.
  • We use UCASE$(C$) to make the comparison case-insensitive.
  • The counter COUNT is correctly incremented when a vowel is found.

Summary of Corrected Programs:

  1. Program a (Pattern printing):

    • Corrects the loop variable mismatch and ensures proper printing of the substring from RIGHT$().
  2. Program b (Reversing a string):

    • Fixes the reverse string logic and ensures proper string reversal with a loop and MID$().
  3. Program c (Counting vowels):

    • Fixes the logic of counting vowels, uses correct substring extraction (MID$()) and proper case-insensitive checks using UCASE$().

Let me know if you need further explanations or assistance!

8. Write the programs to print the following patterns:

Here are the QBasic programs for each pattern you requested:

a. Pattern:

L
AL
PAL
EPAL
NEPAL

QBasic Code:

REM Program to print the first pattern
X$ = "NEPAL"
FOR I = 1 TO LEN(X$)
    PRINT MID$(X$, 1, I)
NEXT I
END

Explanation:

  • The loop prints substrings of increasing length, starting from the first character up to the Ith character.

b. Pattern:

*NEPAL*
*NEPA*
*NEP*
*NE*
*N*

QBasic Code:

REM Program to print the second pattern
X$ = "NEPAL"
FOR I = LEN(X$) TO 1 STEP -1
    PRINT "*" + MID$(X$, 1, I) + "*"
NEXT I
END

Explanation:

  • The loop starts from the full string "NEPAL" and progressively reduces the length by 1 in each iteration.
  • Each line is enclosed in asterisks.

d. Pattern:

KATHMANDU
 ATHMAND
  THMAN
   HMA
    M

QBasic Code:

REM Program to print the fourth pattern
P=1
X$ = "KATHMANDU"
L=LEN(X$)
Space=1
FOR I = 1 TO 5
    PRINT SPACE$(Space);MID$(X$, P, L)
P=P+1
L=L-1
Space=Space-1
NEXT I
END

Explanation:

  • The loop uses SPACE$(Space) to add leading spaces in each line, and the MID$() function prints progressively shorter substrings.

e. Pattern:

     M
    HMA
   THMAN
  ATHMAND
 KATHMANDU

QBasic Code:

REM Program to print the fifth pattern
P=5
L=1
Space=50
X$ = "KATHMANDU"
FOR I = 5 TO 1 STEP -1
    PRINT SPACE$(Space) + MID$(X$, P, L)
    Space=Space-1
    L=L+2
    P=P-1
NEXT I
END

Explanation:

  • The loop reduces the number of characters to print in each iteration.
  • The number of leading spaces (SPACE$(Space) decreases as the string shortens.

Summary of Programs:

  • Each program uses loops and string manipulation functions like MID$() and SPACE$() to print the desired patterns.
  • The programs leverage the FOR loop to print progressively smaller or larger substrings with appropriate spacing.

Let me know if you need further assistance!

8. Write program codes for the following:


a. To read three different strings from the keyboard and print the longest string.

QBasic Code:

REM To find the longest string
INPUT "Enter the first string: ", A$
INPUT "Enter the second string: ", B$
INPUT "Enter the third string: ", C$

IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
    PRINT "The longest string is: "; A$
ELSEIF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
    PRINT "The longest string is: "; B$
ELSE
    PRINT "The longest string is: "; C$
END IF
END

b. To supply a word and then to count the total number of vowels.

QBasic Code:

REM To count the vowels in a word
INPUT "Enter a word: ", Word$
VowelCount = 0
FOR I = 1 TO LEN(Word$)
    C$ = MID$(Word$, I, 1)
    IF UCASE$(C$) = "A" OR UCASE$(C$) = "E" OR UCASE$(C$) = "I" OR UCASE$(C$) = "O" OR UCASE$(C$) = "U" THEN
        VowelCount = VowelCount + 1
    END IF
NEXT I
PRINT "Total vowels: "; VowelCount
END

c. To input three different names from the keyboard and print the shortest name.

QBasic Code:

REM To find the shortest name
INPUT "Enter the first name: ", A$
INPUT "Enter the second name: ", B$
INPUT "Enter the third name: ", C$

IF LEN(A$) < LEN(B$) AND LEN(A$) < LEN(C$) THEN
    PRINT "The shortest name is: "; A$
ELSEIF LEN(B$) < LEN(A$) AND LEN(B$) < LEN(C$) THEN
    PRINT "The shortest name is: "; B$
ELSE
    PRINT "The shortest name is: "; C$
END IF
END

h. To supply a string and print it in reverse form.

QBasic Code:

REM To print a string in reverse
INPUT "Enter a string: ", Word$
Reversed$ = ""
FOR I = LEN(Word$) TO 1 STEP -1
    Reversed$ = Reversed$ + MID$(Word$, I, 1)
NEXT I
PRINT "Reversed string is: "; Reversed$
END

j. To print the input word in alternate capitals.

QBasic Code:

REM To print a word in alternate capitals
INPUT "Enter a word: ", Word$
Result$ = ""
FOR I = 1 TO LEN(Word$)
    C$ = MID$(Word$, I, 1)
    IF I MOD 2 = 1 THEN
        Result$ = Result$ + UCASE$(C$)
    ELSE
        Result$ = Result$ + LCASE$(C$)
    END IF
NEXT I
PRINT "Alternate capitals word is: "; Result$
END

k. To print the sum of ASCII codes of each character of input string.

QBasic Code:

REM To find the sum of ASCII codes
INPUT "Enter a string: ", Word$
Sum = 0
FOR I = 1 TO LEN(Word$)
    C$ = MID$(Word$, I, 1)
    Sum = Sum + ASC(C$)
NEXT I
PRINT "Sum of ASCII codes: "; Sum
END

l. To supply a sentence and count the number of vowels in the sentence.

QBasic Code:

REM To count vowels in a sentence
INPUT "Enter a sentence: ", Sentence$
VowelCount = 0
FOR I = 1 TO LEN(Sentence$)
    C$ = MID$(Sentence$, I, 1)
    IF UCASE$(C$) = "A" OR UCASE$(C$) = "E" OR UCASE$(C$) = "I" OR UCASE$(C$) = "O" OR UCASE$(C$) = "U" THEN
        VowelCount = VowelCount + 1
    END IF
NEXT I
PRINT "Total vowels in the sentence: "; VowelCount
END

m. To supply a word then count the total number of consonants.

QBasic Code:

REM To count consonants in a word
INPUT "Enter a word: ", Word$
ConsonantCount = 0
FOR I = 1 TO LEN(Word$)
    C$ = UCASE$(MID$(Word$, I, 1))

    SELECT CASE C$
            CASE <>"A","E","I","O","U"
        ConsonantCount = ConsonantCount + 1
    END SELECT
NEXT I
PRINT "Total consonants: "; ConsonantCount
END

n. To input a string then print "*" in place of vowel characters.

QBasic Code:

REM To replace vowels with "*"
INPUT "Enter a string: ", Word$
NewWord$ = ""
FOR I = 1 TO LEN(Word$)
    C$ = MID$(Word$, I, 1)
    IF UCASE$(C$) = "A" OR UCASE$(C$) = "E" OR UCASE$(C$) = "I" OR UCASE$(C$) = "O" OR UCASE$(C$) = "U" THEN
        NewWord$ = NewWord$ + "*"
    ELSE
        NewWord$ = NewWord$ + C$
    END IF
NEXT I
PRINT "Modified string: "; NewWord$
END

Explanation of Each Program:

  1. a. Compares the lengths of three strings using the LEN() function to determine the longest string.
  2. b. Loops through each character in the word and checks if it's a vowel (A, E, I, O, U).
  3. c. Similar to a, but finds the shortest name based on string length.
  4. h. Reverses the string by looping from the last character to the first and building the reverse string.
  5. j. Alternates between uppercase and lowercase for each character in the string.
  6. k. Uses the ASC() function to find the ASCII value of each character and sums them.
  7. l. Similar to b, but works for sentences and counts the vowels in the sentence.
  8. m. Loops through each character, checks if it's a consonant (i.e., a letter that's not a vowel), and counts it.
  9. n. Replaces vowels with * in the string.

These programs demonstrate various string manipulations, counting, and conditional logic in QBasic. Let me know if you need further clarification or modifications!


No comments:

Post a Comment

Popular Posts