0% found this document useful (0 votes)
8 views14 pages

Arrays, Characters, Strings

The document discusses arrays in C programming. It provides examples of declaring and initializing single-dimensional and multi-dimensional arrays. It also demonstrates how to access array elements, pass arrays to functions, return arrays from functions, sort arrays, perform operations on arrays like finding the largest/sum of elements, and searching arrays. Examples of 2D arrays include matrix addition and multiplication.

Uploaded by

It's Me
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)
8 views14 pages

Arrays, Characters, Strings

The document discusses arrays in C programming. It provides examples of declaring and initializing single-dimensional and multi-dimensional arrays. It also demonstrates how to access array elements, pass arrays to functions, return arrays from functions, sort arrays, perform operations on arrays like finding the largest/sum of elements, and searching arrays. Examples of 2D arrays include matrix addition and multiplication.

Uploaded by

It's Me
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/ 14

Array:

//Arrays - Collection of elements of same datatypes stored in a contiguous location


#include<stdio.h>
int main()
{
int a; //single variable of int -2 bytes
//Initialize array
int a[3]; //array-10 integer variables, each 2 bytes, 20 bytes
a[0]=14; //starting index of array
a[1]=15;
a[2]=16;
//Initialize array
int b[3]={14,15,16};
//Initialize array
int c[]={14,15,16};
printf("%p %p %p",a,b,c);
return 0; //0 stands for successful termination of the program, returned to compiler
}

Name of an array is a pointer to the starting index of array

//Arrays - Collection of elements of same datatypes stored in a contiguous location


#include<stdio.h>
int main()
{
int *p; //Pointer variable
//Initialize array
int a[3]; //array-10 integer variables, each 2 bytes, 20 bytes
a[0]=14; //starting index of array
a[1]=15;
a[2]=16;
//Initialize array
int b[3]={14,15,16};
//Initialize array
int c[]={14,15,16};
p=a;
printf("%p ",p);
p=b;
printf("%p ",p);
p=c;
printf("%p ",p);
return 0; //0 stands for successful termination of the program, returned to compiler
}
Accessing array elements and addresses:

#include<stdio.h>
int main()
{
int a[3]={5,6,7};
int *p;
printf("a[0]=%d *(a+0)=%d &a[0]=%p a+0=%p\n",a[0],*(a+0),&a[0],a+0);
printf("a[1]=%d *(a+1)=%d &a[1]=%p a+1=%p\n",a[1],*(a+1),&a[1],a+1);
printf("a[2]=%d *(a+2)=%d &a[2]=%p a+2=%p\n",a[2],*(a+2),&a[2],a+2);
return 0;
}

Input/Output array values:

#include<stdio.h>
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]); //a+i
printf("\nThe array entered is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]); //*(a+i)
return 0;
}

Passing an array to function

1. As pointer

#include<stdio.h>
void printarray(int *b,int size) //thus, b is a pointer taking address from a
{
int i;
for(i=0;i<size;i++)
printf("%d ",*(b+i));
}
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array entered is: ");
printarray(a,n); //array name a is a pointer, holding address
//a is passed by reference, n is passed by value
return 0;
}

2. As sized array

#include<stdio.h>
void printarray(int b[10],int size) //thus, b is a pointer taking address from a
{
int i;
for(i=0;i<size;i++)
printf("%d ",b[i]);
}
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array entered is: ");
printarray(a,n); //array name a is a pointer, holding address
//a is passed by reference, n is passed by value
return 0;
}

3. Unsized array:

#include<stdio.h>
void printarray(int b[],int size) //thus, b is a pointer taking address from a
{
int i;
for(i=0;i<size;i++)
printf("%d ",b[i]);
}
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array entered is: ");
printarray(a,n); //array name a is a pointer, holding address
//a is passed by reference, n is passed by value
return 0;
}

Returning an array:

1. Using static

#include<stdio.h>
void printarray(int b[],int size) //thus, b is a pointer taking address from a
{
int i;
for(i=0;i<size;i++)
printf("%d ",b[i]);
}
int* getarray(int size) //int[]=int*
{
static int b[10];
int i; //b is a local variable, destroyed when function returns
for(i=0;i<size;i++)
scanf("%d",&b[i]);
return b;
}
int main()
{
int *a,n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
a=getarray(n);
printf("\nThe array entered is: ");
printarray(a,n); //array name a is a pointer, holding address
//a is passed by reference, n is passed by value
return 0;
}
2. By passing another array

#include<stdio.h>
void printarray(int b[10],int size) //thus, b is a pointer taking address from a
{
int i;
for(i=0;i<size;i++)
printf("%d ",b[i]);
}
int* getarray(int b[10],int size) //passing an array itself from main
{
int i;
for(i=0;i<size;i++)
scanf("%d",&b[i]);
return b;
}
int main()
{
int a[10],n,i;
int *p;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
p=getarray(a,n); //a's address is passed, and the same address is returned to p
printf("\nThe array entered is: ");
printarray(p,n);
return 0;
}

Sort an array in ascending order:

#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void sortarray(int c[10],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(c[i]>c[j])
swap(&c[i],&c[j]);
}
}
}
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nThe sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

Find the largest element in array:

#include<stdio.h>
int largestinarray(int c[10],int size)
{
int max,i,j;
max=c[0];
for(i=1;i<size;i++)
{
if(max<c[i])
max=c[i];
}
return max;
}
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe entered array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\nThe largest element in array is: %d",largestinarray(a,n));
return 0;
}

Sum of array elements:

#include<stdio.h>
int sumofarray(int c[10],int size)
{
int sum=0,i,j;
for(i=0;i<size;i++)
{
sum+=c[i];
}
return sum;
}
int main()
{
int a[10],n,i;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe entered array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\nThe sum of elements of array is: %d",sumofarray(a,n));
return 0;
}

Search an array for a given value:

#include<stdio.h>
int searcharray(int c[10],int size,int value)
{
int i,j,index=-1;
for(i=0;i<size;i++)
{
if(c[i]==value)
index=i;
}
return index;
}
int main()
{
int a[10],n,i,item,pos;
printf("Enter the no of elements of array: ");
scanf("%d",&n);
printf("\nEnter the array, separated by a space: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe entered array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\nEnter the element you're searching for: ");
scanf("%d",&item);
pos=searcharray(a,n,item);
if(pos!=-1)
printf("\nThe element %d is found at index %d",item,pos);
else
printf("\nThe element %d is not found in the array",item);
return 0;
}

2D Array:

#include<stdio.h>
int main()
{
//rows-->[3]
//1234 -->[4]
//3465 -->[4]
//5678 -->[4]
int a[3][4],i,j,row,col;
printf("Enter the number of rows: ");
scanf("%d",&row);
printf("Enter the number of columns: ");
scanf("%d",&col);
printf("\nEnter the 2D array:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
printf("\nThe entered 2D array is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}

Matrix Addition:

#include<stdio.h>
int a[10][10],b[10][10],c[10][10],row,col;
int main()
{
int i,j;
printf("Enter the number of rows: ");
scanf("%d",&row);
printf("Enter the number of columns: ");
scanf("%d",&col);
printf("\nEnter the matrix A:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter the matrix B:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("\nThe sum matrix C is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}

Matrix Multiplication:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],rowA,colA,rowB,colB,i,j,k;
printf("Enter the number of rows of matrix A: ");
scanf("%d",&rowA);
printf("Enter the number of columns of matrix A: ");
scanf("%d",&colA);
printf("\nEnter the matrix A:\n");
for(i=0;i<rowA;i++)
{
for(j=0;j<colA;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the number of rows of matrix B: ");
scanf("%d",&rowB);
printf("Enter the number of columns of matrix B: ");
scanf("%d",&colB);
printf("\nEnter the matrix B:\n");
for(i=0;i<rowB;i++)
{
for(j=0;j<colB;j++)
scanf("%d",&b[i][j]);
}
if(colA!=rowB)
{
printf("\nCannot multiply matrices\nExiting");
return 0;
}
for(i=0;i<rowA;i++)
{
for(j=0;j<colB;j++)
{
c[i][j]=0;
for(k=0;k<rowB;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\nThe product matrix C is:\n");
for(i=0;i<rowA;i++)
{
for(j=0;j<colB;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}

Characters:

#include<stdio.h>
int main()
{
printf("%c in ASCII is %d\n",'A','A');
printf("%c in ASCII is %d\n",'a','a');
printf("%c in ASCII is %d",'0','0');
return 0;
}

A in ASCII is 65
a in ASCII is 97
0 in ASCII is 48

Uppercase to lowercase:

#include<stdio.h>
int main()
{
char ch='A';
printf("%c in lowercase is %c",ch,ch+32);
return 0;
}

Lowercase to Uppercase:

#include<stdio.h>
int main()
{
char ch='a';
printf("%c in lowercase is %c",ch,ch-32);
return 0;
}

Character to Integer: (Numbers)

#include<stdio.h>
int main()
{
char ch='5'; //53
printf("'%c' in integer is %d",ch,ch-48);
return 0;
}

Integer to Character: (Numbers)

#include<stdio.h>
int main()
{
int ch=5; //53
printf("%d in character is '%c'",ch,ch+48);
return 0;
}

ASCII of space:
#include<stdio.h>
int main()
{
char ch=' ';
printf("'%c' in ASCII is %d",ch,ch);
return 0;
}
' ' in ASCII is 32

Check if a given character is a number,uppercase,lowercase,space or special character:

#include<stdio.h>
int main()
{
char ch='.';
if(ch>=48&&ch<=57)
printf("'%c' is a number character with ASCII value %d",ch,ch);
else if(ch>=65&&ch<=91)
printf("'%c' is a uppercase character with ASCII value %d",ch,ch);
else if(ch>=97&&ch<=123)
printf("'%c' is a lowercase character with ASCII value %d",ch,ch);
else if(ch==32)
printf("'%c' is a blank character with ASCII value %d",ch,ch);
else
printf("'%c' is a special character with ASCII value %d",ch,ch);
return 0;
}

Using library functions: <ctype.h>

#include<stdio.h>
#include<ctype.h>
int main()
{
char ch='?';
if(isdigit(ch))
printf("'%c' is a number character with ASCII value %d",ch,ch);
else if(isupper(ch))
printf("'%c' is a uppercase character with ASCII value %d",ch,ch);
else if(islower(ch))
printf("'%c' is a lowercase character with ASCII value %d",ch,ch);
else if(isspace(ch)||isblank(ch))
printf("'%c' is a blank character with ASCII value %d",ch,ch);
else
printf("'%c' is a special character with ASCII value %d",ch,ch);
return 0;
}

Strings:

Pointer logic16:26
Pointer with array16:26
Pointer to pointer16:26
Array of pointers16:27
Function pointer16:27
Void pointer16:27
Null pointer16:27
Dangling pointer16:27
Pointer to constant16:27
Constant pointer16:27
Strings16:27
Strings and pointers16:27
Pointer arithmetic16:27
Structures16:27
Structure variables16:27
Structure pointers16:27
Unions

You might also like