0% found this document useful (0 votes)
3 views35 pages

LectureSlides_4_ArraysStrings

The document provides an overview of arrays and strings, detailing operations on one-dimensional and two-dimensional arrays, including traversal, searching, insertion, deletion, and merging. It also covers string input/output, inbuilt string functions, and character arrays. Additionally, it includes code snippets demonstrating these concepts in C programming.

Uploaded by

cgrewalbe24
Copyright
© © All Rights Reserved
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
Download as key, pdf, or txt
0% found this document useful (0 votes)
3 views35 pages

LectureSlides_4_ArraysStrings

The document provides an overview of arrays and strings, detailing operations on one-dimensional and two-dimensional arrays, including traversal, searching, insertion, deletion, and merging. It also covers string input/output, inbuilt string functions, and character arrays. Additionally, it includes code snippets demonstrating these concepts in C programming.

Uploaded by

cgrewalbe24
Copyright
© © All Rights Reserved
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
Download as key, pdf, or txt
Download as key, pdf, or txt
You are on page 1/ 35

Arrays and Strings

One-dimensional array its operations


(Traversal, Linear Search, Insertion, Deletion,
Bubble Sort), Two-dimensional and its
operations (Addition, Transpose and
Multiplication), Passing of array into a function
(row and entire array), Input and output of a
string, string inbuilt functions, 2-D Character
array.
Collection of same data type elements at
adjacent locations
1-d array
int num[5] ; // total 20 bytes
Initialization
int num[6] = { 2, 4, 12, 5, 45, 5 } ;
int n[ ] = { 2, 4, 12, 5, 45, 5 } ;
float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
Input array – 1d
for ( i = 0 ; i <30 ; i++ ) {
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; }
Array traversal – go through each
element

int A[] ={15,32,14,68,42},i;


for(i=0;i<5;i++)
printf(“%d “,A[i]);
Insertion in array
Deletion is opposite of insertion
Merging arrays
Program exercises
To insert an element in a given array. Ask the
user the value and position of the element to
be inserted
To delete an element in a given array. Ask the
user which element to delete after displaying
the given array
To merge two given arrays.
(given means initialise e.g int A[] = {3,-9,5};
Searching the array
Linear search
Binary search
Hint – insertion in array
int arr[5] ={1,2,3,4}, nElts=4;
//....
for(i=nElts; i>=pos; i--) arr[i] = arr[i-1];

arr[pos-1] = num;
nElts++;
//…
Hint – delete array element
for(i=pos-1; i<nElts-1; i++)
arr[i] = arr[i + 1];

nElts--;
Hint: Merge two arrays
int A[] = {1,2,3,4}, B[] = {4,5,6},C[10];
int a=4,b=3,c=a+b,i,j;
for(i=0;i<a;i++) C[i] = A[i];
for(j=a;j<c;j++) C[j] = B[j-a];
Answer: insertion
main(){
int arr[5] ={1,2,3,4}, nElts=4, pos,i, num;
printf("Enter elt and pos: "); scanf("%d
%d",&num,&pos);
for(i=nElts; i>=pos; i--) arr[i] = arr[i-1];
arr[pos-1] = num;
nElts++;
for(i=0;i<nElts;i++) printf(" %d",arr[i]);
}
//Linear Search
int main( ) {
int A[] = { 55, 65, 75, 56, 78, 78, 90 },n=7,x=78;
int index=-1,i;
for (i = 0; i < n; i++) if(A[i] == x) index= i;
//found
if(index>-1)
printf("%d found at loc %d",x,index+1);
else printf("%d not found",x);
}
Try binary search
You need a sorted array
int A[] = { 55, 56,65, 75, 78, 78, 90 };
int x=75; //to be searched
int n = 7; //array size
Binary search
while (beg<=end) {
mid = (beg + end)/2;
if (A[mid] == x) return mid;
if (A[mid] > x) beg = mid-1;
else end = mid+1);
}
2-d array
int s[4][2] ; // total 32 bytes
Arrays gets stored sequentially in the
memory
3-d array
int A[3][4][2];
2-d initialization
int stud[4][2] = { { 1234, 56 }, { 1212, 33 },
{ 1434, 80 }, { 1312, 78 } } ;
int stud[4][2] = { 1234, 56, 1212, 33, 1434,
80, 1312, 78 } ;
3-d initialization
int A[3][4][2]={
{{2,4},{7,8},{3,4},{5,6}},
{{7,6},{3,4},{5,3},{2,3}},
{{8,9},{7,2},{3,4},{5,1},}
};
Displaying basic matrix
main() {
int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) printf("%d ", A[i][j]);
printf("\n"); } }
Function to add two matrices
int i,j,C[3][3];
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
C[i][j] = A[i][j]+B[i][j];
Strings

Input and output of a string, string


inbuilt functions, 2-D Character array.
String definition
String is a 1-d array of characters terminated
by a null (‘\0’ )
char C[] = {'A','B','C','\0'};
char S[] = "ABC";
Both C and S have length = 3
You can also have NULL instead of ‘\0’ defined
in stdio.h and string.h
I/O tough way
main(){
char name[25] ;
printf ( "Enter name: " ) ;
scanf ( "%[^\n]s", name ) ; // for having
blanks
printf("hello %s",name);
}
I/O easy way
main(){
char C[20];
gets(C); // accepts blank spaces
puts(C);
}
NULL and strlen()
main(){
int i=0;
char C[10];
gets(C);
while(C[i]!=NULL) printf("%c",C[i++]);
//OR while(i<strlen(C)) printf("%c",C[i+
+]);
}
Few Inbuilt functions
main( ) {
char A[] = "wxYZ", B[]="78",C[20];
printf("length of %s = %d",A,strlen(A));
if(strcmp(A,B)==0) printf("\nA,B same
srings");
printf("\nC = %s",strcpy(C,A));
printf("\nUppercase of A = %s",strupr(A));
printf("\nConcat A+C = %s",strcat(C,A));
printf("\n Reverse A = %s",strrev(A));
}
2-d character array
char Names[6][10] = {"akshay",
"parag",
"raman"};
puts(Names[0]); //akshay
char Names[6][10] storage

You might also like