27/march/2021
Things you cannot do
ā¢You cannot
- use ā=ā to assign one array variable to another:
a = b; /* a and b are arrays */
-use ā==ā to directly compare array variables:
if (a==b) .ā¦ā¦ā¦ . .
- directly scanf or printf arrays:
printf (āā¦ā¦ā, a) ;
- The size may be omitted. In such cases, the compiler automatically allocates enough space for all initialized elements.
int flag [] = {1, 1, 1,0} ;
char name [] = {āAā, āmā, āiā , ātā } ;
How to copy the elements of one array to another?
ā¢ By copying individual elements:
How to read the elements of an array?
ā¢ The elements can be entered all in one line or different lines.
How the print the elements of an array?
ā¢ By printing them one element at a time.
ā¢ The elements are printed one per line.
Character string
Introduction
ā¢ A string is an array of characters.
-Individual characters are stored in memory in ASCII code.
- A string is represented as a sequence of characters terminated by the null (ā\0ā) character.
Declaring string variable
ā¢ A string is declared like any other array:
char string-name [size] ;
-size determines the number of characters in
string_name.
ā¢ When a character string is assigned to a character
array, it automatically appends the null character
(ā\0ā) at the end of the string.
-Size should be equal to the number of characters
in the string plus one.
Examples
char name [30] ;
char city [15];
char dob [11];
ā¢ A string may be initialized at the time of
declaration.
Reading āwordsā
ā¢ scanf can be used with the ā%sā format specification.
Char name [30] ;
:
:
Scanf (ā%sā, name) ;
- The ampersand (&) is not required before the variable name with ā%sā.
- The problem here is that the string is taken to be up to the first white space (blank, tab, carriage return, etc.)
- If we type āRupak Biswasā
- Name will be assigned the string āRupakā
Reading a āline of textā
ā¢ In many applications, we need to read in the entire line of text (including blank spaces).
ā¢ We can use the getchar() function for the purpose.
Writing strings to the screen
ā¢ We can use printf with the ā%sā format
specification.
Processing character strings
ā¢ There exists a set of C library functions for
character string manipulation.
- strcpy :: string copy
- strlen :: string length
- strcmp :: string comparison
ā¢ It is required to add the line
#include <string.h>
Writing string to the screen
ā¢ We can use printf with the ā%sā format
specification.
25/march/2021
Basic concept
ā¢ Many applications require multiple data items
that have common characteristics
ā¢ In mathematics, we often express such groups of
data items in indexed form:
x1,x2,x3ā¦..,xn
ā¢ Why are arrays essential for some applications?
- Take an example.
- Finding the minimum of a set of numbers.
Using Arrays
ā¢ All the data items constituting the group share
the same name.
Int x[10];
ā¢ Individual elements are accessed by specifying
the index.
Declaring Arrays
ā¢ Like variables, the arrays that are used in a
program must be declared below they are used.
ā¢ General syntax:
type array-name [size];
- type specifies the type of element that will be
contained in the array (int, float, char, etc.).
- size is an integer constant that indicates the
maximum number of elements that can be
stored.
ā¢ Example
int x[10];
char line[80];
float points[150];
char name [35];
ā¢ If we are not sure of the exact size of the array, we
can define an array of a large size.
int marks [50];
char line [80];
float points[150];
char name[35];
ā¢ If we are not sure of the exact size of the array,
we can define an array of a large size.
int marks[50];
though in a particular run we may only be using
say, 10 elements.
ā¢ Starting from a given memory location, the
successive array elements are allocated space in
consecutive memory locations.
Array a
x x+k x+2k
X: starting address of the array in memory
K: number of bytes allocated per array element
ā¢ element a[i]:: allocated memory at the address
x+ i*k
ā¢ First array index assumed to be at zero.
Accessing Array Elements
ā¢ A particular of the array can be accessed by
specifying two things:
- Name the array.
- Index (relative position) of the element in the
array.
ā¢ In C, the index of an array starts from zero.
ā¢ Example:
- An array is defined as int x[10];
- The first element of the array x can be
accessed as x[0], the fourth element as x[3],
tenth element as x[9], etc.
Contd.
ā¢ The array index must evaluate to an
integer between 0 and n-1 where n is the
number of elements in the array.
a[x+2] = 25;
b[3*x-y] = a[10-x]+5;
A Warning
ā¢ In C, while accessing array elements,
array bounds are not checked.
ā¢ Example:
int marks[5];
ā¢ Example:
int marks[5];
:
:
marks[8] = 75
- The above assignment would not necessarily
cause an error.
- Rather, it may result in unpredictable
program results.
Initialization of Arrays
ā¢ General form:
type array_name [size] = { list of values } ;
ā¢ Examples:
int marks [5] = {72, 83, 65, 80, 76};
char name [4]= {āAā, āmā, āiā, ātā };
ā¢ Some special cases:
- if the number of values in the list is less than
the number of elements, the remaining
elements are automatically set to zero.
float total [5] = {24.2, -12.5, 35.1};
total [0] = 24.2, total [1] =-12.5, total [2] =
35.1,
total [3] = 0, total [4] = 0
Contd.
-The size may be omitted. In such cases, the
compiler automatically allocates enough
space for all initialized elements.
Int flag [ ] = {1, 1,1,0};
char name[ ] = { āAā, āmā, āiā, ātā, };
Example 1: Find the minimum of a set of 10
numbers
Alternate
Version 1
# include < stdio.h >
# include < conio.h>
Main (void )
{
int a,b,c;
printf (āsum-1 \n, sub-2\n, multi-3\n, div-4\n, mod-5\nā);
printf (āEnter the choice : \nā);
scanf (ā%d ā ,&a,&b);
If (c==2)
Sub = a-b;
Else if (c==3)
Multiply =a*b;
Else if (c ==4)
Divide =a/b;
Else if (c == 5)
Mod = a%b;
Getch();
}
ā¢ We can use any level of nesting with the switch
case statement.
ā¢ Default:- When no case is matched, at that
level default is executed.
ā¢ We can use any loop inside the switch case statement.
How we can use char data with switch case
# include(stdio.h)
//used to clear input buffer.
Scanf (ā/cā , &c );
Switch (c)
{
case āaā;
case āAā;
case ātā;
x = a+b;
break;
ā¢ We enter in switch case which we want to
check
Case āsā;
Case ārā;
case ā*ā;
x= a*b,
break;
case ādā;
case āDā;
case āIā;
switch (b)
{
case o : printf ( āwrong dataā);
printf ( āDenominator is zeroā);
printf ( āEnter non zero value for
denominatorā);
scanf (ā%dā ,&b );
x=a/b;
break;
default,
x = a/b;
}
break;
default
{
printf ( āresult = %dā,x);
ProgramsC programming initialsConstant/ Variable/KeywordLecture 10
Lecture 7Lecture 6Lecture 54th Lecture3rd lecture2nd lecture


Comments
Post a Comment