Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Array

1. What is array? What is the main facility of using array?


OR
Why array is used in program.
Ans:

Array:
An array is a fixed-sized sequenced collection of elements of the same data type. It is simply a
grouping of like-type data. Arrays are defined in much the same manner as ordinary variables,
except that each array must be accompanied by a size specification (i.e., number of elements).

Why use?
In many applications, we need to handle large volume of data in terms of reading, processing and
printing. Array can provide the facility of efficient storing, accessing and manipulation of such
large amounts of data items.

The main facility of using array is that it has the ability to use a single name to represent a
collection of items and to refer to an item by specifying the item number which enables us to
develop concise and efficient program.

2. How can we declare and initialize of one dimensional array and two dimensional array?
Ans:

One dimensional array:


The general form of one-dimensional array declaration is:

type array-name[size];

The type specifies the type of element that will be contained in the array, such as int, float, or
char and the size indicates the maximum number of elements that can be stored inside the array.

For example,
char name[30];

The general form of initialization of one-dimensional array is:

type array-name[size]={list of values};


For example,
int number[3]={0,0,0};

will declare the variable number as an array of size 3 and will assign zero to each element.
Dr. Abu Nowshed Chy 1
Lecturer, Dept. of CSE, CU
Two dimensional array:
The general form of two-dimensional array declaration is:

type array-name[row_size][column_size];

The type specifies the type of element that will be contained in the array, such as int, float, or
char.
For example,
int grades[NUM_STUDENTS][NUM_TESTS];

The general form of initialization of two-dimensional array is:

type array-name[row_size][column_size]={list of values};


For example,
int table[2][3]={0,0,0,1,1,1};

initializes the elements of the first row to zero and the second row to one. The initialization is
done row by row.

The above statement can be equivalently written as:


int table[2][3]={{0,0,0},{1,1,1}};

Also initializes the two-dimensional array in the form of a matrix as shown below:

int table[2][3]={
{0,0,0},
{1,1,1}
};

3. Write the function of atoi( ) operator .


Or
When atoi( ) function is required?
Ans:

atoi( ) function converts a string of digits into their integer values.


The function takes the form:
x = atoi(string);
x is an integer variable and string is a character array containing a string of digits.

Consider the following segment of the program:


number = “1988”;
year = atoi (number);
number is a string variable which assigned the string constant “1988”. The function atoi
converts the string “1988” to its numeric equivalent 1988 and assigns it to the integer variable
year.

Dr. Abu Nowshed Chy 2


Lecturer, Dept. of CSE, CU
4. What is the limitation of using array? How you can solve it?
Ans:

Limitations of using array:


An array created at compile time by specifying size in the source code has a fixed size and
cannot be modified at run time.

But consider a situation where we want to use an array that can vary greatly in size. We must
guess what will be the largest size ever needed and create the array accordingly. A difficult task
in fact!

This limitation can be overcome in C because in C it is possible to allocate memory to arrays at


run time. This feature is known as dynamic memory allocation and the array created at run time
are called dynamic arrays. Dynamic arrays are created using what are known as pointer variables
and memory management functions malloc, calloc and realloc.

Other limitation:

o Because names of arrays represent just a pointer to the beginning of the array, we have some
limitations or problems.

1. Array do not perform the out of bounds checking. For example:


int ia[2] = {0, 1};
printf("%d ", ia[2])
The above code would segfault, because you are trying to look at an area of memory
not inside the array memory allocation.
2. Array size must be constant or known at compile-time.
3. Arrays cannot be copied or compared because they are pointers.
4. Array index type must be Integral.

o Another limitation comes with arrays being passed into functions. Take for example:

void func(int ia[])


void func(int *ia)

Both are the same declaration. As only the pointer to the array is passed in, not the whole
array. So what if you mistakenly did a sizeof(ia) inside func? Instead of returning the
sizeof the whole array, it would return the size of a pointer which corresponds to the word
size of the computer.

Dr. Abu Nowshed Chy 3


Lecturer, Dept. of CSE, CU
5. What is the limitation of scanf function in case of taking string data?
Or
What is the disadvantage of scanf function in case of taking string input? How will u
solve this problem?
Or
What is the problem of using scanf ( ) in case of taking a line of text? How can u solve
this problem?

Ans:
The limitation of scanf ( ) function in case of taking string data is that %s specifier cannot be
used to read strings with blank spaces i.e.; it can read only the word before first space. But this
problem can be solved with the help of %[ ] specification. Blank spaces may be included within
the brackets, thus enabling the scanf ( ) to read strings with blank spaces.

6. Briefly define the memory representation of one and two dimensional array.
Ans:

Representation of one dimensional / Linear arrays in memory:


Let LA be a linear array in memory of the computers, and memory of computer is simply a
sequence of address location and we use notation:
LOC(LA[K]) = address of element LA[K] of the array LA
because these stores in successive memory cells so only we needs to keep track of the first
element of LA,

Using the the base address of LA (denoted by Base(LA)), computer calculates the address of
elements of LA by following formula:

LOC(LA[K]) = Base (LA) + w(K- lower bound)

where w is the number of words per memory cell for the array LA.

Dr. Abu Nowshed Chy 4


Lecturer, Dept. of CSE, CU
Dr. Abu Nowshed Chy 5
Lecturer, Dept. of CSE, CU

You might also like