PPS Assignment 2.1 CSE

1. What is an array? Explain with example.

Ans: An array is a collection of elements of the same data type that are stored in a contiguous block of memory. Each element in the array can be accessed using an index that starts from 0. 

For example, to declare an array of integers with 5 elements, the syntax would be:  int arr[5]; 

To access an element in the array, we use the index of that element inside square brackets. The index starts from 0, which means the first element in the array has an index of 0, the second element has an index of 1, and so on. here is example read 10 integers in an array. Find the addition of all elements.

#include <stdio.h> 
#include <conio.h> 
void main() 
{     i
nt arr[10], i, sum = 0;     
clrscr();     
printf("Enter 10 integers:\n");     
for (i = 0; i < 10; i++) {        
 scanf("%d", &arr[i]);     
}     
for (i = 0; i < 10; i++) {        
 sum += arr[i];     
}    
printf("Sum of all elements in the array is %d", sum);     
getch(); 
}

2. Explain Classification of an array.

Ans:

  1. One-dimensional array: Also known as a vector or a linear array, a one-dimensional array is a collection of elements arranged in a single row. Each element in the array is accessed using its index, which starts from zero.
  2. Multi-dimensional array: A multi-dimensional array is an array with two or more dimensions. It can be thought of as a table with rows and columns, where each cell contains an element. Common examples of multi-dimensional arrays include two-dimensional (2D) arrays, three-dimensional (3D) arrays, and higher-dimensional arrays.
  3. Static array: A static array is an array whose size is fixed at the time of its creation and cannot be changed during runtime. Static arrays are typically allocated on the stack or in the data segment of the program.
  4. Dynamic array: A dynamic array is an array whose size can be changed during runtime. Dynamic arrays are typically implemented using pointers and memory allocation functions like malloc() and free().

3. Explain array declaration and initialization.

Ans: In programming, an array is a collection of elements of the same type that are stored in contiguous memory locations. To use an array in a program, it must be declared and initialized.

Array Declaration:
To declare an array, you must specify its data type and its name, and optionally, the number of elements it will contain. The syntax for declaring an array is as follows:

data_type array_name[array_size];

Where,

  • data_type is the data type of the elements in the array.
  • array_name is the name of the array.
  • array_size is the number of elements in the array. This is optional, and you can omit it if you want to declare an array of unknown size.

For example, to declare an integer array of size 5, you would write:

int numbers[5];

This creates an integer array named numbers that can hold 5 integer values.

Array Initialization:
After you declare an array, you can initialize its elements by assigning values to them. There are several ways to initialize an array:

  1. Initialize individual elements: You can initialize each element of the array one by one using the array index. For example:
int numbers[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
  1. Initialize all elements at once: You can initialize all elements of the array at once using an initialization list. For example:
int numbers[5] = {1, 2, 3, 4, 5};

This creates an integer array named numbers that contains the values 1, 2, 3, 4, and 5.

  1. Partial initialization: You can initialize some of the elements of the array and leave the rest uninitialized. For example:
int numbers[5] = {1, 2};

This creates an integer array named numbers that contains the values 1 and 2, and the rest of the elements are initialized to 0.

In summary, to declare an array, you must specify its data type, name, and size (optional), while to initialize an array, you can either assign values to individual elements or use an initialization list to assign values to all elements at once.

4. What do you mean by String? Explain with example.

Ans: In C language, a string is an array of characters terminated by a null character ‘\0’. The null character is used to indicate the end of the string. For example, the string “hello” is represented in C as an array of characters {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}. The length of this string is 5 characters, but the size of the array is 6 (including the null character). C provides a number of built-in functions to work with strings. Here are some commonly used string functions in C:

strlen(): calculates the length of a string (excluding the null character).

strcpy(): copies a string from one location to another.

strcat(): concatenates two strings into one. strcmp(): compares two strings to see if they are equal.

strchr(): searches a string for a given character and returns a pointer to its first occurrence.

To use these functions, you need to include the header file string.h. C also provides a way to declare a string literal directly in your code, without having to manually create an array of characters. For example, you can write “hello” instead of {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}. The compiler automatically creates an array of characters for you and adds the null character at the end.

5. Differentiate scanf() and Gets() in terms of string.

Ans: scanf() function :

  • scanf() is a general-purpose input function that can be used to read different types of data from the standard input stream, including strings.
  • It reads a string from the input stream until it encounters a white space character (such as a space or a newline) and stores the string in the specified variable. It stops reading when it encounters the white space character or when the maximum field width is reached.
  • The scanf() function is prone to buffer overflow and security issues, so it is not recommended for reading input strings.

gets() function :

  • gets() is a string input function that reads a line of text from the standard input stream and stores it in a buffer.
  • It reads input until a newline character or the end-of-file is reached and stores the input in the buffer. It also appends a null character (‘\0’) to the end of the string.
  • The gets() function is deprecated and not recommended for use in modern C programming because it is vulnerable to buffer overflow attacks. Instead, the fgets() function should be used for reading input strings, as it provides better security by allowing you to specify the maximum size of the buffer.

6. List out string handling function and explain any two in detail.

Ans:

  • strlen
  • strcpy
  • strncpy
  • strcat
  • strncat
  • strcmp
  • strncmp
  • strstr
  • strchr
  • strrchr
  • strlen() : This function is used to determine the length of a string, i.e., the number of characters in the string. It takes a string as input and returns an integer value representing the length of the string.
  • strcpy() : This function is used to copy one string to another. It takes two strings as input – the destination string and the source string – and copies the contents of the source string to the destination string.

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top