0% found this document useful (0 votes)
20 views10 pages

What Is An Array? Array

The document provides an overview of arrays and strings in C, including their definitions, characteristics, and initialization methods. It explains both 1D and 2D arrays, string operations, and differences between structures and arrays. Additionally, it includes code examples for various operations such as calculating averages, reversing strings, and demonstrating string functions.

Uploaded by

surajandhale1600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

What Is An Array? Array

The document provides an overview of arrays and strings in C, including their definitions, characteristics, and initialization methods. It explains both 1D and 2D arrays, string operations, and differences between structures and arrays. Additionally, it includes code examples for various operations such as calculating averages, reversing strings, and demonstrating string functions.

Uploaded by

surajandhale1600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

FPL Assignment 4:Q & A

Q.1 What is Array? Explain array initialization with example.

What is an Array?

An array is a data structure that stores a collection of elements, all of the same type, in a
contiguous block of memory. It allows you to store multiple values in a single variable, with
each element being accessed by its index (position in the array).

Key Characteristics of Arrays:

1. Fixed Size: The size of an array is fixed at the time of its creation. This means you
cannot change the size of an array after it's initialized
2. Homogeneous: All elements in an array must be of the same data type
3. Indexed: Each element in an array is accessed using an index. In most programming
languages, array indexing starts at 0.

Types of Array Initialization:

1. Static Initialization: In static initialization, you directly assign values to an array at


the time of its creation.

Example:

int numbers[]= {1, 2, 3, 4, 5};

In this example:

o An integer array numbers is declared.


o The array is initialized with values {1, 2, 3, 4, 5}.
o The size of the array is automatically determined based on the number of
elements (5).
2. Dynamic Initialization: In dynamic initialization, you define the size of the array
first and then assign values later.

Example :

int numbers[5]; // Declaring an array of 5 integers


numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

Q.2 Explain 2D Array with initialization and its example.


A 2D array (two-dimensional array) is essentially an array of arrays. It is used to store data
in a table-like structure with rows and columns, where each element is accessed using two
indices: one for the row and one for the column.

In C, a 2D array is a matrix-like structure, where elements can be stored in a grid format (like
a table). It can be thought of as an array of arrays, where each element in the main array
represents a row, and each row contains a set of values (columns).

2D Array Initialization

There are two ways to initialize a 2D array in C:

1. Static Initialization (with values predefined at compile-time).


2. Dynamic Initialization (with values assigned during runtime).

1. Static Initialization of 2D Array in C

When you know the values you want to store in the array, you can initialize a 2D array at the
time of declaration by specifying both the row and column sizes, and providing the values in
a matrix format.

Syntax:
data_type array_name[row_size][column_size] = {
{value1, value2, ..., valueN},
{value1, value2, ..., valueN},
...
};
Example:

#include <stdio.h>
int main() {
// Declare and initialize a 2D array (3 rows and 4 columns)
int arr[3][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8}, // Second row
{9, 10, 11, 12} // Third row
};

// Display the elements of the array


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}
Output:

1234
5678
9 10 11 12

In this example:

 A 2D array arr is declared with 3 rows and 4 columns.


 The array is initialized with values for each row. Each row contains 4 values.

2. Dynamic Initialization of 2D Array in C

In dynamic initialization, you can define the array with a fixed size but initialize the values
during the execution of the program (runtime). You can achieve this by assigning values to
each element in a loop.

Example:
#include <stdio.h>

int main() {
int arr[3][4]; // Declare a 2D array with 3 rows and 4 columns

// Dynamic initialization: manually setting values


int value = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = value++; // Assigning values incrementally
}
}

// Display the elements of the array


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Output:

1234
5678
9 10 11 12

In this example:

 A 2D array arr is declared with 3 rows and 4 columns.


 A variable value is used to dynamically initialize the array by incrementing it row by
row and column by column.

Q.3 Explain String and its initialization with example.

In C, a string is a sequence of characters stored in an array of type char. It is terminated by a


special character known as the null terminator, represented by '\0'. This null terminator
marks the end of the string. Unlike higher-level languages where strings are a data type, in C,
strings are essentially arrays of characters.

Characteristics of a String in C:

1. Character Array: A string is a series of characters stored in an array of type char.


2. Null Terminated: A string always ends with the null character '\0' to mark the end.
3. Fixed Size: The size of the array must be large enough to hold all characters and the
null terminator.

String Initialization in C

There are different ways to initialize a string in C. Here are the common methods:

1. Static Initialization

This is the most common way of initializing a string, using a string literal.

Syntax:

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

In this case:

 str is a character array.


 "Hello, World!" is a string literal, and the compiler automatically adds the null
terminator '\0' at the end of the string.

Example:

#include <stdio.h>

int main() {

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

printf("String: %s\n", str);

return 0;
}

Output
String: Hello, World!

In this example:

 The string "Hello, World!" is stored in a character array str.


 The null terminator is automatically appended at the end of the string (i.e., '\0').

2. Explicit Initialization (Character by Character)

You can also initialize a string by specifying each character individually and adding the null
terminator manually.

Example:
#include <stdio.h>

int main() {

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

printf("String: %s\n", str);

return 0;
}

Output:

String: Hello
Q.4 WAP to calculate average of 5 numbers.
#include <stdio.h>
int main()
{

int numbers[5], sum = 0;


float average;

printf("Enter five numbers:\n");

for (int i = 0; i < 5; i++) {


scanf("%d", &numbers[i]);
}

for (int i = 0; i < 5; i++)


{
sum =sum+numbers[i];
}

average = sum / 5;

printf("The average of the five numbers is: %f\n", average);


return 0;
}

Q.5 Explain all string built in operations with example.

1.strlen()

 Purpose: Computes the length of a string (excluding the null terminator \0).
 Syntax: strlen(str);;

Example:

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

int main() {
char str[] = "Hello, World!";
printf("Length of string: %d\n", strlen(str)); // Output: 13
return 0;
}

Explanation: The strlen() function returns the number of characters in the string before the
null terminator.

2. strcpy()

 Purpose: Copies a string from a source to a destination.


 Syntax: strcpy(dest, src);;

Example:

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

int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src); // Copy src to dest
printf("Source: %s\n", src); // Output: Hello, World!
printf("Destination: %s\n", dest); // Output: Hello, World!
return 0;
}

Explanation: The strcpy() function copies the contents of src into dest.

3. strncpy()

 Purpose: Copies a specified number of characters from one string to another.


 Syntax: strncpy( dest, src, n );
Example:

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

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

strncpy(dest, src, 5); // Copy first 5 characters from src to dest


dest[5] = '\0'; // Manually adding null terminator
printf("Destination: %s\n", dest); // Output: Hello

return 0;
}

Explanation: The strncpy() function copies up to n characters from the source string to the
destination string. In this case, it copies only the first 5 characters.

4. strcat()

 Purpose: Concatenates (appends) one string to the end of another.


 Syntax: strcat(dest, src);
 Example:

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

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

strcat(str1, str2); // Append str2 to str1


printf("Concatenated string: %s\n", str1); // Output: Hello, World!

return 0;
}

Explanation: The strcat() function appends the src string to the dest string. Note that dest
must have enough space to accommodate the resulting concatenated string.

5. strncat()

 Purpose: Concatenates a specified number of characters from one string to another.


 Syntax: strncat(dest, src, n);

Example:

#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";

strncat(str1, str2, 3); // Append the first 3 characters of str2 to str1


printf("Concatenated string: %s\n", str1); // Output: Hello, Wor

return 0;
}

Explanation: The strncat() function appends the first n characters from src to dest.

6. strcmp()

 Purpose: Compares two strings lexicographically.


 Syntax: strcmp(s1, s2);

Return Value:

 0: If the strings are equal.


 > 0: If str1 is greater than str2.
 < 0: If str1 is smaller than str2.

Example

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

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

int result = strcmp(str1, str2);

if (result == 0) {
printf("Strings are equal.\n");
} else if (result > 0) {
printf("str1 is greater than str2.\n");
} else {
printf("str1 is less than str2.\n");
}
return 0;
}

Explanation: The strcmp() function compares two strings character by character and returns
a value indicating their lexicographical order.

Q.6..Explain difference between structure and array.


 Array: An array is a collection of elements of the same data type stored in
contiguous memory locations. It can hold multiple values, but all values must be of
the same type (e.g., all integers, all floats, etc.).
 Structure: A structure is a user-defined data type that can hold different types of
data (e.g., integers, floats, strings) together as a single unit. The elements of a
structure are called members and can be of different data types.

2. Data Type:

 Array: All elements in an array must be of the same type (e.g., all integers, all floats).
 Structure: Members of a structure can be of different types (e.g., a structure can have
an integer, a float, and a string).

3. Memory Allocation:

 Array: Memory is allocated in a contiguous block for all elements. The size of the
array is fixed (if it's statically allocated).
 Structure: Memory is allocated for each member according to its data type. The
members may not necessarily be contiguous, and the size of the structure is the sum of
the sizes of its members, plus any padding needed for alignment.

4. Accessing Elements:

 Array: Elements in an array are accessed using an index. For example, array[0] refers
to the first element in the array.
 Structure: Members in a structure are accessed using the member name. For
example, person.name accesses the name member of a structure person.

5. Purpose:

 Array: Typically used when you want to store multiple values of the same type (e.g.,
a list of numbers, a list of names).
 Structure: Useful when you need to represent a complex entity that has different
properties (e.g., a person's name, age, and height).

Example in C:

 Array:

int arr[3] = {1, 2, 3}; // Array of integers

 Structure:

struct Person {
char name[50];
int age;
float height;
};

struct Person person1 = {"Alice", 30, 5.5};


Q.7 WAP to reverse a string.
#include <stdio.h>
#include <string.h>

int main() {
char s[] = "abcde";

// Reversing string using strrev()


printf("%s", strrev(s));

return 0;
}
Q.8 WAP to calculate the length of string using built in function
#include <stdio.h>
#include <string.h>

int main() {
char s[] = "Geeks";

// Find length of string using strlen()


printf("%d", strlen(s));

return 0;
}
Q.9 WAP to demonstrate strcat () function.
#include <stdio.h>
#include <string.h>

int main() {
char str1[50] = "Hello, ";
char str2[] = "world!";

strcat(str1, str2);

printf("Concatenated string: %s\n", str1);

return 0;
}

You might also like