Am - En.u4cse20370 Lab 6
Am - En.u4cse20370 Lab 6
AM EN U4CSE20370
Lab Assignment 6
1. Write a program which reads in 10 integers from the user and stores them
in an array. Find the largest value in the array and print it.
#include<stdio.h>
int main(){
int int forarr[11]; t=0;
{
(int i=0;i<10;i++)
scanf("%d",&arr[i]);
}
for(inti=0;i<10;i++)
{
if (arr[i]>t){ t = arr[i];
}
}
printf("Largestnumberis%d",t);
}
2. Modify the last program to use a preprocessor constant for the size of the
array and in the test condition of the loop which processes the array.
#include<stdio.h>
#definet10
int main(){
int int for
arr[t]; max=0;
{
(int i=0;i<t;i++)
scanf("%d",&arr[i]);
}
for(inti=0;i<t;i++)
{
if(arr[i]>max){ max=arr[i];
}
}
printf("Largestnumberis%d",max);
}
3. Modify the last program to find mean of n numbers using arrays.
int main(){
int int int for
t=10;
{ arr[t]; max=0;
(int i=0;i<t;i++)
scanf("%d",&arr[i]);
}
for(inti=0;i<t;i++)
{
max += arr[i];
}
max=max/10;
printf("Averagenumberis%d",max);
}
4. Write a program to interchange the largest and the smallest number in the array.
#include<stdio.h>
int main(){
int int intt=10;
int for
{ arr[t]; max=0; min=0;
(int i=0;i<t;i++)
scanf("%d",&arr[i]);
}
for (inti=0;i<t;i++)
{
if (arr[i]>arr[max]){ max = i;
}
if (arr[i]<arr[min]){ min = i;
}
}
int
temp=arr[max];
arr[max]=arr[min];
arr[min]=temp;
printf("largest afterinterchanging%d\n",arr[mi n]); printf("smallestnumbersafterinterchanging%d ",arr[max]);
}
5. Write a program to find the second biggest number using an array of n
numbers.
#include<stdio.h>
int main(){
int int int
arr[11];
for t=0; x=0;
{
(int i=0;i<10;i++)
scanf("%d",&arr[i]);
}
for(inti=0;i<10;i++)
{
if (arr[i]>t){ x=t;
t = arr[i];
}
}
printf("secondnumberis%d",x);
}
scanf("%d",&arr[i]);
}
for(inti=0;i< 10;i++)
{
for(intk=i+1; k<10;k++)
{
if(arr[k]==arr[i]){ printf("duplicate
}}}
valueof%disonlocati ons%d\n",arr[i],k);}
7. Write a program which can store 10 integers in an array. Fill the array with
“random” numbers using the library functions rand() instead of reading them
from the user. Find the largest element in the array and print it out.
Each time rand() is called it returns a “random” integer. Use the mod operator
( % ) to get a value in the desired range. For example:
int result; result = rand()
% 1000;
#include <stdlib.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
int for arr[10];
{ (inti=0;i <10;i++)
arr[i]=rand()%1000;
}
int max=0;
for (inti=0;i <10;i++)
{
if (max<arr[i])
{
max = arr[i];
}
}
printf("largestnumberinrandaomarrayis %d",max);
}
8. Modify the last program so that instead of finding the largest element in the
array, the program sorts the elements of the array into ascending order.
#include<stdio.h>
#include<stdlib.h>
int main(){
int for arr[10];
{ (inti=0; i<10;i++)
arr[i]=rand()%999;
}
intmax=0,tem=0; for(inti=0;
{ i <10;i++)
for(intj=
1; j < 10;j++){
if(arr[j]<arr[j-1]){
tem=arr[j]; arr[j]=arr[j-1]; arr[j-1]=tem;
}}
}
for (int i = 0; i < 10; i++)
{
printf("array %d ",arr[i]);
}}
#include<stdio.h>
int bs(intstart,intend,int x,intarr[])
{
if (start<=end)
{
intmid=start+(end-start)/2; if(x==arr[mid])
{
return mid;
}
if(arr[mid]>x)
{
returnbs(start,mid-1,x,arr);
}
if (arr[mid]<x)
{
returnbs(mid+1,end,x,arr);
}
}
else{
return-1;
}
}
intmain(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int x=0,e=0; e= 9;
printf("Enter number bw 1-10 ");
scanf("%d",&x);
int res = bs(0,e,x,arr);
if (res!=-1)
{
printf("\nFound at %d",res);
}
else{
printf("\nFound at %d",res);
printf("\n not found");
}}
#include<stdio.h>
int main(){
int 8}; arr[]={15,12,48,56,12,7,8,78,59,13,4,6,18,49,3
int for
{ tem=0;
(inti=0;i<15; i++)
for(intj=0;j<15;j++){
if(arr[j]<arr[j-1]){ tem=arr[j]; arr[j]=arr[j-1];
arr[j-1]=tem;
}
}}
for (int i=0;i<15;i++)
{
printf("%d",arr[i]);
}
}
#include<stdio.h>
int main(){
int int for arr[]={15,12,48,56,12,7,8,78,59,13,4,6,18,49,38};
{ m=100; int x=0;
(inti= 0;i<15;i++)
for(intj=i;j<15;j++){
if(m>arr[j]){
m=arr[j]; x=j;
}
}
arr[x]=arr[i]; arr[i]=m; m=100;
}
for (int i = 0; i < 15; i++)
{
printf("%d ",arr[i]);
}
}
13. Read a value k from the user and using k left rotation and right
rotation depending on the user choice Left or Right, print the rotated
value. [Use function for the rotations]
#include <conio.h>
#include <stdio.h>
int main(void){
int nums[10]={1,10,6,12,2,4,5,8,0,-1};
sum;
printf("Enter a number: ");
scanf("%d",&sum);
for(int i=0;i<10;i++){
for(int j=i+1;j<10;j++)
{
if(nums[i]+nums[j]==sum){ printf("(%d,
%d)\n",nums[i],n ums[j]); break;
}}
}
return 0;
}
2-D Array
15. Write a program to read and display a matrix.
#include <stdio.h>
int main()
{
int i=0,j=0,x[100][100],m,n;
#include <stdio.h>
int main()
{
int i=0,j=0,m,n,x[100][100];
printf("Enter the order of the matrix:");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter Row %d Column %d:",i,j);
scanf("%d",&x[i][j]);}
}
printf("The matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
printf("Matrix after transposing:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[j][i]);
}
printf("\n");
}
return 0;
}
18. Write a program to find the sum of all the elements in a 2D array.
#include <stdio.h>
int main()
{
int i=0,j=0,sum,m,n;
printf("Enter the order of the matrix:");
scanf("%d %d",&m,&n);
int x[100][100];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("x[%d][%d]:",i,j);
scanf("%d",&x[i][j]);}
}
for(i=0;i<m;i++){ for(j=0;j<n;j++)
{
sum=sum+x[i][j];}
}
printf("Sum of all rows in the array:
%d",sum); return 0;
}
19. Write a program to find the sum of the elements in each row of a 2D array and
print it.
#include <stdio.h>
int main()
{
int i,j,sum=0,p,q; int x[100][100];
printf("Enter the dimensions of the array:");
scanf("%d %d",&p,&q);
printf("Enter %d elements into the
array:\n",p*q); for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("x[%d][%d]:",i,j);
scanf("%d",&x[i][j]);
}
}
printf("The array is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{sum=sum+x[i][j];
}
printf("Sum of elements of row %d is:
%d\n",i,sum); sum=0;
}
return 0;
}
20. Write a program to fill a square matrix with value 0 on the diagonal, 1 on
the upper right triagle and -1 on the lower left triangle.
#include <stdio.h>
int main()
{
int i=0,j=0,x[100][100],n;
printf("Enter the order of the square
matrix:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&x[i][j]);
}
}
printf("The matrix
is:\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
x[i][j]=0;
else if(i!=j && i>j) x[i]
[j]=1; else x[i][j]=-1;
}
}
printf("Matrix after
Interchanging:\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
return 0;
}
21. Write a program to multiply two matrices.
#include <stdio.h>
int main()
{
int p,q;
printf("Enter order of 1st matrix:");
scanf("%d %d",&p,&q);
printf("Enter elements into 1st
matrix:\n"); int i=0,j=0,x[100][100];
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&x[i][j]);
}
}
printf("The 1st matrix is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",x[i][j]);} printf("\n");
}
int y[100][100];
int a,b;
printf("Enter order of 2nd matrix:");
scanf("%d %d",&a,&b);
for(i=0;i<p;i++){ for(j=0;j<b;j++)
{
printf("%d\t",prod[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
void main()
{
static int array1[10][10], array2[10][10]; int i, j, m, n, a, b, c, p, q, r;
24. Write a program to Sort Rows of the Matrix in Ascending & Columns in
Descending Order.
Sample Output
Enter the order of the matix
22
Enter the co-efficients of the
matrix 40 30
38 90
The given matrix
is 40 30
38 90
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (array2[i][j] < array2[k][j])
{
a = array2[i][j]; array2[i][j] = array2[k][j]; array2[k][j] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array2[i][j]);
}
printf("\n");
}
}
25. Write a program to do the Sum of the Main & Opposite Diagonal Elements
of a MxN Matrix.
#include <stdio.h>
void main ()
{
int i, j, m, n, a = 0, sum = 0;
if (m == n )
{
printf("\nThe sum of the main diagonal elements is = %d\n", sum); printf("The sum of the off diagonal elements is = %d\
else
printf("The given order is not square matrix\n");
}
Strings
Reading Strings
26. If we declare a string by writing char str[50]; Then str can be read
from the user by using three ways:
1.Using scanf() function
2.Using gets() function
3.Using getchar() function repeatedly
Write program to read a string in the above three ways.
CODE 1
#include <stdio.h>
int main()
{
char str[50];printf("Enter your name:"); scanf("%
[^\n]",str);
printf("%s using 'scanf':\n",str);
}
CODE 2
#include <stdio.h>
int main()
{
char str[50];
printf("Enter your full name:");
gets(str);
printf("%s",str);
return 0;
}
CODE 3
#include <stdio.h>
int main()
{
char str[50], ch; int i=0;
printf("Enter a string:
"); ch = getchar ();
while(ch!='\n')
{
str[i] = ch; i++;
ch = getchar();
}
str[i] ='\0';
printf("Using 'getchar': %s",
str); return 0;
}
Writing strings
27. The string can be displayed on screen using three ways:
1.Using printf() function
2.Using puts() function
3.Using putchar()function repeatedly.
Modify the above program to display the string that you read.
CODE 1
#include <stdio.h>
int main()
{
char str[50];
printf(“Enter your name:”);
scanf(“%[^\n]”,str);
printf(“%s”,str);
}
CODE 2
#include<stdio.h>
int main()
{
char str[50];
printf("Enter your name: ");
scanf("%[^\n]",str);
puts(str);
return 0;
}
CODE 3
#include <stdio.h>
int main()
{
char str[50]; int i= 0;
char ch = getchar();
while(ch!='\n')
{
str[i] = ch;
ch = getchar(); i++;
}
str[i] ='\0'; int j = 0;
while(str[j]!='\0')
{
putchar( str[j] );
j++;}
return 0;
}
28. Run the following program and analyze the result. #include<stdio.h> main()
{
char str = “Hello”; printf(“\n
%s”,str); printf(“\n
%s”,&str); printf(“\n
%s”,&str[2]);
}
OUTPUT:
Hello
Hello
Llo
REASON:
* Here, %s in the printf function prints the string until termination
character is reached, which is Hello.
* %s , & str also prints the entire string until the termination character, that is
Hello
* %s,&str[2] prints the string from the index location:2 which is, llo
29. Run the following program and analyze the result. It’s
about the use of width and precision specifications along with
%s .
#include<stdio.h> main()
{
char str[] = “Introduction
to C”; printf(“\n
|%s|”,str); printf(“\n
|%20s|”,str); printf(“\n
|%- 20s|”,str); printf(“\n
|%.4s|”,str); printf(“\n
|%20.4s|”,str); printf(“\n
|%-20.4s|”,str);
}
REASON:
In 1st printf function |%s| is used so it prints the string between | and |, ie
| Introduction to c|
30. Write a program to find the length of a string. Also use strlen( ) to do the same.
#include <stdio.h>
int main()
{
char str[100],i; printf("Enter a string:
\n"); scanf("%s",str);
for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);
return 0;
}
WITH STRLEN()
#include <stdio.h>
#include <string.h>
int main()
{
char a[100]; int length;
printf("Enter a string to calculate its
length\n"); gets(a);
length = strlen(a);
printf("Length of the string = %d\n",
length); return 0;
}
31. Write a program to copy on string to another without using any string
library functions. Do the same operation using strcpy() function in string.h)
int main() {
char s1[100], s2[100]; int i;
printf("\nEnter the string :");
gets(s1);
i = 0;
while (s1[i] != '\0') { s2[i] =
s1[i]; i++;
}
s2[i] = '\0';
printf("\nCopied String is %s ",
s2); return (0);
}
WITH STRCPY()
#include<stdio.h>
#include<string.h>
int main() {
char str1[100];
char str2[100];
printf("\nEnter the String 1 : ");
gets(str1);
strcpy(str2, str1);
printf("\nCopied String : %s", str2);
return (0);
}
#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0; str1[i]!='\0'; ++i);
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
printf("\nOutput: %s",str1);
return 0;
}
With strcat()
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first
string\n"); gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a); return 0;
}
34. Write a program to compare two strings. (Do the same operation using the
string library function strcmp() and analyze the behavior; you should include
string .h)
WITHOUT LIBRARY FUNCTION
#include<stdio.h>
int main() {
char str1[30], str2[30]; int i;
printf("\nEnter two strings :");
gets(str1);
gets(str2);
i = 0;
while (str1[i] == str2[i] && str1[i] !=
'\0') i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
return (0);
}
WITH STRCMP()
#include
<stdio.h>
#include<string.h
> int main()
{
char str1[20];
printf("Enter the second string : ");
scanf("%s",str2);
if(value==0)
printf("strings are
same"); else
printf("strings are not same");
return 0;
}