Wednesday, October 16, 2024

ASSIGNMENT-5

Q1: Explain types of arrays with an example.

Arrays are used to store multiple values of the same type in a single variable. We will discuss three types of arrays: one-dimensional, two-dimensional, and multidimensional arrays.

Accessing Elements in an Array

In programming, an array is a collection of elements that are stored in contiguous memory locations. The elements in an array can be accessed using their index, which represents their position within the array. The indexing of an array typically starts from 0, meaning the first element is at index 0, the second at index 1, and so on.

Accessing Array Elements

To access an element in an array, you specify the array name followed by the index of the element in square brackets. For example, if you have an array named arr, accessing the element at index i is done using arr[i].

Here's a simple example in C:

#include <iostream>
using namespace std;

int main() {
int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;

printf("arr[0]: %4d ", arr[0] );
printf("arr[1]: %4d ", arr[1] );
printf("arr[2]: %4d ", arr[2] );

return 0;
}

In this example, we have an array arr with three elements. We access each element using its index and print its value.

Traversing an Array

To access all elements in an array, you can traverse the array using a loop. This process involves iterating over the array indices and accessing each element sequentially.

Here's an example of traversing an array in C:

#include <stdio.h>

int main() {
int table_of_two[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
for (int i = 0; i < 10; i++) {
printf("%4d", table_of_two[i] );
}
return 0;
}

In this code, we have an array table_of_two containing the first ten multiples of two. We use a for loop to traverse the array and print each element.

Determining Array Size

The size of an array, which is the total number of elements it can hold, is often determined at the time of declaration. However, you can also calculate the size of an array dynamically using the sizeof operator. The size of the array is obtained by dividing the total size of the array by the size of an individual element.

Here's how you can find the size of an array in C:

#include <stdio.h>
using namespace std;

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Length of the array: %d", n);
return 0;
}

In this example, sizeof(arr) gives the total size in bytes of the array arr, and sizeof(arr[0]) gives the size of one element. Dividing these two gives us the number of elements in the array.

1. One-dimensional Array:

A one-dimensional array is a linear array where elements are stored sequentially.

Example:


#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5}; // One-dimensional array initialization
printf("One-dimensional Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); // Accessing array elements
}
return 0;
}

Explanation:

  • The array arr contains 5 integers.
  • We access each element of the array using a for loop, printing each one sequentially.

2. Two-dimensional Array:

A two-dimensional array can be visualized as a table of rows and columns.

Example:


#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // 2D array initialization
printf("Two-dimensional Array:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]); // Accessing 2D array elements
}
printf("\n");
}
return 0;
}

Explanation:

  • arr is a 2D array of size 3x3. It is initialized with values in a matrix-like form.
  • Nested for loops are used to access and print the array elements row by row.

3. Multidimensional Array:

Higher-dimensional arrays are possible, such as 3D arrays, where elements are accessed via multiple indices.

Example:


#include <stdio.h>

int main() {
int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; // 3D array initialization
printf("Multidimensional Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]); // Accessing 3D array elements
}
printf("\n");
}
printf("\n");
}
return 0;
}

Explanation:

  • arr is a 3D array of size 2x2x2, representing layers of 2D arrays.
  • Three nested loops are used to access and print each element in the 3D array.

Q2: Explain character array with an example.

A character array is a series of characters stored in contiguous memory locations, typically used to represent strings.

A character array in C is essentially a sequence of characters stored in contiguous memory locations. You can think of it as a string or a line of text.

Here's a simple example:

#include <stdio.h>

int main() {

    char greeting[] = "Hello, World!";

        printf("%s\n", greeting);

    return 0;

}

In this example, the character array greeting holds the string "Hello, World!". When you use printf with %s, it prints the entire character array as a string.


Now, let's look at another example where you can modify characters in the array:

#include <stdio.h>

int main() {

    char name[] = "John Doe";

    name[5] = 'X'; // Change 'D' to 'X'

    printf("%s\n", name); // Output will be "John Xoe"

    return 0;

}

In this case, we change the character at index 5 from 'D' to 'X'. Arrays are handy for manipulating strings in such a way.

Character arrays can also be used to take input from the user:

#include <stdio.h>

int main() {

    char name[50];

    printf("Enter your name: ");

    fgets(name, sizeof(name), stdin); // safer than gets(), avoids buffer overflow

    printf("Hello, %s", name);

    return 0;

}

Here, fgets reads a line of text from standard input and stores it in the name array. This makes it safer than using gets, which can lead to buffer overflow.

Example:


#include <stdio.h>

int main() {
char name[6] = "Hello"; // Character array (string) initialization
printf("Character Array: ");
for (int i = 0; i < 5; i++) {
printf("%c", name[i]); // Accessing and printing each character
}
return 0;
}

Explanation:

  • name is a character array with 6 elements (including the null terminator \0).
  • The loop prints each character from the array individually.

Q3: Explain the following string functions with examples.

strcmp()
Compares two strings.

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";

    int result = strcmp(str1, str2);
    printf("strcmp result: %d\n", result); // returns negative because "Hello" < "World"

    return 0;
}
strlen()
Returns the length of a string.

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    printf("Length of the string: %lu\n", strlen(str)); // prints the length of str

    return 0;
}
strcat()
Concatenates (joins) two strings.

#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[] = " World";

    strcat(str1, str2);
    printf("%s\n", str1); // prints "Hello World"

    return 0;
}
strcpy()
Copies one string into another.

#include <stdio.h>
#include <string.h>

int main() {
    char dest[20];
    char src[] = "Hello, World!";

    strcpy(dest, src);
    printf("%s\n", dest); // prints "Hello, World!"

    return 0;
}
strstr()
Finds the first occurrence of a substring in a string.
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char *sub = strstr(str, "World");

    if (sub) {
        printf("Found: %s\n", sub); // prints "World!"
    }

    return 0;
}
strupr()
Converts a string to uppercase. Note: This function is not standard in C, but available in some compilers like Turbo C.

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello";

    for(int i = 0; str[i]; i++){
      str[i] = toupper(str[i]);
    }

    printf("%s\n", str); // prints "HELLO"

    return 0;
}
strchr()
Finds the first occurrence of a character in a string.
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char *pos = strchr(str, 'W');

    if (pos) {
        printf("Found: %s\n", pos); // prints "World!"
    }

    return 0;
}
strncmp()
Compares the first n characters of two strings.
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello, World";

    int result = strncmp(str1, str2, 5);
    printf("strncmp result: %d\n", result); // returns 0 because first 5 chars are equal

    return 0;
}
strrev()
Reverses a string. Note: This function is not standard in C, but available in some compilers like Turbo C.
#include <stdio.h>
#include <string.h>

void reverse(char *str) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
}

int main() {
    char str[] = "Hello";
    reverse(str);
    printf("%s\n", str); // prints "olleH"

    return 0;
}
strlwr()
Converts a string to lowercase. Note: This function is not standard in C, but available in some compilers like Turbo C.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void strlwr(char *str) {
    for (int i = 0; str[i]; i++) {
        str[i] = tolower(str[i]);
    }
}

int main() {
    char str[] = "Hello, World!";
    strlwr(str);
    printf("%s\n", str); // prints "hello, world!"

    return 0;
}
strncat()
Concatenates the first n characters of one string to another.

#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[] = " World";

    strncat(str1, str2, 3);
    printf("%s\n", str1); // prints "Hello Wo"

    return 0;
}
strncpy()
Copies the first n characters of one string into another.

#include <stdio.h>
#include <string.h>

int main() {
    char dest[20];
    char src[] = "Hello, World!";

    strncpy(dest, src, 5);
    dest[5] = '\0'; // null-terminate the string
    printf("%s\n", dest); // prints "Hello"

    return 0;
}
strcat(s1, s2)
Concatenates two strings, s1 and s2.

#include <stdio.h>
#include <string.h>

int main() {
    char s1[20] = "Good";
    char s2[] = " Day";

    strcat(s1, s2);
    printf("%s\n", s1); // prints "Good Day"

    return 0;
}
puts()
Writes a string to stdout.
#include <stdio.h>

int main() {
    char str[] = "Hello, World!";
    puts(str); // prints "Hello, World!" followed by a newline

    return 0;
}
strcpy(s1, s2)
Copies string s2 into s1.

#include <stdio.h>
#include <string.h>

int main() {
    char s1[20];
    char s2[] = "Copy this!";

    strcpy(s1, s2);
    printf("%s\n", s1); // prints "Copy this!"

    return 0

Below is a full program that demonstrates various string functions:

#include <stdio.h>
#include <string.h>
int main() {
char s1[20] = "Hello";
char s2[20] = "World";
char s3[20];
// strcmp(): Compare two strings
int result = strcmp(s1, s2);
printf("strcmp(s1, s2): %d\n", result);
// strlen(): Get the length of the string
printf("strlen(s1): %lu\n", strlen(s1));
// strcat(): Concatenate s2 to s1
strcat(s1, s2);
printf("strcat(s1, s2): %s\n", s1);
// strcpy(): Copy s2 into s3
strcpy(s3, s2);
printf("strcpy(s3, s2): %s\n", s3);
// strstr(): Find "World" in s1
char *sub = strstr(s1, "World");
if (sub) {
printf("strstr(s1, \"World\"): %s\n", sub);
}
// strupr(): Convert string to uppercase
printf("Before strupr: %s\n", s1);
strupr(s1);
printf("After strupr: %s\n", s1);
// strchr(): Find first occurrence of 'o'
char *pos = strchr(s1, 'O');
if (pos) {
printf("strchr(s1, 'O'): %s\n", pos);
}
// strrev(): Reverse the string
strrev(s1);
printf("strrev(s1): %s\n", s1);
return 0;
}

Explanation:

  • The program demonstrates various string functions from the string.h library.
  • Functions like strcmp, strlen, strcat, strcpy, strstr, and strchr are commonly used for string operations.

Q4: What is UDF? Explain types of UDF.

A User-Defined Function (UDF) is a block of code that performs a specific task, defined by the programmer. UDFs help break down complex problems into smaller, manageable parts and allow code reuse, making programs more modular and easier to understand.

Types of UDFs in C

Functions with No Arguments and No Return Value

These functions do not take any input arguments and do not return a value.

Used for tasks like printing a message.

Example:

#include <stdio.h>

void greet() {

    printf("Hello, World!\n");

}

int main() {

    greet();

    return 0;

}

Functions with No Arguments and a Return Value

These functions do not take any input arguments but return a value.

Used for calculations or obtaining system information.

Example:

#include <stdio.h>

int getFive() {

    return 5;

}

int main() {

    int number = getFive();

    printf("Number: %d\n", number);

    return 0;

}

Functions with Arguments and No Return Value

These functions take input arguments but do not return a value.

Used for tasks that need input but do not produce a result to return.

Example:

#include <stdio.h>

void printMessage(char message[]) {

    printf("%s\n", message);

}

int main() {

    printMessage("Hello, C Programming!");

    return 0;

}

Functions with Arguments and a Return Value

These functions take input arguments and return a value.

Most versatile and commonly used type of UDF.

Example:

#include <stdio.h>

int add(int a, int b) {

    return a + b;

}

int main() {

    int sum = add(5, 3);

    printf("Sum: %d\n", sum);

    return 0;

}

Example Explained: Prime Number Check

Here’s a more detailed example using a function to check if a number is prime.

#include <stdio.h>

#include <stdbool.h>

// Function to check if a number is prime

bool isPrime(int n) {

    if (n <= 1) return false;

    for (int i = 2; i <= n / 2; i++) {

        if (n % i == 0) return false;

    }

    return true;

}

int main() {

    int number;

    printf("Enter a positive integer: ");

    scanf("%d", &number);

    if (isPrime(number)) {

        printf("%d is a prime number.\n", number);

    } else {

        printf("%d is not a prime number.\n", number);

    }

    return 0;

}

Detailed Breakdown

Function Prototype: The declaration of the function, specifying its return type and parameters.

bool isPrime(int n);

Function Definition: Contains the actual code for the function.

bool isPrime(int n) {

    if (n <= 1) return false;

    for (int i = 2; i <= n / 2; i++) {

        if (n % i == 0) return false;

    }

    return true;

}

Function Call: Where the function is invoked in the main function.

if (isPrime(number)) {

    printf("%d is a prime number.\n", number);

} else {

    printf("%d is not a prime number.\n", number);

}

This function checks if a number is prime by iterating through all numbers from 2 to half of the input number. If the input number is divisible by any of these, it's not prime.


Q5: Declaring and using functions, and explaining components of a function.

Declaring and using functions in C can make your programs more modular and easier to manage. Here’s a comprehensive overview:

Components of a Function

Function Declaration (Prototype): This tells the compiler about the function name, return type, and parameters without providing the actual body.

Function Definition: This contains the actual body of the function where the logic is implemented.

Function Call: This is where the function is invoked in the program.

Declaring a Function

To declare a function, you specify the return type, the function name, and parameters (if any).

#include <stdio.h>

// Function Declaration

int add(int, int);

int main() {

    int a = 5, b = 10;

    // Function Call

    int sum = add(a, b);

    printf("Sum: %d\n", sum);

    return 0;

}

// Function Definition

int add(int x, int y) {

    return x + y;

}

Explanation of Components

Function Declaration:

int add(int, int);

Return Type: Indicates the type of value the function returns, e.g., int.

Function Name: The name of the function, e.g., add.

Parameters: The types and names of parameters the function takes, e.g., int, int.

Function Definition:

int add(int x, int y)

 {

    return x + y;

}

Return Type: The same type as declared.

Function Name: The same name as declared.

Parameters: The names can be different from the declaration, but types must match.

Body: Contains statements that define what the function does. Here, it returns the sum of x and y.

Function Call:

int sum = add(a, b);

Function Name: The name of the function to be called.

Arguments: The actual values passed to the function, matching the parameters in type and order.

Example: Calculating Factorial

Let’s look at a slightly more complex example where we calculate the factorial of a number using a recursive function.

#include <stdio.h>

// Function Declaration

int factorial(int n);

int main() {

    int number = 5;

    // Function Call

    printf("Factorial of %d is %d\n", number, factorial(number));

    return 0;

}

// Function Definition

int factorial(int n) {

    if (n == 0) return 1;  // Base case

    else return n * factorial(n - 1);  // Recursive call

}

Explanation

Function Declaration: int factorial(int n);

Function Call: printf("Factorial of %d is %d\n", number, factorial(number));

Function Definition:

int factorial(int n) {

    if (n == 0) return 1;  // Base case

    else return n * factorial(n - 1);  // Recursive call

}

This example demonstrates recursion, where the factorial function calls itself with a decremented value of n until it reaches the base case (n == 0).

Key Points (need of user define functions)

Modularity: Functions break down a program into smaller, manageable parts.

Reusability: Functions can be reused across different programs or parts of the same program.

Readability: Functions make programs easier to read and understand.

Functions are fundamental in C programming, enabling structured, modular, and maintainable code. 


Q6: Difference between call by value and call by reference.

Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters.

The parameters passed to the function are called actual parameters whereas the parameters received by the function are called formal parameters.

Call By Value in C

In call by value method of parameter passing, the values of actual parameters are copied to the function’s formal parameters.

  • There are two copies of parameters stored in different memory locations.
  • One is the original copy and the other is the function copy.
  • Any changes made inside functions are not reflected in the actual parameters of the caller.

Example of Call by Value

The following example demonstrates the call-by-value method of parameter passing

// C program to illustrate call by value
#include <stdio.h>

// Function Prototype
void swapx(int x, int y);

// Main function
int main()
{
    int a = 10, b = 20;

    // Pass by Values
    swapx(a, b); // Actual Parameters

    printf("In the Caller:\na = %d b = %d\n", a, b);

    return 0;
}

// Swap functions that swaps
// two values
void swapx(int x, int y) // Formal Parameters
{
    int t;

    t = x;
    x = y;
    y = t;

    printf("Inside Function:\nx = %d y = %d\n", x, y);
}
Output
Inside Function:
x = 20 y = 10
In the Caller:
a = 10 b = 20

Thus actual values of a and b remain unchanged even after exchanging the values of x and y in the function.

Call by Reference in C

In call by reference method of parameter passing, the address of the actual parameters is passed to the function as the formal parameters. In C, we use pointers to achieve call-by-reference.

  • Both the actual and formal parameters refer to the same locations.
  • Any changes made inside the function are actually reflected in the actual parameters of the caller.

Example of Call by Reference

The following C program is an example of a call-by-reference method.

// C program to illustrate Call by Reference
#include <stdio.h>

// Function Prototype
void swapx(int*, int*);

// Main function
int main()
{
    int a = 10, b = 20;

    // Pass reference
    swapx(&a, &b); // Actual Parameters

    printf("Inside the Caller:\na = %d b = %d\n", a, b);

    return 0;
}

// Function to swap two variables
// by references
void swapx(int* x, int* y) // Formal Parameters
{
    int t;

    t = *x;
    *x = *y;
    *y = t;

    printf("Inside the Function:\nx = %d y = %d\n", *x, *y);
}
Output
Inside the Function:
x = 20 y = 10
Inside the Caller:
a = 20 b = 10

Thus actual values of a and b get changed after exchanging values of x and y.

Difference between the Call by Value and Call by 

Call By Value

Call By Reference

While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”.While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References.
In this method, the value of each variable in the calling function is copied into corresponding dummy variables of the called function.In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function.
With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function.With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them.
In call-by-values, we cannot alter the values of actual variables through function calls.In call by reference, we can alter the values of variables through function calls.
Values of variables are passed by the Simple technique.Pointer variables are necessary to define to store the address values of variables.
This method is preferred when we have to pass some small values that should not change.This method is preferred when we have to pass a large amount of data to the function.
Call by value is considered safer as original data is preservedCall by reference is risky as it allows direct modification in original data

Q7: What is recursion? Explain with an example.

Function calls it self is call Recursion.

Recursion occurs when a function calls itself to solve a smaller version of the problem.

In programming, recursion is a technique where a function calls itself to solve a problem. The idea is to break down a problem into smaller, similar subproblems, until you reach a base case that can be solved directly.

Example in C: Calculating Factorial

Example:


#include <stdio.h>

// Recursive function to calculate factorial
int factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n - 1); // Function calls itself
}
}
int main() {
int num = 5;
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
return 0;
}

Base case:

The if statement checks if n is 0. If so, the function returns 1, which is the factorial of 0.

Recursive case:

If n is not 0, the function calls itself with the argument n - 1. This continues until the base case is reached.

Unwinding:

Once the base case is reached, the recursive calls start "unwinding," returning values back up the chain of calls until the final result is returned to the original call.

In this example, factorial(5) calls factorial(4), which calls factorial(3), and so on until factorial(0) is reached. Then, the results are multiplied and returned back up the chain.

Key points:

  • Every recursive function must have a base case to stop the recursion.
  • Recursion can be a powerful technique, but it can also be less efficient than iterative solutions for some problems.
  • Use recursion when it makes the code clearer and more concise.

Explanation:

  • The factorial function calls itself to reduce the problem size.
  • For example, factorial(5) calculates 5 * factorial(4) and so on until factorial(1) is reached, which terminates the recursion.

Deep Learning with NLP Laboratory Btech sem 7 [203105477] Parul University



1. Implementation of Text Preprocessing with NLTK

Tokenization, Stemming, Lemmatization, and Removal of Stopwords:


import nltk from nltk.tokenize import word_tokenize from nltk.corpus import stopwords from nltk.stem import PorterStemmer, WordNetLemmatizer nltk.download('punkt') nltk.download('stopwords') nltk.download('wordnet') # Sample text text = "NLTK is a powerful library for text preprocessing in NLP tasks." # Tokenization tokens = word_tokenize(text) # Stemming stemmer = PorterStemmer() stemmed_words = [stemmer.stem(word) for word in tokens] # Lemmatization lemmatizer = WordNetLemmatizer() lemmatized_words = [lemmatizer.lemmatize(word) for word in tokens] # Removing Stopwords stop_words = set(stopwords.words('english')) filtered_words = [word for word in tokens if word.lower() not in stop_words] print("Tokens:", tokens) print("Stemmed Words:", stemmed_words) print("Lemmatized Words:", lemmatized_words) print("Filtered Words:", filtered_words)

2. Convert Text to Word Count Vectors with Scikit-Learn

Using CountVectorizer:


from sklearn.feature_extraction.text import CountVectorizer # Sample text data corpus = [ 'This is the first document.', 'This document is the second document.', 'And this is the third one.', 'Is this the first document?' ] vectorizer = CountVectorizer() X = vectorizer.fit_transform(corpus) print("Feature Names:", vectorizer.get_feature_names_out()) print("Word Count Vectors:\n", X.toarray())

3. Convert Text to Word Frequency Vectors with Scikit-Learn

Using TfidfVectorizer:


from sklearn.feature_extraction.text import TfidfVectorizer corpus = [ 'This is the first document.', 'This document is the second document.', 'And this is the third one.', 'Is this the first document?' ] tfidf_vectorizer = TfidfVectorizer() X_tfidf = tfidf_vectorizer.fit_transform(corpus) print("TF-IDF Feature Names:", tfidf_vectorizer.get_feature_names_out()) print("TF-IDF Vectors:\n", X_tfidf.toarray())

4. Convert Text to Unique Integers with Scikit-Learn

Using HashingVectorizer:


from sklearn.feature_extraction.text import HashingVectorizer corpus = [ 'This is the first document.', 'This document is the second document.', 'And this is the third one.', 'Is this the first document?' ] hashing_vectorizer = HashingVectorizer(n_features=10) X_hash = hashing_vectorizer.fit_transform(corpus) print("Hashing Vectors:\n", X_hash.toarray())

5. Using Keras to Split Words

Using text_to_word_sequence:


from keras.preprocessing.text import text_to_word_sequence # Sample text text = "Keras is a powerful deep learning library." # Split words words = text_to_word_sequence(text) print("Words:", words)

6. Encoding with Keras one_hot


from keras.preprocessing.text import one_hot # Sample text text = "Keras is a great library for deep learning." # Vocabulary size vocab_size = 50 # One-hot encode encoded_words = one_hot(text, vocab_size) print("Encoded words:", encoded_words)

7. Hash Encoding with Keras hashing_trick


from keras.preprocessing.text import hashing_trick # Sample text text = "Keras is an amazing library for NLP." # Hash Encoding vocab_size = 50 hashed_words = hashing_trick(text, vocab_size, hash_function='md5') print("Hashed words:", hashed_words)

8. Demo of Keras Tokenizer API


from keras.preprocessing.text import Tokenizer # Sample corpus corpus = [ 'Keras is a deep learning library.', 'It helps in NLP and other machine learning tasks.', 'We are learning text processing with Keras.' ] # Tokenization tokenizer = Tokenizer() tokenizer.fit_on_texts(corpus) # Word index word_index = tokenizer.word_index print("Word Index:", word_index) # Convert to sequences sequences = tokenizer.texts_to_sequences(corpus) print("Text Sequences:", sequences)

9. Sentiment Analysis Experiment

You can perform sentiment analysis on a dataset like Twitter tweets or movie reviews. Here's an example using the nltk movie_reviews dataset:


import nltk from nltk.corpus import movie_reviews from nltk.classify import NaiveBayesClassifier from nltk.classify.util import accuracy nltk.download('movie_reviews') # Extract features def extract_features(words): return {word: True for word in words} # Prepare data positive_reviews = [(extract_features(movie_reviews.words(fileid)), 'pos') for fileid in movie_reviews.fileids('pos')] negative_reviews = [(extract_features(movie_reviews.words(fileid)), 'neg') for fileid in movie_reviews.fileids('neg')] # Split dataset into training and testing train_data = positive_reviews[:800] + negative_reviews[:800] test_data = positive_reviews[800:] + negative_reviews[800:] # Train Naive Bayes classifier classifier = NaiveBayesClassifier.train(train_data) # Evaluate print("Accuracy:", accuracy(classifier, test_data)) # Show most informative features classifier.show_most_informative_features(10)

10. Word2Vec Embedding with Gensim


from gensim.models import Word2Vec # Sample sentences sentences = [ ['this', 'is', 'the', 'first', 'sentence'], ['this', 'is', 'another', 'sentence'], ['word2vec', 'is', 'cool'] ] # Train Word2Vec model model = Word2Vec(sentences, vector_size=100, window=5, min_count=1, workers=4) # Check word vector vector = model.wv['sentence'] print("Vector for 'sentence':", vector) # Check most similar words similar_words = model.wv.most_similar('sentence') print("Most similar to 'sentence':", similar_words)

Wednesday, October 9, 2024

History of C Programming Language

 

Introduction to the C Language

The C programming language has been a cornerstone of computer science for decades. Its simplicity and efficiency have made it one of the most widely used languages in the world. From operating systems to embedded systems, C is everywhere.

Have you ever wondered how this powerful language came into existence? Or what makes it stand out from its counterparts? Whether you're a seasoned programmer or just starting your coding journey, understanding the history of C can deepen your appreciation for its design and functionality.

Join us as we explore the fascinating evolution of the C programming language, uncovering its roots and significance along the way. You might find that there's more to C than meets the eye!

The Development of C

The C programming language emerged in the early 1970s, crafted by Dennis Ritchie at Bell Labs. It was born out of necessity to improve upon B, an earlier language that struggled with system-level programming.

Ritchie's work coincided with the development of Unix. C became the go-to language for writing operating systems and utilities. Its syntax drew inspiration from both B and BCPL, making it more powerful yet simpler.

With its flexible structure and efficiency, C rapidly gained popularity among programmers. By 1978, Brian Kernighan and Ritchie published "The C Programming Language," which solidified its status as a vital tool for developers everywhere.

As technology evolved, so did C. The ANSI standardization in 1989 updated it for modern computing needs while maintaining backward compatibility. This evolution ensured that programmers could rely on it across various platforms without losing functionality or performance.

Key Features and Benefits of C

The C programming language is renowned for its efficiency and flexibility. One of its standout features is low-level access to memory, allowing developers to manipulate hardware directly. This capability makes it an ideal choice for system programming.

Another significant benefit of C is its portability. Code written in C can be compiled on various platforms with minimal modifications, making it versatile across different operating systems.

C also boasts a rich set of operators and data types, enabling precise control over program behavior. Its structured approach promotes clean code organization and enhances readability.

The extensive library support available means that developers can tap into pre-existing functions, speeding up the development process while ensuring reliability. These attributes have solidified C's place as a foundational language in computer science education and software development.

Comparison with Other Programming Languages

The C programming language stands out in the crowded landscape of coding languages. Its efficiency and performance are often unmatched, making it a preferred choice for system-level programming.

When compared to higher-level languages like Python or Ruby, C offers greater control over hardware resources. This is crucial for applications that require speed and optimized memory usage.

Languages such as Java introduce additional layers with their virtual machine, which can slow down execution time. In contrast, C compiles directly to machine code, allowing programs to run faster on various platforms.

Moreover, while modern languages often prioritize ease of use and abstraction, C demands a deeper understanding of how computers work. This foundational knowledge can be invaluable for programmers transitioning into more complex environments later on.

Famous Programs Written in C

The C programming language has been the backbone of many influential software applications. One notable example is the operating system Unix, which was primarily developed in C in the late 1960s and early 1970s. Its portability and efficiency revolutionized how developers approached operating systems.

Another landmark program written in C is the GNU Compiler Collection (GCC). This powerful toolchain compiles code for various programming languages, making it a crucial resource for developers worldwide.

Additionally, major databases like MySQL owe their existence to C. Its ability to handle low-level data manipulation makes it ideal for performance-critical applications.

Even today, much of modern software relies on libraries or components initially crafted in C. Its enduring influence can be seen across countless fields—from embedded systems to high-performance computing environments.

Future of C and its Relevance Today

The C programming language continues to hold a significant place in the world of software development. Despite newer languages emerging, C remains foundational, influencing many modern languages like C++, Java, and Python.

Its efficiency makes it ideal for system-level programming. Operating systems, embedded systems, and performance-critical applications still rely heavily on C. This reliability ensures that skills in this language remain highly sought after.

With the rise of IoT (Internet of Things), C is becoming increasingly relevant again. Many microcontrollers use C due to its close-to-hardware nature and minimal overhead.

Educational institutions also emphasize learning C as a first programming language. It fosters a deep understanding of computer fundamentals. As technology evolves, so does the need for robust solutions that often trace back to this versatile language.

Conclusion: Why C is Still Important Today

The C programming language has stood the test of time, remaining relevant even in an ever-evolving tech landscape. Its efficiency and flexibility make it a popular choice for system-level programming, embedded systems, and even game development. As more modern languages emerge, they often draw inspiration from C's foundational concepts.

Its straightforward syntax allows programmers to easily grasp complex ideas while maintaining control over hardware. The widespread use of C in operating systems like Unix and Linux cements its significance in software development history. Moreover, understanding C lays a solid foundation for learning other programming languages.

As technology continues to advance, the principles established by the C programming language will likely influence future innovations. It's clear that mastering C is not just about working with legacy code; it's an essential skill that empowers developers today and tomorrow. This enduring relevance highlights why the C programming language remains important for both new learners and seasoned professionals alike.

CTSD model paper SET-6

 Instructions:                                                                                

1. This question paper comprises of two sections. Write answer of both the sections in separate answer books.

2. From Section I, Q.1 is compulsory, attempt any THREE from Q. 2 to Q. 5

3. From Section II, Q.6 is compulsory, attempt any THREE from Q. 7 to Q. 10

4. Make suitable assumptions wherever necessary.

5. Start new question on new page.

CTSD Paper model SET-5

Instructions:                                                                                

1. This question paper comprises of two sections. Write answer of both the sections in separate answer books.

2. From Section I, Q.1 is compulsory, attempt any THREE from Q. 2 to Q. 5

3. From Section II, Q.6 is compulsory, attempt any THREE from Q. 7 to Q. 10

4. Make suitable assumptions wherever necessary.

5. Start new question on new page.

CTSD Model paper SET-4

Instructions:                                                                                

1. This question paper comprises of two sections. Write answer of both the sections in separate answer books.

2. From Section I, Q.1 is compulsory, attempt any THREE from Q. 2 to Q. 5

3. From Section II, Q.6 is compulsory, attempt any THREE from Q. 7 to Q. 10

4. Make suitable assumptions wherever necessary.

5. Start new question on new page.


Section-A (30 Marks)

Q.1 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define the ‘do-while’ loop.
  2. List the basic data types in C.
  3. State the purpose of the ‘goto’ statement.

Q.2 Answer the following questions.
A) Define flowchart symbols. (2 Marks)
B) Explain the history of C language. (6 Marks)

Q.3 Answer the following questions.
C) What is an enum in C? (2 Marks)
D) Write a program to find the average of three numbers. (6 Marks)

Q.4 Answer the following questions.
A) Define a 2D array. (2 Marks)
B) Write a program to sort an array in ascending order. (6 Marks)

Q.5 Answer the following questions.
A) Explain call by reference. (2 Marks)
B) Write a program to reverse a given number. (6 Marks)


Section-B (30 Marks)

Q.6 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define typecasting in C.
  2. What is the purpose of the ‘break’ statement?
  3. List the string functions in C.

Q.7 Answer the following questions.
A) Describe the structure of a C program. (2 Marks)
B) Explain the use of recursion with an example. (6 Marks)

Q.8 Answer the following questions.
A) What is a switch case? (2 Marks)
B) Write a program to check whether a number is Armstrong. (6 Marks)

Q.9 Answer the following questions.
A) Define nested loops. (2 Marks)
B) Write a program to generate a multiplication table using a loop. (6 Marks)

Q.10 Answer the following questions.
A) What are string functions? (2 Marks)
B) Write a program to count the total number of words in a string. (6 Marks)

CTSD model Question paper SET-3

Instructions:                                                                                

1. This question paper comprises of two sections. Write answer of both the sections in separate answer books.

2. From Section I, Q.1 is compulsory, attempt any THREE from Q. 2 to Q. 5

3. From Section II, Q.6 is compulsory, attempt any THREE from Q. 7 to Q. 10

4. Make suitable assumptions wherever necessary.

5. Start new question on new page.

aper Set 4

Section-A (30 Marks)

Q.1 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define type conversion.
  2. List the components of a C program.
  3. State the use of the ‘continue’ statement.

Q.2 Answer the following questions.
A) Define nested if-else statements. (2 Marks)
B) Explain the concept of user-defined functions in C. (6 Marks)

Q.3 Answer the following questions.
C) What is a 1D array? (2 Marks)
D) Write a program to generate a Fibonacci series up to N numbers. (6 Marks)

Q.4 Answer the following questions.
A) Define a string in C. (2 Marks)
B) Write a program to toggle the case of characters in a string. (6 Marks)

Q.5 Answer the following questions.
A) Explain call by value. (2 Marks)
B) Write a program to find the factorial of a number using recursion. (6 Marks)


Section-B (30 Marks)

Q.6 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define the ‘for’ loop.
  2. What is a string function?
  3. State the difference between call by value and call by reference.

Q.7 Answer the following questions.
A) List the branching statements in C. (2 Marks)
B) Explain how the ‘switch’ statement works with an example. (6 Marks)

Q.8 Answer the following questions.
A) What is a function prototype? (2 Marks)
B) Write a program to generate a result sheet for 5 students using a loop. (6 Marks)

Q.9 Answer the following questions.
A) Define arrays in C. (2 Marks)
B) Write a program to reverse the order of numbers in an array. (6 Marks)

Q.10 Answer the following questions.
A) What is recursion? (2 Marks)
B) Write a program to create a calculator using user-defined functions. (6 Marks)

CTSD Model Question Paper for practice SET-2

Instructions:                                                                                

1. This question paper comprises of two sections. Write answer of both the sections in separate answer books.

2. From Section I, Q.1 is compulsory, attempt any THREE from Q. 2 to Q. 5

3. From Section II, Q.6 is compulsory, attempt any THREE from Q. 7 to Q. 10

4. Make suitable assumptions wherever necessary.

5. Start new question on new page.


Section-A (30 Marks)

Q.1 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define recursion in programming.
  2. List the loop control statements in C.
  3. State the components of a function in C.

Q.2 Answer the following questions.
A) Define a 2D array. (2 Marks)
B) Explain the concept of nested loops with an example. (6 Marks)

Q.3 Answer the following questions.
C) What is a string function in C? (2 Marks)
D) Write a program to find the maximum of three numbers using else-if ladder. (6 Marks)

Q.4 Answer the following questions.
A) Define matrix in C. (2 Marks)
B) Write a program to multiply two matrices. (6 Marks)

Q.5 Answer the following questions.
A) Explain the use of the ‘break’ statement in loops. (2 Marks)
B) Write a program to check whether a given number is a palindrome. (6 Marks)


Section-B (30 Marks)

Q.6 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define a loop in programming.
  2. What is an operator in C?
  3. State the types of user-defined functions.

Q.7 Answer the following questions.
A) List the string functions available in C. (2 Marks)
B) Explain the difference between pre-decrement and post-decrement operators. (6 Marks)

Q.8 Answer the following questions.
A) What is a nested if-else statement? (2 Marks)
B) Write a program to generate an electricity bill based on unit consumption. (6 Marks)

Q.9 Answer the following questions.
A) Define an algorithm. (2 Marks)
B) Write a program to add two matrices. (6 Marks)

Q.10 Answer the following questions.
A) What is a flowchart? (2 Marks)
B) Write a program to arrange a given set of numbers in descending order. (6 Marks)

CTSD - Model Question Paper of 60 Marks for practice SET-1

Instructions:                                                                                

1. This question paper comprises of two sections. Write answer of both the sections in separate answer books.

2. From Section I, Q.1 is compulsory, attempt any THREE from Q. 2 to Q. 5

3. From Section II, Q.6 is compulsory, attempt any THREE from Q. 7 to Q. 10

4. Make suitable assumptions wherever necessary.

5. Start new question on new page.


Section-A (30 Marks)

Q.1 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define typecasting in C.
  2. List the different data types in C.
  3. State the use of the ‘continue’ statement in loops.

Q.2 Answer the following questions.
A) Define a function in C. (2 Marks)
B) Explain the concept of call by reference with an example. (6 Marks)

Q.3 Answer the following questions.
C) What is a 1D array? (2 Marks)
D) Write a program to generate the first 10 natural numbers using a loop. (6 Marks)

Q.4 Answer the following questions.
A) Define a string in C. (2 Marks)
B) Write a program to find the length of a string. (6 Marks)

Q.5 Answer the following questions.
A) Explain recursion with an example. (2 Marks)
B) Write a program to find the area of a circle using a function. (6 Marks)


Section-B (30 Marks)

Q.6 Objective Type Questions - (State, Define, List, etc) (6 Marks)

  1. Define a loop.
  2. What is an array in C?
  3. State the difference between 'while' and 'do-while' loops.

Q.7 Answer the following questions.
A) List the different types of loops in C. (2 Marks)
B) Explain the use of the ‘for’ loop with an example. (6 Marks)

Q.8 Answer the following questions.
A) What is a switch case? (2 Marks)
B) Write a program to check whether a number is prime. (6 Marks)

Q.9 Answer the following questions.
A) Define nested loops. (2 Marks)
B) Write a program to generate a multiplication table for a given number using nested loops. (6 Marks)

Q.10 Answer the following questions.
A) What is a user-defined function? (2 Marks)
B) Write a program to calculate the factorial of a number using recursion. (6 Marks)