Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: arrays and string Class notes.
Description: This is class notes and very very helpfull for you.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Unit 4
4
...

 A one-dimensional array is like a list
...

4
...
1 Declaring Arrays
 Array variables are declared identically to variables of their data type, except that the
variable name is followed by one pair of square [ ] brackets for each dimension of the
array
...
listed within
the square brackets
...

Examples:
int i_Array[ 10 ];
float f_Array[ 1000 ];
4
...
2 Initializing Arrays
 Arrays may be initialized when they are declared, just as any other variables
...
Note the use of
commas in the examples below
...
The remaining array elements will be automatically initialized to zero
...
The
compiler will automatically size the array to fit the initialized data
...
)
 Examples:
int i_Array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
float f_Array[ 100 ] = { 1
...
0, 20
...
1
...

 Array subscripts must be of integer type
...
For example, a five element array will have indices zero through four
...
( The first
element is at the beginning of the array, and hence has zero offset
...
Fibonacci numbers are used to determine the sample points used in certain
optimization methods
...
*/
#include ...
h>
int main( void )
{
int i, fibonacci[ 20 ];
fibonacci[ 0 ] = 0;
fibonacci[ 1 ] = 1;
for( i = 2; i < 20; i++ )
fibonacci[ i ] = fibonacci[ i - 2 ] + fibonacci[ i - 1 ];
for( i = 0; i < 20; i++ )
printf( "Fibonacci[ %d ] = %f\n", i, fibonacci[ i ] );
} /* End of sample program to calculate Fibonacci numbers */
4
...

 One dimensional arrays do not require the dimension to be given if the array is to be
completely initialized
...
All dimensions after the
first must be given in any case
...

 Two dimensional arrays are considered by C/C++ to be an array of (single dimensional
arrays)
...

 C stores two dimensional arrays by rows, with all elements of a row being stored together
as a single unit
...

Sample Program Using 2-D Arrays
/* Sample program Using 2-D Arrays */
#include ...
h>
int main( void ) {
/* Program to add two multidimensional arrays */

int a[ 2 ][ 3 ] = { { 5, 6, 7 }, { 10, 20, 30 } };
int b[ 2 ][ 3 ] = { { 1, 2, 3 }, { 3, 2, 1 } };
int sum[ 2 ][ 3 ], row, column;
/* First the addition */
for( row = 0; row < 2; row++ )
for( column = 0; column < 3; column++ )
sum[ row ][ column ] =
a[ row ][ column ] + b[ row ][ column ];
/* Then print the results */
printf( "The sum is: \n\n" );
for( row = 0; row < 2; row++ ) {
for( column = 0; column < 3; column++ )
printf( "\t%d", sum[ row ][ column ] );
printf( '\n' ); /* at end of each row */
}
return 0;
}
4
...

• Union is a special data type available in C that allows to store different data types in the
same memory location
...

4
...
1 Structure: A structure is a collection of different variables referenced under one name,
providing a convenient means of keeping related information together
...
The
variables that make up the structure are called members
...
) The following code fragment shows how to declare a structure
that defines the name and address fields
...

struct addr
{
char name[30];
char street[40];
char city[20];
unsigned long int zip;
};
Notice that the declaration is terminated by a semicolon
...
Also, the structure tag addr identifies this particular data structure and is its type

specifier
...
Only the form of the data has been
defined
...
Not until you
declare a variable of that type does one actually exist
...
Thus, addr describes the form of a
structure (its type), and addr_info is an instance (an object) of the structure
...
The general form of a structure declaration is
struct tag {
type member-name;
type member-name;
type member-name;
} structure-variables;
 Accessing Structure Members: Individual members of a structure are accessed through
the use of the
...
For example, the following
statement assigns the ZIP code 12345 to the zip field of the structure variable addr_info
declared earlier:
addr_info
...
member-name
Therefore, to print the ZIP code on the screen, write
printf("%lu", addr_info
...
name can be used in a call to gets( ),
as shown here:
gets(addr_info
...
You do not need
to assign the value of each member separately
...
h>
int main(void)
{
struct {
int a;
int b;
} x, y;
x
...
a);
return 0;
}

After the assignment, y
...

 Arrays of Structures: To declare an array of structures, you must first define a structure
and then declare an array variable of that type
...
To
access a specific structure, index the array name
...
zip);
Like all array variables, arrays of structures begin indexing at 0
...
When you want to index a specific element of a structure, index the
element
...

addr_list[2]
...
It is irrelevant that the value is obtained from a member of a
structure
...
x); /* passes character value of x */
func2(mike
...
z); /* passes float value of z */
func4(mike
...
s[2]); /* passes character value of s[2] */
In each case, it is the value of a specific element that is passed to the function
...

If you wish to pass the address of an individual structure member, put the & operator
before the
structure name
...
x); /* passes address of character x */
func2(&mike
...
z); /* passes address of float z */
func4(mike
...
s[2]); /* passes address of character s[2] */
Note that the & operator precedes the structure name, not the individual member name
...
Of course,
this means that any changes made to the contents of the parameter inside the function do
not affect the structure passed as the argument
...
For example, in the following program both the
argument arg and the parameter parm are declared as the same type of structure
...
h>
/* Define a structure type
...
a = 1000;
f1(arg);
return 0;
}
void f1(struct struct_type parm)
{
printf(''%d", parm
...
For example, had struct_type been declared inside main( ), it would not have
been visible to f1( )
...
For
example, the structure address is nested inside emp in this example:
struct emp {
struct addr address; /* nested structure */
float wage;
} worker;
Here, structure emp has been defined as having two members
...
The other is wage, which holds the
employee's wage
...

worker
...
zip = 93456;

4
...
Declaring a union is similar to
declaring a structure
...


...

} union-variables;
For example:
union u_type {
int i;
char ch;
};
This declaration does not create any variables
...
To declare a
union variable called cnvt of type u_type using the definition just given, write
union u_type cnvt;
In cnvt, both integer i and character ch share the same memory location
...

When a union variable is declared, the compiler automatically allocates enough storage to hold
the largest member of the union
...

To access a member of a union, use the same syntax that you would use for structures: the dot
and arrow operators
...
If the union
is accessed through a pointer, use the arrow operator
...
i = 10;
Difference between struct and union
...
Each member has its own storage location
...
Memory occupied by this type of variable will be
equal to the sum of memory occupied by the
individual members of the structure
...

This type of variable will have memory equal to the
memory occupied by the member(having largest
memory )
union student
{
int rollno;
char name[40];
};

union student stud1;
memory of variable stud1=40 bytes

4
...

Enumerations are common in everyday life
...

Enumerations are defined much like structures; the keyword enum signals the start of an
enumeration type
...
Value n};

e
...
enum coin { penny, nickel, dime, quarter, half_dollar, dollar};
The enumeration identifier name can be used to declare variables of its type
...
\n");
The key point to understand about an enumeration is that each of the symbols stands for an
integer value
...
Each symbol is
given a value one greater than the symbol that precedes it
...
Therefore,
printf("%d %d", penny, dime);
displays 0 2 on the screen
...
Do this by
following the symbol with an equal sign and an integer value
...
For example, the following code
assigns the value of 100 to quarter:
enum coin { penny, nickel, dime, quarter=100, half_dollar, dollar};
Now, the values of these symbols are
penny 0
nickel 1
dime 2
quarter 100
half_dollar 101
dollar 102
4
...
Character arrays are many a time also called strings
...
For
example,
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;

Each character in the array occupies one byte of memory and the last character is always ‘\0’
...
A string not terminated by a ‘\0’ is not really a string, but merely
a collection of characters
...

Klinsman
Can we write the while loop without using the final value 7? We can; because we know that each
character array always ends with a ‘\0’
...

main( )
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}
And here is the output
...

main( )
{
char name[ ] = "Klinsman" ;
char *ptr ;
ptr = name ; /* store base address of string */
while ( *ptr != `\0' )
{
printf ( "%c", *ptr ) ;
ptr++ ;
}
}
The %s used in printf( ) is a format specification for printing out a string
...

main( )

{
char name[25] ;
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ;
printf ( "Hello %s!", name ) ;
}
And here is a sample run of the program
...
Once enter is hit, scanf( ) places a ‘\0’ in the array
...
This is
because the C compiler doesn’t perform bounds checking on character arrays
...

b) scanf( ) is not capable of receiving multi-word strings
...
The way to get around this limitation is by using the function
gets()
...

main( )
{
char name[25] ;
printf ( "Enter your full name " ) ;
gets ( name ) ;
puts ( "Hello!" ) ;
puts ( name ) ;
}
And here is the output
...

4
...
We may either store it in a string or we may ask the C
compiler to store it at some location in memory and assign the address of the string in a char
pointer
...
For example, we cannot assign a string
to another, whereas, we can assign a char pointer to another char pointer
...

main( )

{
char str1[ ] = "Hello" ;
char str2[10] ;
char *s = "Good Morning" ;
char *q ;
str2 = str1 ; /* error */
q = s ; /* works */
}
Also, once a string has been defined it cannot be initialized to another set of characters
...


main( )
{
char str1[ ] = "Hello" ;
char *p = "Hello" ;
str1 = "Bye" ; /* error */
p = "Bye" ; /* works */
}
4
...
Figure
9
...

Function Use
strlen
Finds length of a string
strlwr
Converts a string to lowercase
strupr
Converts a string to uppercase
strcat
Appends one string at the end of another
strncat
Appends first n characters of a string at the end of another
strcpy
Copies a string into another
strncpy
Copies first n characters of one string into another
strcmp
Compares two strings
strncmp
Compares first n characters of two strings
strcmpi
Compares two strings without regard to case ("i" denotes that this function
ignores case)
stricmp
Compares two strings without regard to case (identical to strcmpi)
strnicmp
Compares first n characters of two strings without regard to case
strdup
Duplicates a string
strchr
Finds first occurrence of a given character in a string
strrchr
Finds last occurrence of a given character in a string
strstr
Finds first occurrence of a given string in another string
strset
Sets all characters of string to a given character
strnset
Sets first n characters of a string to a given character
strrev
Reverses string

Out of the above list we shall discuss the functions strlen( ), strcpy( ), strcat( ) and strcmp( ),
since these are the most commonly used functions
...
Its usage is illustrated in
the following program
...

string = Bamboozled length = 10
string = Humpty Dumpty length = 13
Note that in the first call to the function strlen( ), we are passing the base address of the
string, and the function in turn returns the length of the string
...
Even in the second call,
len2 = strlen ( "Humpty Dumpty" ) ;
what gets passed to strlen( ) is the address of the string and not the string itself
...

/* A look-alike of the function strlen( ) */
main( )
{
char arr[ ] = "Bamboozled" ;
int len1, len2 ;
len1 = xstrlen ( arr ) ;
len2 = xstrlen ( "Humpty Dumpty" ) ;
printf ( "\nstring = %s length = %d", arr, len1 ) ;
printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ;
}
xstrlen ( char *s )
{
int length = 0 ;
while ( *s != '\0' )
{
length++ ;
s++ ;
}

return ( length ) ;
}
The output would be
...
The base addresses of the source
and target strings should be supplied to this function
...

main( )
{
char source[ ] = "Sayonara" ;
char target[20] ;
strcpy ( target, source ) ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
}
And here is the output
...

source string = Sayonara

target string = Sayonara
Note that having copied the entire source string into the target string, it is necessary to place
a ‘\0’ into the target string, to mark its end
...
For example,
“Bombay” and “Nagpur” on concatenation would result into a string “BombayNagpur”
...

main( )
{
char source[ ] = "Folks!" ;
char target[30] = "Hello" ;
strcat ( target, source ) ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
}
And here is the output
...

The two strings are compared character by character until there is a mismatch or end of one
of the strings is reached, whichever occurs first
...
If they’re not, it returns the numeric difference between the ASCII
values of the first non-matching pairs of characters
...

main( )
{
char string1[ ] = "Jerry" ;
char string2[ ] = "Ferry" ;
int i, j, k ;
i = strcmp ( string1, "Jerry" ) ;
j = strcmp ( string1, string2 ) ;
k = strcmp ( string1, "Jerry boy" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
}
And here is the output
...
In the second call, the first character of “Jerry” doesn't
match with the first character of “Ferry” and the result is 4, which is the numeric difference
between ASCII value of ‘J’ and ASCII value of ‘F’
...
The value returned is -32, which is the value of null
character minus the ASCII value of space, i
...
, ‘\0’ minus ‘ ’, which is equal to -32
...
9 Two-Dimensional Array of Characters/ Array of Strings
In Array of Strings each row is used to hold a separate string
...
When you do so, it checks your name against
a master list to see if you are worthy of entry to the palace
...

#define FOUND 1
#define NOTFOUND 0
main( )
{
char masterlist[6][10] = {
"akshay",
"parag",
"raman",
"srinivas",
"gopal",
"rajesh"
};
int i, flag, a ;
char yourname[10] ;
printf ( "\nEnter your name " ) ;
scanf ( "%s", yourname ) ;
flag = NOTFOUND ;
for ( i = 0 ; i <= 5 ; i++ )
{
a = strcmp ( &masterlist[i][0], yourname ) ;
if ( a == 0 )
{
printf ( "Welcome, you can enter the palace" ) ;
flag = FOUND ;
break ;
}
}
if ( flag == NOTFOUND )
printf ( "Sorry, you are a trespasser" ) ;
}
And here is the output for two sample runs of this program
...
The first subscript gives the
number of names in the array, while the second subscript gives the length of each item in the
array
...

for ( i = 0 ; i <= 5 ; i++ )
scanf ( "%s", &masterlist[i][0] ) ;

Questions
From 2011-12
1
...

ii) Sum of all the elements of the matrix
...
Write algorithm and function program to sort an array of integer into descending order,
where the size of array is input by user
...
Define a structure called cricket that will describe the following information:
player name
team name
batting average
Using cricket, declare an array player with 50 elements and write a program to read the
information about all the 50 players and print a team-wise list containing names of
players with their batting average
...
Write a program which will read a string and rewrite it in the alphabetical order
...

From 2012-13
1
...

2
...
Write a program to multiply two N×N
matrix
...
Discuss String handling function
...
Write a program in C that accepts the Rollno and name of 60 students in a class along
with their marks in Physics,
5
...
Print the roll no
...

6
...
Use this structure to store the data in a file named “employee
...
Once all
the records are entered display the employee details stored in a file
...
Write a program to create an integer array of n elements, pass this array as an argument to
a function where it is sorted and displayed
...
Two matrices of real numbers of size 4 × 4 is given
...

9
...

Also write a program in C to enter the records of 200 employees and display the names of
those who earn greater than Rs 50,000
...

10
...
Write a program in C language to store these marks in an array and
calculate the average mark obtained by all the students and then display the deviation of
mark for each student from average
...
What do you mean by sorting
...


From 2013-14
1
...

2
...
Write a program in ‘C’ to find the largest
element of a 3 × 3 matrix
...
Define Union
...
Each record has roll no, name, class and marks fields
...
Write a program in ‘C’ to multiply the two matrices of M × N
...
Write a C program to find the multiplication of two matrices
...
How to declare an array? Explain about various operations of an array
...
What is enumerated data type? Write a C program to display months in the year using
union
...
Differentiate structure and union in C
...

15-16(Even)
What are the principles of recursion? Explain in detail
...
-5 Marks
15-16(Odd)
Write difference between structure and union
...
) operator in c language with proper example
...
-5
16-17(Odd)
Write short notes on Union and enumerated data type
...
-5
What is sorting? Write a program to sort a list of n positive integers
Title: arrays and string Class notes.
Description: This is class notes and very very helpfull for you.