UNIT-2 (1)

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

UNIT-II

Introduction

When large amount of similar data has to be stored in memory and this data has to be referenced in uniform manner,
some means of representation is required to store and manipulate data efficiently. If we use ordinary variables to
hold each data item, handling of such data will be confusing and lengthy. Hence, we go for arrays which makes this
entire task easier.

One-dimensional array:

It is the simplest form of array in which list of elements are stored in contiguous memory locations and accessed
using only one subscript.

Example:
int abc[10];
here abc is an array of 10 integers.
Declaration of one-dimensional array:
The general format of declaring arrays is as follows:
type array-name [size];
Where, type is the data type of array elements, array-name is the name of array variable and size specifies the
number of elements in the array. The size of array is fixed at compile time and it cannot be changed during
execution of program hence, it is a constant integer value.
e.g. int a[50];

Two-dimensional arrays

When the data must be stored in the form of a matrix we use two-dimensional arrays. For example,
int a[5] [3];
represents a two-dimensional array consisting of 5 rows and 3 columns. So the total number of elements which can
be stored in this array are 5 X 3 i.e.15.
Declaration of two-dimensional array:
As shown in the above example, the general format of declaring a two-dimensional array is:
type array-name[row-size][column-size];
Where type represents the data type of array elements. array-name is name of the array, row-size represents the
number of rows in array and column-size represents number of columns of the array. Both row-size and column size
must be integer constants.

Prepared by : M V B REDDY 20
e.g :
int xyz[2][5];
Here, xyz is an array consisting of two rows with each row consisting of 5 integer elements.
float a[5][5];
Here, a is an array consisting of 5 rows with each row consisting of 5 floating point numbers.

Multi-dimensional arrays:

A three dimensional array can be thought of an array of arrays. It is a collection of two-dimensional arrays.
Representation of 3-D array
nd
2 2-D array
2 1 3

1st 2-D array


1 2 4
5

0th 2-D array 4 5 6


6 8

7 10 12
Strings :
A string is an array of characters. Any array of characters defined between double quotation marks is a constant
string.
e.g
“I love my country” is a constant string
Declaration of string variables:
The general format of declaring string variable is as follows:
char string-name [size]

 ‘char’ represents the data type


 ‘string-name’ represents the name of the string variable.
 ‘size’ represents the number of characters in the string.

Example :
Char name[20];
The string ‘name’ consists of 20 characters. When the compiler assigns a character string to a character array, it
automatically supplies a null character ‘\0’ at the end of the string.

Prepared by : M V B REDDY 21
Operations on strings
The various operations that can be performed on strings are:
 Reading and writing strings
 Combining strings together
 Copying strings (one string to another)
 Comparing strings for equality
 Extracting a portion of a string

String handling functions

The C library provides a large number of string handling functions that can be used for performing string
manipulations. These functions are available in the file string.h. This header file is used or included whenever these
functions are used.

The following are the commonly used string-handling functions:


strcat ( ) : concatenates two strings
strcmp ( ) : compares two strings
strcpy ( ) : copies one string into another
strlen ( ) : Finds the length of a string
strcmpi ( ) : compares two strings without case sensitivity
strncmp ( ) : compares two portions of substrings upto specified position
strncmpi ( ) : compares two portions of substrings upto specified length without case sensitivity.

Table of strings

Till now we have seen how to read and manipulate individual strings. Now we will see how to declare and use array
to strings.

A number of strings can be stored in a two-dimensional array declared as :


Char string_name [size 1] [size 2];
Where size 1 indicates the number of strings stored in the array and size 2 indicates the maximum size of each string
(i.e. maximum characters in each string. )
E.g
Consider the string declared below:
Char str[5][10]={ “Hello”,
“world”,
“CSE”,

Prepared by : M V B REDDY 22
“students”,
“I year “};
Functions
A function is a self contained program segment that performs specific and well defined task. The C language
provides two types of functions: Library functions and user defined functions.

Library functions

Library functions are predefined functions which are mostly provided to perform common computations such as
sin(x), cos(x), pow(x,i)(to compute xi), sqrt(x) etc. the code of these functions is not available to the user so they
cannot modify these functions.

User defined functions

These functions allow user to define their own functions according to the requirement of program.

Any C function is defined in the following formats.

Format : return_type func_name (arg;ument_list)

Argument declaration

local variable;

executable _smt 1;

executable_stmt 2;

_______________

_____________

return (expression); or return;

Function call and function definition

The statement which is used to call a function is known as function call and the actual code of a function is known
as function definition.

Prepared by : M V B REDDY 23
E.g.

void main( )

void example ( ); //function declaration

example( ); // function call

void example ( ) // function definition for example

printf(“this is example program”);

Calling function and called function

The function from which another function is called is known as calling function and this another function is known
as called function.

As shown in the program below main( ) is calling function and mul(a,b) is called function.

Function declaration

If a function call is made before its definition then its prototype must be given. In this example mul(a,b); is called
before its definition, so its prototype is given in function main( ). Otherwise this prototype need not be given.

Arguments used in functions

Depending on the location of argument in functions, they are categorized as actual parameters and formal
parameters.

Actual parameters

The parameters which are passed in function call are know as actual parameters. They are defined in calling
functions and they can be expressions, constants or variables.

Prepared by : M V B REDDY 24
Types of functions

Depending on the return value and argument passed, functions can be classified into 4 categories.
Functions with no arguments and no return values:
These are the functions in which no parameters are passed from calling function to called function and no values are
returned from called function to calling function.

Functions with arguments and no return values

These are the functions in which arguments are passed from calling function to called functions but no values are
returned from called function to calling function.

Function with return values and no arguments

In this no arguments are passed from calling function to called function but values are returned from called function
to calling function.

Function with arguments and return values:

These are the functions in which parameters are passed from calling function to called function and values are
returned from called function to calling function.

Parameter passing techniques

These are two ways of passing parameters to a function


1) Call by value
2) Call by reference

Call by value

When an argument is passed by value, the data in the actual parameters is copied to the formal parameters. Any
modifications done to formal parameters will not be reflected back to actual parameters.

Call by reference :
This is also known as call by address. In this mechanism, the address of actual parameters are passed to formal
parameters. So any changes made to the formal parameters causes change in actual parameters also.

Prepared by : M V B REDDY 25
Passing array as arguments

Array can be passed as arguments. There are two ways of passing arrays as parameters

 Passing each element of array as arguments.

 Passing entire array as argument

 Passing two-dimensional array as argument

Nesting of functions

The nesting of functions is permitted in C. As main ( ) is a function which calls another function 1 which inturn calls
fucntion2 which inturn calls function 3 and so on.

Recursion

Recursion is a name given to a technique of defining a function or a process in terms of itself.

In the recursive procedure given above, M is the main procedure from which recursive function is called.
Two conditions must be satisfied by a recursive function.
a) A recursive function must always call a simple case of itself.
b) There should be a terminating condition otherwise it will go into infinite loop

Storage classes
When you declare a variable, it is associated with some data type. Each declared variable is associated with some set
rules which determine where the variable can be used., when it can be sued and where it is stored. These features are
defined using the following terms:
Storage :

Prepared by : M V B REDDY 26
Any variable declare in a program can be stored either in memory or registers. Registers are small amounts of
storage in CPU. The data stored in registers has faster access compared to data stored in memory normally,
compilers determine what data is to be stored in registers of CPU.
Scope:
Scope of a variable is the part of program in which it can be used. The different scopes of variables can be :
 Function’
 Block
 File

Storage class
The storage classes are used to control storage, scope and lifetime of variable. Different storage classes available in
C language are
 Automatic
 External
 Static
 Register

Prepared by : M V B REDDY 27
QUIZ QUESTIONS

1. int a[5]={1,2,3};
The value of a [4] will be
a) 2 b) 3 c) garbage value d) 0

2. int a[ ]={‘a’,’b’,’c’,’d’}
size of array a is
a) 4 b) 5 c) cannot be determined d)error

3. char a[ ] = {a, b,c,d}


size of array b is
a) 4 b) 5 c) cannot be determined d)none

4. When a string is read using getchar( ) function it


a) should be explicitly terminated with ‘\0’
b) is automatically terminated with ‘\0’
c) need not be terminated with ‘\0’
d) none
5. The size of this array is
a) 20 bytes b) 10 bytes c) 22 bytes d) none

6. char s[20] =”welcome”;


char p[ ]= “world”;
strcat(s,p);
what will be the value of s ?
a) welcome world b) world welcome
c) error due to insufficient size d) welcome

7. char S[ ] =”welcome”;
char P[ ] =”world”;
strcat(S,P);
what will be the value of S?
a) welcome world b) world welcome
c) error due to insufficient size d) none

8. int a[3] [2] = { {1,2}, {5},{7});

Prepared by : M V B REDDY 28
The value a[1][1] is
a) 1 b) 5 c) 2 d) garbage value

9. char str1[ ] = “computer “;


char str2 [ ] = “science”;
char str3[50];
str3=str1+ str2;
value of str3 is
a) computer b) science c) 2 d) invalid statement

By the given data solve 10 & 11


Char str1[ ] =”cse”;
Cahr str2[ ] =”dept”;

10. i = strcmp(strl, str2);


The value of i is
a) 0 b) 1 c) -1 d) none
11. i = strcmp(str2. strl); The value of i is
a) 0 bM c) -1 d) none

12. char S1[ ]= "COMPUTER";


char S2[ ] = "Computer";
i =strcmp(Sl, S2);
The value of i is
a) 0 b) 1 c) -1 d) none
13. char S1[ ]= "COMPUTER";
char S2[ ] = "Computer";
i = strcmpi(Sl, S2);
The value of i is
a) 0 b) 1 c) -1 d) none

14. char Sl[ ] = "Computer";


char S2[ ]= "Compass";
i = strcmp(Sl, S2);
The value of i is
a) 0 b) 20 c) 20 d) none

Prepared by : M V B REDDY 29
15. A character array CSE can be initialized as ;
a) char S{ ] =-- "CSE"
b) char S[ ] = {'C'. 'S', 'E'}
c) char S[ ]= { 'C,’S ‘,'E ', ‘\0’)
d) both a and c are correct

16. char S[6] - "Hello";


for (i = 0; S[i]! = ‘\0’, i++);
printf ("%d". i);
The output will be
a) 012345 b) 0 1 2 3 4 5 6 c) 5 d) none
17. By default, storage class for any variables declared in a block is
a) auto b) extern c) register d) static
18. Before an auto variable is initialized, it will have
a) 0 b) garbage value c) no value d) none
19. Automatic variables are stored in
a) memory b) register c) cache d) none
20. For faster access of variables, they must be declared as
a) register b) auto c) static d) extern
21. If no registers are available for storage, register variables are same as
a) auto. b) static c) extern d) none

22. External variables can be accessed by


a) only in which they arc declared
b) all the functions declared in the program
c) functions which are defined after the declaration of external variable
d) none
23. void main ( )
{ …………..}
int a;
demo1( ) { }
demo2( ) { }

To which function a is accessible?


a) only to demo 1( ) b) only to demo2( )
c) only to main( ) d) to both demo 1( ) and demo2( )

Prepared by : M V B REDDY 30
24. Storage class controls
a) life time of variable b) scope of variable
c) storage of variable d) all the above
25. void main ( )
{
if (……)
{
int i,j=10;
printf("%3d",j );
printf ("%3d%3d", i,j);
}
The output will be
a) 5 10 5 b) 5 10 10 c) error d) none

26. void main( )


{
Demo( );
Demo( );
}
Demo( )
{
static int i;
printf(“%3d",i);
i++;
}
The output will be
a) 01 b) 00 c) 1 2 d) none
27. The initial value of any static variable is :
a) 0 b) 1 c) depends on external initialization d) none

28. Which storage class refers to global variables?


a) auto b) static c) extern d) registers

29. Which of the following is correct return statement?


a) return(2 + 10); b) return(a);
c) return(a, b); d) b and c

Prepared by : M V B REDDY 31
30. A static variable is one which
a) retains its value throughout the life of program
b) is same as automatic variable
c) cannot be initialized
d) none
31. Static variables are stored in
a) memory b) register c) both a and b d) none

32. int a[3]= { 10, 15, 20}, x;


x = a++; The value of x is a) 10 b) 15 c) error d) 20
33. int a[ ]= { 1,2, 3};
a[-l]= 10;
Is this valid statement?
a) not a valid statement
b) may not be a valid statement
c) it is a valid statement
d) none of the above
34. Array is collection of items of
a) different data types b) same data type c) both a and b d) none
35. Array elements are stored in
a) contiguous memory locations
b) non contiguous memory locations
c) both a and b
d) none
36. A character array ends with
a) "\0" b) '#' c) '@' d) blank space
37. char S[ 10] = "program";
S[4] = ‘\0’;
puts(S);
The output will be
a) pro b) prog c) program d) gram

38. void main()


{
int j = 5, k, i;

Prepared by : M V B REDDY 32
k = i +j;
pnntf("0/od", k);
The output will be
a) compiler error h) run time error c) 15 d) none
39. void main( )
{
auto int a = 50;
register char b = 'a';
b = b + 5;
printf("% c", b);
}
The output will be
a) f b) a c) error d) none
40. The parameters used in function call are known as
a) formal parameters b) actual parameters
c) user defined parameters d) all the above

41. Which of the following statements are true9 Function


a) reduces the space occupied but increases computation time
b) increases space occupied and decreases computation time
c) reduces space as well as computation time
d) none of the above

Prepared by : M V B REDDY 33
PREVIOUS QUESTIONS

1. In what way array is different from an ordinary variable?


2.. What conditions must be satisfied by the entire elements of any given array?
3. What are subscripts? How are they written? What restrictions apply to the values that can be assigned
to subscripts?
4. What advantage is there in defining an array size in terms of a symbolic constant rather than a fixed
integer quantity?
5. Write a program to find the largest element in an array.
6. Write a program to sort the set of strings in an alphabetical order.
7. How are multidimensional arrays defined? Compare with the manner in which one-dimensional arrays
are defined.
8. Write a C program to do Matrix Multiplications.
9. Write in detail about one dimensional and multidimensional arrays. Also write about how initial
values can be specified for each type of array.
10. What do you mean by functions? Give the structure of the functions and
explain about the arguments and their return values.
11. Write a C program that uses a function to sort an array of integers.
12. Distinguish between getchar and scanf functions for reading strings.
13. Write a program to count the number of words, lines and characters in a text.
14. How are initial values written in a one-dimensional array definition? Must the entire array be
initialized? What value is automatically assigned to those array elements not explicitly initialized?
15. Write a program to calculate mean, variance and standard deviation of n numbers.
16. The annual examination is conducted for 50 students for three subjects. Write
a program to read the data and determine the following:
(i) Total marks obtained by each student.
(ii) The highest marks in each subject and the Roll Number of the student who secured it.
(iii) The student who obtained the highest total marks.
17. Explain how strings can be stored using a multidimensional arrays.
18. What are the string built-in functions available? Write in detail about each one of them with an
example.
19. The names of employees of an organization are stored in three arrays, namely, first name, second
name and last name. Write a program to concatenate the three parts into one string to be called name.
20.Write in detail about different storage classes with an example.

Prepared by : M V B REDDY 34
21. Write a function that will round a floating-point number to an indicated decimal place. For e.g., the
number 17.457 would yield the value 17.46 when it is rounded-off to two decimal places.
22, Distinguish between the following: Actual and formal arguments.
23. Distinguish between the following: Global and local variables
24. Distinguish between the following: Automatic and static variables.
25. Explain in detail about pass by values and pass by reference. Explain with a sample program.
26.What do you mean by functions. Give the structure of the functions and explain about the arguments
and their return values.
27. Write a C Program that uses a function to sort an array of integers.

Prepared by : M V B REDDY 35

You might also like