Class 9th CBSE

Programming Skill (204)

Programming Skill (204)

Assignment

Explain Two-dimensional Array with Example.

A two-dimensional array is a data structure that represents a table or matrix of elements. It is essentially an array of arrays, where each element is identified by two indices – the row index and the column index. This structure allows for the representation of data in a grid-like format.
Syntax for declaring a two-dimensional array in C programming:
data_type array_name[row_size][column_size];
Here’s an example in C to illustrate the concept:
#include <stdio.h>
int main() {
// Declaration of a 2D array with 3 rows and 4 columns
int matrix[3][4];
// Initialization of the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = i * 4 + j + 1;
}
}
// Accessing and printing elements of the 2D array
printf(“Matrix:\n”);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf(“%3d “, matrix[i][j]);
}
printf(“\n”);
}
return 0;
}
Explanation:
1.Declaration and Initialization:
int matrix[3][4]; declares a 2D array named matrix with 3 rows and 4 columns.
The nested for loops are used to initialize each element in the array. In this example, the elements are assigned values based on their position in the matrix.
2.Accessing and Printing:
The nested for loops are used again to access and print the elements of the 2D array.
The format specifier %3d in the printf statement ensures that each element is printed in a field of width 3, providing a neat and aligned output.

Differentiate between Structure and Union. Explain structure in detail.

The struct keyword is used to define a structure.
The union keyword is used to define union.
When the variables are declared in a structure, the compiler allocates memory to each variables member. The size of a structure is equal or greater to the sum of the sizes of each data member.
When the variable is declared in the union, the compiler allocates memory to the largest size variable member. The size of a union is equal to the size of its largest data member size.
Each variable member occupied a unique memory space.
Variables members share the memory space of the largest size variable.
Changing the value of a member will not affect other variables members.
Changing the value of one member will also affect other variables members.
Each variable member will be assessed at a time.
Only one variable member will be assessed at a time.
We can initialize multiple variables of a structure at a time.
In union, only the first data member can be initialized.
All variable members store some value at any point in the program.
Exactly only one data member stores a value at any particular instance in the program.
The structure allows initializing multiple variable members at once.
Union allows initializing only one variable member at once
It is used to store different data type values.
It is used for storing one at a time from different data type values.

Explain Category of function in detail.

A function is a block of statements that can perform a particular task. As we all know, there is always at least one function in C, and that is main().
Example
In the example below, the function’s name is sum and the data type is int. This task of this function is to produce the sum of two numbers:
int sum(int a,int b)
{
return(a+b);
}
Below, the function is declared in main():
void main()
{
int sum(int,int); //function declaration
int x=5,y=6;
total = sum(x,y);
}
Formal parameters and actual parameters
When we call a function in main() or anywhere else in the program, and the function we created needs parameters, we would pass parameters to it while calling the function. In the example above, we passed variables x and y to obtain the sum of x and y.
According to the example above, the formal parameters are a and b, and the *actual *parameters are x and y.
Essentially, the variables being passed in the function call are actual parameters, and the variables being initialized and used in the function are formal* *parameters.
Advantages of a function
A function can be used any number of times after it is defined once.
Functions make programs manageable and easy to understand.
Function categories
There are 4 types of functions:
1. Functions with arguments and return values
This function has arguments and returns a value:
#include <stdio.h>
// Function declaration
int addNumbers(int a, int b);
int main() {
// Example usage
int num1 = 5, num2 = 7;
int sumResult = addNumbers(num1, num2);
// Printing the result
printf(“The sum of %d and %d is: %d\n”, num1, num2, sumResult);
return 0;
}
// Function definition
int addNumbers(int a, int b) {
// Adding two numbers and returning the result
int result = a + b;
return result;
}
2. Functions with arguments and without return values
This function has arguments, but it does not return a value:
#include <stdio.h>
int main()
{
void sum(float,float); //function with arguments and no return value
float x=10.56,y=7.22;
sum(x,y);
}
void sum(float a,float b) //function with arguments and no return value
{
float z = a+b;
printf(“x + y = %f”,z);
}
3. Functions without arguments and with return values
This function has no arguments, but it has a return value:
#include<stdio.h>
int main()
{
int sum();
int c = sum();
printf(“Sum = %d”,c);
}
int sum() //function with no arguments and return data type
{
int x=10,y=20,z=5;
printf(“x = %d ; y = %d ; z = %d \n”,x,y,z);
int sum = x+y+z;
return(sum);
}
4. Functions without arguments and without return values
This function has no arguments and no return value:
#include<stdio.h>
int main()
{
void sum();
sum();
}
void sum() //function with no arguments and return data type
{
int x=15,y=35,z=5;
printf(“x = %d ; y = %d ; z = %d \n”,x,y,z);
int sum = x+y+z;
printf(“Sum = %d”,sum);
}
 
 

Write a note on set operation and explain with Example.

Set operations are mathematical operations that can be performed on sets, which are collections of distinct elements. Common set operations include union, intersection, difference, and complement. These operations help manipulate and analyze sets in various mathematical and programmin1. Union of Sets:
1. Union of Sets A and B is defined to be the set of all those elements which belong to A or B or both and is denoted by A∪B.g contexts.

A∪B = {x: x ∈ A or x ∈ B}

Example: Let A = {1, 2, 3}, B= {3, 4, 5, 6}
A∪B = {1, 2, 3, 4, 5, 6}.

2. Intersection of Sets: Intersection of two sets A and B is the set of all those elements which belong to both A and B and is denoted by A ∩ B.

A ∩ B = {x: x ∈ A and x ∈ B}

Example: Let A = {11, 12, 13}, B = {13, 14, 15}
A ∩ B = {13}.

3. Difference of Sets: The difference of two sets A and B is a set of all those elements which belongs to A but do not belong to B and is denoted by A – B.

A – B = {x: x ∈ A and x ∉ B}

Example: Let A = {1, 2, 3, 4} and B = {3, 4, 5, 6} then A – B = {3, 4} and B – A = {5, 6}

4. Complement of a Set: The Complement of a Set A is a set of all those elements of the universal set which do not belong to A and is denoted by Ac.

Ac = U – A = {x: x ∈ U and x ∉ A} = {x: x ∉ A}

Example: Let U is the set of all natural numbers.
A = {1, 2, 3}
Ac = {all natural numbers except 1, 2, and 3}.

5. Symmetric Difference of Sets: The symmetric difference of two sets A and B is the set containing all the elements that are in A or B but not in both and is denoted by A ⨁ B i.e.

A ⨁ B = (A ∪ B) – (A ∩ B)
Example: Let A = {a, b, c, d}
B = {a, b, l, m}
A ⨁ B = {c, d, l, m}

#include <stdio.h>
// Function to perform the union of two sets
void unionSets(int set1[], int size1, int set2[], int size2, int result[], int *resultSize) {
int i, j;
// Copy elements of set1 to the result
for (i = 0; i < size1; i++) {
result[i] = set1[i];
}
// Check and add elements from set2 that are not already in the result
for (i = 0; i < size2; i++) {
int isInResult = 0;
for (j = 0; j < *resultSize; j++) {
if (set2[i] == result[j]) {
isInResult = 1;
break;
}
}
if (!isInResult) {
result[*resultSize] = set2[i];
(*resultSize)++;
}
}
}
// Function to perform the intersection of two sets
void intersectionSets(int set1[], int size1, int set2[], int size2, int result[], int *resultSize) {
int i, j;
// Check and add elements from set1 that are also in set2 to the result
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
result[*resultSize] = set1[i];
(*resultSize)++;
break;
}
}
}
}
// Function to perform the difference of two sets (set1 – set2)
void differenceSets(int set1[], int size1, int set2[], int size2, int result[], int *resultSize) {
int i, j;
// Check and add elements from set1 that are not in set2 to the result
for (i = 0; i < size1; i++) {
int isInSet2 = 0;
for (j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
isInSet2 = 1;
break;
}
}
if (!isInSet2) {
result[*resultSize] = set1[i];
(*resultSize)++;
}
}
}
// Function to display the elements of a set
void displaySet(int set[], int size) {
printf(“{ “);
for (int i = 0; i < size; i++) {
printf(“%d “, set[i]);
}
printf(“}\n”);
}
int main() {
// Example sets
int set1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(set1) / sizeof(set1[0]);
int set2[] = {3, 4, 5, 6, 7};
int size2 = sizeof(set2) / sizeof(set2[0]);
// Arrays to store the results of set operations
int unionResult[10], intersectionResult[10], differenceResult[10];
int resultSize;
// Perform set operations
printf(“Set 1: “);
displaySet(set1, size1);
printf(“Set 2: “);
displaySet(set2, size2);
resultSize = 0;
unionSets(set1, size1, set2, size2, unionResult, &resultSize);
printf(“Union (Set 1 ∪ Set 2): “);
displaySet(unionResult, resultSize);
resultSize = 0;
intersectionSets(set1, size1, set2, size2, intersectionResult, &resultSize);
printf(“Intersection (Set 1 ∩ Set 2): “);
displaySet(intersectionResult, resultSize);
resultSize = 0;
differenceSets(set1, size1, set2, size2, differenceResult, &resultSize);
printf(“Difference (Set 1 – Set 2): “);
displaySet(differenceResult, resultSize);
return 0;
}

Explain Numpy with example.

NumPy is a powerful library in Python for numerical operations. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these elements. NumPy is widely used in scientific computing, data analysis, and machine learning applications.
Installation:
Before using NumPy, you need to install it. You can install NumPy using the following command:
pip install numpy

NumPy is a powerful library in Python for numerical operations. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these elements. NumPy is widely used in scientific computing, data analysis, and machine learning applications.
Installation:
Before using NumPy, you need to install it. You can install NumPy using the following command:
bash
Copy code
pip install numpy
Example: Basic Operations with NumPy:
Let’s look at a simple example that demonstrates some basic operations using NumPy arrays.
import numpy as np
# Creating NumPy arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Basic operations
sum_result = arr1 + arr2
product_result = arr1 * arr2
dot_product_result = np.dot(arr1, arr2)

# Displaying results
print(“Array 1:”, arr1)
print(“Array 2:”, arr2)
print(“Sum:”, sum_result)
print(“Product:”, product_result)
print(“Dot Product:”, dot_product_result)
Output :-
Array 1: [1 2 3]
Array 2: [4 5 6]
Sum: [5 7 9]
Product: [ 4 10 18]
Dot Product: 32
 
 
Fun & Easy to follow
Works on all devices
Your own Pace
Super Affordable

Popular Videos

Play Video

UX for Teams

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer

Play Video

SEO & Instagram

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer