Array Programs
Array Programs
Array Programs
#include<stdio.h>
int main() {
int i, arr[50], num;
return (0);
}
int main() {
int arr1[30], arr2[30], i, num;
return (0);
}
Reversal of an Array
#include<stdio.h>
int main() {
int arr[30], i, j, num, temp;
while (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; // increment i
j--; // decrement j
}
//Print out the Result of Insertion
printf("\nResult after reversal : ");
for (i = 0; i < num; i++) {
printf("%d \t", arr[i]);
}
return (0);
}
1 Enter no of elements : 5
2 11 22 33 44 55
3 Result after reversal : 55 44 33 22 11
#include<stdio.h>
int main() {
int a[30], i, num, largest;
return (0);
}
Output:
Enter no of elements : 5
11 55 33 77 22
Largest Element : 77
#include<stdio.h>
int main() {
int i, arr[50], sum, num;
//Computation of total
sum = 0;
for (i = 0; i < num; i++)
sum = sum + arr[i];
//Printing of total
printf("\nSum=%d", sum);
return (0);
}
Output:
Enter no of elements : 3
Enter the values : 11 22 33
a[0]=11
a[1]=22
a[2]=33
Sum=66
int main() {
int arr[20], i, j, k, size;
return (0);
}
Output:
Enter array size : 5
Accept Numbers : 1 3 4 5 3
Array with Unique list : 1 3 4 5
int main()
{
int array[100], search, c, n;
return 0;
}
Exercise:
Find the smallest element in an array
Find Reverse of an array
void main()
{
int i, j;
char a[3][4];
for(i = 0; i <= 2; i = i + 1)
for(j = 0; j <= 3; j = j + 1)
scanf(" %c", &a[i][j]);
for(i = 0; i <= 2; i = i + 1)
{
for(j = 0; j <= 3; j = j + 1)
printf("a[%d][%d] = %c\t", i, j, a[i][j]);
printf("\n");
}
}
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
return 0;
}
Show the output for each of the following programs. Use the following sample
array data:
int a[3][3] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
#include <stdio.h>
void main()
{
int i, j;
int a[3][3] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
for(i = 0; i <= 2; i = i + 1)
{
for(j = 0; j <= 2; j = j + 1)
printf("a[%d][%d] = %d\t", i, j, a[i][j]);
printf("\n");
}
}
#include <stdio.h>
void main()
{
int i, j;
int a[3][3] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
for(i = 0; i <= 2; i = i + 1)
{
for(j = 0; j <= 2; j = j + 1)
printf("a[%d][%d] = %d\t", j, i, a[j][i]);
printf("\n");
}
}
#include <stdio.h>
void main()
{
int i, j;
int a[3][3] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
for(i = 0; i <= 2; i = i + 1)
{
for(j = 0; j <= 2; j = j + 1)
printf("a[%d][2 - %d] = %d ", i, j, a[i][2 - j]);
printf("\n");
}
}
#include <stdio.h>
void main()
{
int i, j;
int a[3][3] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
for(i = 0; i <= 2; i = i + 1)
{
for(j = 2; j >= 0; j = j - 1)
printf("a[2][%d] = %d\t", j, a[2][j]);
printf("\n");
}
}
Matrix multiplication
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
return 0;
}
int main() {
int i, j, a[10][10], sum, rows, columns;
#include<stdio.h>
Void main()
{
Int I,j,k,n,m;
Int s[50][50],sum,avg,sub;
Char grade[50];
Printf(Enter The No.of Studens\n);
Scanf(%d,&n);
For(i=0;i<n;i++)
{
Sum=0;
Printf(Enter subject marks of student %d:\n,i+1);
For(j=0;j<sub;j++)
{
Scanf(%d,&s[i][j]);
Sum=sum+ s[i][j];
}
Avg=sum/sub;
S[i][j]=avg;
If(avg >80)
Grade[i]=A;
Elseif(avg>60)
Grade[i]=B;
Elseif(avg>50)
Grade[i]=C;
Else
Grade[i]=U;
Exercise:
Find sum of row elements of a matrix
Find sum of column elements of a matrix
Transpose of a matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, x=1;
int arr[3][3][3];
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
scanf("%d",&arr[i][j][k]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
arr[i][j][k] = x;
printf("%d\t",arr[i][j][k]);
x++;
}
printf("\n");
}
printf("\n");
}
getch();
}
void main()
{
int arr[5],i;
clrscr();
printf("\nEnter the array elements : ");
for(i=0;i< 5;i++)
scanf("%d",&arr[i]);
for(i=0;i< 5;i++)
fun(arr[i]);
getch();
}
#include <stdio.h>
float average(float a[]);
int main()
{
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument. */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[]){
int i;
float avg, sum=0.0;
for(i=0;i<6;++i){
sum+=a[i];
}
avg =(sum/6);
return avg;
}
#include
void Function(int c[2][2]);
int main()
{
int c[2][2],i,j;
printf("Enter 4 numbers:\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
scanf("%d",&c[i][j]);
}
Function(c); /* passing multi-dimensional array to function */
return 0;
}
char name[]={'L','e','s','s','o','n','s','\0'};
char str[4];
str="hell";
Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that,
strings are of char type.
char s[5];
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};
char c[20];
scanf("%s",c);
String variable c can only take a word. It is because when white space is
encountered, the scanf()function terminates.
Here, program will ignore Ritchie because, scanf() function takes only string
before the white space.
Reading a line of text
C program to read line of text manually.
#include <stdio.h>
int main(){
char name[30],ch;
int i=0;
printf("Enter name: ");
while(ch!='\n') // terminates if user hit enter
{
ch=getchar();
name[i]=ch;
i++;
}
name[i]='\0'; // inserting null character at end
printf("Name: %s",name);
return 0;
}
This process to take string is tedious. There are predefined
functions gets() and puts in C language to read and display string
respectively.
int main(){
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
clrscr();
printf("Enter a string");
scanf(%[^\n],str); Edit set conversion
printf("%s",str);
getch();
}
char text[20];
gets(text);
printf("%s",text);
-getchar()
-putchar(ch)
-gets(str);
-puts(str);
1. Char x=a;
Printf(%d,x);
2. X=y-1;
3. Char ch;
Ch>=A && ch<=z
STRING FUNCTIONS
There are numerous functions defined in "string.h" header file. Few
commonly used string handling functions are discussed below:
Strings handling functions are defined under "string.h" header file, i.e, you
have to include the code below to run string handling functions.
strchr(mystr, 'f')
strrchr(mystr, 'f')
strstr(inputstr, 'Begi')
p2 = strdup(p1);
strnset(str,'#',4)
String Handling Functions
These functions are packaged in string.h library. Hence, you must include string.h
header file in your program to use these functions.
strcat(string1,string2) function
strcat("hello","world");
strcat() function will add the string "world" to "hello".
strlen(string) function
strlen() function will return the length of the string passed to it.
int j;
j=strlen("studytonight");
printf("%d",j);
output :
12
strcmp() function
strcmp(str1,str2) function will return the ASCII difference between first
unmatching character of two strings.
int j;
j=strcmp("study","tonight");
printf("%d",j);
output:
-1
Strcpy(string1,string2)
Strncpy(string1,string2,n)
Strncmp(string1,stirng2,n)
Strncat(string1,string2,n)
Strstr(string1,string2)
Source Code to Find Number of Vowels, Consonants, Digits and White Space
Character
#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' ||
line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d",v);
printf("\nConsonants: %d",c);
printf("\nDigits: %d",d);
printf("\nWhite spaces: %d",s);
return 0;
}
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int length(char str[]);
void reverse(char str[]);
int palindrome(char str[]);
void copy(char str1[], char str2[]);
int compare(char str1[], char str2[]);
void concat(char str1[], char str2[]);
void search(char str1[], char str2[]);
void count(char str1[]);
void main() {
char a[100], b[100];
int result, option;
do {
printf("\n1.Length of a string");
printf("\n2.Reverse the Given String");
printf("\n3.Check for Palindrome");
printf("\n4.Copy");
printf("\n5.String Comparison");
printf("\n6.String Concatenation");
printf("\n7.String Searching");
printf("\n8.Counting of Words,Characters & Special Characters");
printf("\n9.Quit");
printf("\n\nEnter Your Choice:");
scanf("%d", &option);
flushall();
switch (option) {
case 1:
printf("\nEnter a String:");
gets(a);
result = length(a);
printf("\nLength of %s=%d", a, result);
printf("\nPress a Character");
getch();
break;
case 2:
printf("\nEnter a String:");
gets(a);
reverse(a);
printf("\nResult=%s", a);
printf("\nPress a Character");
getch();
break;
case 3:
printf("\n Enter a String:");
gets(a);
result = palindrome(a);
if (result == 0)
printf("\nNot a palindrome");
else
printf("\nA palindrome");
printf("\nPress a Character");
getch();
break;
case 4:
printf("\nEnter a String:");
gets(a);
copy(b, a);
printf("\nResult=%s", b);
printf("\nPress a Character");
getch();
break;
case 5:
printf("\nEnter 1st string:");
gets(a);
printf("\nEnter 2nd string:");
gets(b);
result = compare(a, b);
if (result == 0)
printf("\nboth are same");
else if (result > 0)
printf("\n1st>2nd");
else
printf("\n1st<2nd");
printf("\nPress a Character");
getch();
break;
case 6:
printf("\nEnter 1st string:");
gets(a);
printf("\nEnter 2nd string:");
gets(b);
concat(a, b);
printf("\nresult=%s", a);
printf("\nPress a Character");
getch();
break;
case 7:
printf("\nEnter 1st string:");
gets(a);
printf("\nEnter 2nd string:");
gets(b);
search(a, b);
printf("\nPress a Character");
getch();
break;
case 8:
printf("\nEnter a string:");
gets(a);
count(a);
printf("\nPress a Character");
getch();
break;
default:
printf("\nInvalid Choice:");
break;
}
Exercise: