0% found this document useful (0 votes)
67 views

Unit-3 Questions and Answeres

Arrays allow storing multiple values of the same type in a single variable name. The key advantages are easy access using indexes and efficient search processes. One-dimensional arrays in C are declared with syntax like data type arrayName[size];. Elements are accessed using arrayName[index] and printed in a loop from 0 to size-1. Two-dimensional arrays are similar but require two indexes, like arrayName[row][column].

Uploaded by

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

Unit-3 Questions and Answeres

Arrays allow storing multiple values of the same type in a single variable name. The key advantages are easy access using indexes and efficient search processes. One-dimensional arrays in C are declared with syntax like data type arrayName[size];. Elements are accessed using arrayName[index] and printed in a loop from 0 to size-1. Two-dimensional arrays are similar but require two indexes, like arrayName[row][column].

Uploaded by

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

Unit-3

1. How to access array elements? Explain.


Sol :-

An Array is a collection of related data items which will share a common name. It is a finite
collection of values of similar data type stored in adjacent memory locations that is accessed
using one common name. Each value of the array is called as an element. Elements are accessed
using index, which is a number representing the position of the element. By finite we mean that,
there are specific number of elements in an array and by similar we mean that, all elements in the
array are of same type.

Declaring an array An array is declared just like an ordinary variable but with the addition of number of
elements that the array should contain. A particular value is indicated by writing a number called 'index or
subscript' number in brackets for after the array name.

Syntax:

Data type variablename[size];

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

E.g. The following declaration declares n array of 20 elements where each element is of int type.

int marks[20];

Array can be of any variable type. The ability to use a single name to represent a collection to refer to an
item by specifying the item number enables us to develop efficient programs.

TYPES: Arrays in C Programming are of 3 types:


(1) One [or] Single Dimensional Array
(2) Two [or] Double Dimensional Array
(3) Multi Dimensional Array

ONE-DIMENSIONAL ARRAY

A list of items can be given one variable name using only one subscript and such a variable is
called a single subscripted variable or one dimensional array.

For Example:

If we want to represent a set of 5 numbers 35, 40, 20, 57, 19 by an array variable 'number' then
we may define the variable number as follows:

int number[5]={ 35, 40, 20, 57, 19};


and computer reverses 5 storage locations are shown below:

number[0]
number[1]
number[2]
number[3]
number[4]

The values of an array element can be assigned as follows:

number[0] = 35;
number[1] = 40;
number[2] = 20;
number[3] = 57;
number[4] = 19;

Access elements of Array

An array contains multiple elements. But the total array is accessed using one name. So to access
array elements you have to use index or subscript and array name. The index is nothing but the
number of the element. It identifies the position of the element in the array.

In C array index starts with 0 and ends at number of elements - 1.

E.g. marks[5] = 30; /*will store value 30 into 6th element*/

Subscripts: Subscripts of an array is an integer expression, integer constant or integer variables


like 'i', 'n' etc. that refers to different elements in the array. Subscripts have a range, starting from
'0' upto the "size of array-1". Subscripts can also be reflected in for loops that print the array
elements. Consider a part of program of for loop

Example:-

#include<stdio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter 5 elements into an array:\n");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
printf("The 5 elements are:\n");
for(i=0;i<=4;i++)
printf("%d\n",a[i]);
}
TWO-DIMENSIONAL ARRAY

A list of items can be given one variable name using two subscripts and such a variable is called
a double subscripted variable or two-dimensional array. So to access twodimension array you
have to have one row index and one column index.
Declaration of Two-dimensional array The general form of Two-dimensional arrays is Syntax:

Data type array_name[row_size][column_size];

Initialization of 2D Array

There are many ways to initialize two Dimensional arrays –

int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };

OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Example :

#include <stdio.h>
main()
{
int a[2][2],i,j;
printf("Enter 4 elements into array: ");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);

}
}

printf("The 4 elements are:\n\t");

for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n\t");
}

2. What is a format string? How do you write format strings for data entry and
display?

A collection of characters is called as string. In the absence of string data type to store strings in
C we have to simulate string using an array of characters. Any group of characters (except
double quote sign) defined between double quotations marks is a constant string.

Character strings are often used to build meaningful and readable programs. The common
operations performed on character strings are:

• Reading and writing strings


• Combining strings together
• Copying one string to another
• Comparing strings for equality
• Extracting a portion of a string

Declaring a String An array of characters is also a collection of characters. So an array of


characters is used to represent a string in C.

For example, to store a name, which may be up to 20 characters, you would declare a string as
follows:

char name[20];

char names[10][20]; /* to store 10 names where each name can contain 20 characters */

Initializing String Variables:

Character arrays may be initialized when they are declared. C permits a character array to be
initialized in either of the following two forms:

char text[9] = "WELCOME"; /

char text[9] = {'W', 'E', 'L', 'C', 'O', 'M', 'E'};

Input/output of Strings

You can read a string from keyboard. C has provided a conversion character %s for inputting
and outputting string. You can read a string using scanf() & print using printf().
The following code snippet is to read name of the user and display the same.

char name[30];
printf("Enter your name: ");
scanf("%s",name);
printf("Welcome %s",name);

In order to read and write string of characters the functions gets() and puts() are used gets()
function reads the string and puts() function takes the string as argument and writes on the screen

gets()

This function reads a sequence of characters entered through the keyword and is useful for
interactive programming. Since a string has no predetermined length gets() needs a way to know
to stop. It reads character until it reaches a new line character. To terminate input at the keyword,
press the ‘Enter’ key.

puts()

The function puts() writes a string argument onto the screen. The puts() can only output a string
of characters. It takes less space and runs faster than printf().

Example:
#include <stdio.h>
void main()
{
char ch[30];
printf("Enter String: ");
gets(ch);
printf("\nEntered String: ");
puts(ch);
}

3. What is the advantage of using arrays? Give syntax for declaration, accessing and
printing one - dimensional array?

An Array is a collection of related data items which will share a common name. It is a finite
collection of values of similar data type stored in adjacent memory locations that is accessed
using one common name. Each value of the array is called as an element. Elements are accessed
using index, which is a number representing the position of the element. By finite we mean that,
there are specific number of elements in an array and by similar we mean that, all elements in the
array are of same type
Below are some advantages of the array:
• In an array, accessing an element is very easy by using the index number.
• The search process can be applied to an array easily.
• 2D Array is used to represent matrices.
• For any reason a user wishes to store multiple values of similar type then the Array can be
used and utilized efficiently.

Disadvantages of Arrays

Array size is fixed: The array is static, which means its size is always fixed. The memory
which is allocated to it cannot be increased or decreased.

Refer Q-No-1

4. Write a program to compare two strings without using existing functions.

5. Explain different types of string handling functions with example

Refer Class Notes

6. Write the syntax for declaring two - dimensional array write a program to access
and print the array elements.

Refer Q.No-1
7. Write a program to find max and min elements from the array.

8. Write a program to count number of characters in a string

9. What is a null character? Write its uses in strings.

In C programming, character strings are terminated by NULL Byte. The representation for
NULL Byte is '\0' or '0' (zero) or NULL. This way we wouldn't be able to use NULL character in
ours' strings. C doesn’t have explicit “string data type” This requires to use some character to
terminate the character arrays so the same could be treated as strings.

The fact is NULL character doesn’t have any graphic printable symbol associated with it and
consequently not required and this is why it’s used as a string terminator.

Recall that character strings and string literals are each terminated by NULL byte. String literal,
however, doesn’t have visible NULL terminator. Compiler provides every string literal a NULL
terminator while compiling the program. But character arrays may or may not contain NUL
terminator. Character arrays which contain terminating NULL byte are strings.

For example:

char season[] = {'a','u','t','u','m'}; /* character array *


char country[] = {'I','n','d','i','a','\0'}; /* character string *
char *str = "Hello, how are you?"; /* string literal: No visible NULL terminator */

The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the
string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows −
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C

10. How does a structure differ from an array?

Both the arrays and structures are classified as structured data types as they provide a mechanism
that enables us to access and manipulate data in a relatively easy manner. But they differ in a
number of ways listed in the table below:

Arrays Structures

1. An array is a collection of related data elements of


1. Structure can have elements of different types
the same type.
2. A structure is a derived and programmer-defined data
2. An array is a derived data type
type
3. But in the case of structure, first, we have to design and
3. Any array behaves like a built-in data types. All we
declare a data structure before the variable of that type are
have to do is to declare an array variable and use it.
declared and used.
4. Array allocates static memory and uses 4. Structures allocate dynamic memory and uses
index/subscript for accessing elements of the (.) or (->)operator for accessing the member of a
array. structure.
5. An array is a pointer to the first element of
5. Structure is not a pointer
it
6. Element access takes relatively less time. 6. Property access takes relatively large time.

11. Write a C program to check whether the given string is palindrome or not?

12. Write C program to read an array of names and to sort them in alphabetical order
13. Discuss difference between structure and unions

Refer Class Notes

14. Program for Matrix addition

15. Program for Matrix Multiplications


16. Write a C program read any string and print in reverse without using string
handling functions

17. Write a C program to read and print a 3 by 3 matrix ?


18. Explain the Command Line arguments ?

It is possible to pass some values from the command line to your C programs when they are
executed. These values are called command line arguments and many times they are important
for your program especially when you want to control your program from outside instead of hard
coding those values inside the code.

The command line arguments are handled using main() function arguments where argc refers to
the number of arguments passed, and argv[] is a pointer array which points to each argument
passed to the program.

int main(int argc, char *argv[]) { /* ... */ }


or
int main(int argc, char **argv) { /* ... */ }
• argc (ARGument Count) is int and stores number of command-line arguments passed by
the user including the name of the program. So if we pass a value to a program, value of
argc would be 2 (one for argument and one for program name)
• The value of argc should be non negative.
• argv(ARGument Vector) is array of character pointers listing all the arguments.
• If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain
pointers to strings.
• Argv[0] is the name of the program , After that till argv[argc-1] every element is command
-line arguments.

Example:-
#include <stdio.h>
int main(int argc, char *argv)
{
printf( "You have entered %d arguments:" , argc);

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


printf(“%s \n”,argv[i] );

You might also like