CP 2 Front

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

EST102 Programming in C

Disadvantages of an Array https://www.keralanotes.com/


• It allows us to enter only fixed number of elements into it. We cannot alter the size of
the array once array is declared. Hence if we need to insert more number of records
than declared then it is not possible. We should know array size at the compile time
itself.
• Inserting and deleting the records from the array would be costly since we add /
EST102 Programming in C
delete the elements from the array, we need to manage memory space too.

Disadvantages of an Array https://www.keralanotes.com/


• It does not verify the indexes while compiling the array. In case there is any indexes

• It allows us to enter only fixed number of elements into it. pointed which
We cannot alteristhe
more
sizethan
of the dimension specified, then we will get run time errors
the array once array is declared. Hence if we need to insertrather
morethan identifying
number them at compile time.
of records
Example:
than declared then it is not possible. We should know array size at the compile time
itself. int a[3]={1,2,3}
m

printf("%d",A[3]);
• Inserting and deleting the records from the array would be costly since we add /
EST102 Programming in C
co

Output:
delete the elements from the array, we need to manage memory space too. 3245431 (Garbage Value)
• It doeschar
notstr[]="Hello
verify the World"; https://www.keralanotes.com/
indexes while compiling the array. In1.case Write a program
there to read and display an array of size n.
is any indexes
.

int len=strlen(str); #include<stdio.h>


pointed which is more than the dimension specified, then we will get run time errors
es

int size=sizeof(str); void main()


rather than identifying them at compile time.
printf("Value=%d",len*size); {
ot

Example: int n,A[20],i;


}
int a[3]={1,2,3} printf("Enter the size of Array:");
an

len value will the length of the string which is 11. But size value will be 12 including the
m com

scanf("%d",&n);
printf("%d",A[3]);
end of character. EST102 Programming in C
for(i=0;i<n;i++)
Output:
Output 3245431 (Garbage Value)
al

1. Write a program to read and display an array of size n.


Value=132
{ Sum=50
printf("\nEnter the number:");
https://www.keralanotes.com/
r

scanf("%d",&A[i]); 4. Write a program to multiply two matrices


.

PROGRAM
ke

#include<stdio.h>
s. es

} To multiply two matrices, the number of columns of the first matrix should be
strcat
void main()
#include<stdio.h> { for(i=0;i<n;i++) equal to the number of rows of the second matrix.
It concatenates two strings and returns the concatenated string.
ot

struct employee int n,A[20],i; {


Syntax: strcat(string1,string2) – concatenate string1 with string2. printf("%d\t",A[i]);
{ printf("Enter the size of Array:"); #include <stdio.h>
co
an

#include <stdio.h> }
int employeeID; scanf("%d",&n); void main()
char ename[25]; #include <string.h>
for(i=0;i<n;i++) } {
OUTPUT int i,j,k,m, n, p, q,sum = 0;
ra ral

float salary; {void main()


}; { Enter the size of Array: 5 int A[10][10], B[10][10], C[10][10];
te

printf("\nEnter the number:");


char s1[10] = "Hello"; Enter the number:10 printf("Enter number of rows and columns of first matrix\n");
void main() scanf("%d",&A[i]);
ke

EST102
theProgramming in C scanf("%d%d", &m, &n);
no

char s2[10] = "World"; Enter number:20


{ }strcat(s1,s2); printf("Enter number of rows and columns of second matrix\n");
Enter the number:30
struct employee e1[100]; for(i=0;i<n;i++)
https://www.keralanotes.com/
str1[i]=str2[j];%s", s1);
printf("After concatenation: scanf("%d%d", &p, &q);
m

Enter the number:40


la

int n,i; {} } if (n != p)
printf("Enter the total number of employees:\n"); Output:printf("%d\t",A[i]);
str1[i]='\0'; Enter the number:50 printf("The multiplication isn't possible.\n");
co

scanf("%d",&n); printf("\nOutput:
}After concatenation: %s",str1);
HelloWorld else
} IIPE 2
ke

printf("ENTER THE EMPLOYEE DETAILS:\n"); } {


s.

for(i=0;i<n;i++) OUTPUT printf("Enter elements of first matrix\n");


Program to concatenate two strings without using string handling function
for (i = 0; i < m; i++)
te

{ strcpy
Enter the size of Array: 5
#include <stdio.h> For More Study Materials : www.keralanotes.com
for (j = 0; j < n; j++)
printf("Enter the employeeID :"); Enter the number:10
void It copies the string str2 into string str1, including the end character (terminator
main()
no

scanf("%d", &A[i][j]);
scanf("%d",&e1[i].employeeID); Enter the
{ number:20
char ‘\0’). printf("Enter elements of second matrix\n");
printf("Enter the employee name:"); Enter the number:30
char str1[50], str2[25], i, j; for (i = 0; i < p; i++)
Syntax: strcpy(string1,string2) - copy the content of string2 to string1
la

scanf("%s",e1[i].ename); printf("\nEnter
Enter the number:40 first string: "); for (j = 0; j < q; j++)
#include <stdio.h>
gets(str1);
printf("Enter the salary:"); Enter the number:50
ra

scanf("%d", &B[i][j]);
#include <string.h>
printf("\nEnter second string: ");
scanf("%f",&e1[i].salary); for (i= 0; i < m; i++)
void main()
gets(str2);
ke

} IIPE 2 {
{ for(i=0; str1[i]!='\0'; i++);
om

for (j = 0; j < q; j++)


char s1[30];
for(j=0; str2[j]!='\0'; j++, i++) EST102 Programming in C
{
printf("THE EMPLOYEE DETAILS ARE:\n"); char
{ s2[30] = "Hello World";
For More Study Materials : www.keralanotes.com C[i][j]=0;
for(i=0;i<n;i++) strcpy(s1,s2);
str1[i]=str2[j];
https://www.keralanotes.com/
.c

for (k = 0; k < p; k++)


{ IIPE printf("String
} s1: %s", s1); 13
} {
es

str1[i]='\0';
printf("%d %s %f \n",e1[i].employeeID,e1[i].ename,e1[i].salary); Output:printf("\nOutput: %s",str1); C[i][j]= C[i][j] + A[i][k]*B[k][j];
} String
} For More
s1 : Hello World Study Materials : www.keralanotes.com }
ot

}
Program to copy a string to another without using string handling function
} OUTPUT }
n

strcpy
#include<stdio.h> printf("Product of the matrices:\n");
Enter the total number of employees:
la

void main() for (i = 0; i< m; i++)


3 It copies the string str2 into string str1, including the end character (terminator
ENTER THE EMPLOYEE DETAILS: { { EST102 Programming in C
ra

Enter the employeeID :111 char ‘\0’).


char a[50], b[50]; for (j = 0; j< q; j++)
Enter the employee name:Akash
Enter the salary:20 000
Syntax:int
strcpy(string1,string2)
i=0; - copy the content of string2 to string1 {
https://www.keralanotes.com/
ke

printf(“Enter IIPE 22
Enter the employeeID :112 #include <stdio.h> string1:”); printf("%d\t", C[i][j]);
Enter the employee name:Aishwarya gets(a);
Enter the salary:30 000 #include <string.h> }
for(i=0;a[i]!=‘\0’;i++)
void main() printf("\n");
Enter the employeeID :113
{
For More Study Materials : www.keralanotes.com
Enter the employee name:Sam { }
om

Enter the salary:40 000 b[i]=a[i];


THE EMPLOYEE DETAILS ARE: char s1[30]; }
111 Akash 20.000000
} s2[30] = "Hello World";
char }
112 Aishwarya 30.000000 b[i]=‘\0’;
strcpy(s1,s2);
.c

113 Sam 40.000000 } printf("String s1: %s", s1); PREVIOUS YEAR UNIVERSITY QUESTIONS
} 1. Explain the different ways in which you can declare & initialize a single dimensional
es

strcmp
Output: array. [KTU, MODEL 2020]
String s1 : Hello World 2. Write a C program to read a sentence through keyboard and to display the count of
ot

Program to copy a string to another without using string handling function white spaces in the given sentence. [KTU, MODEL 2020]
3. Write a C program to check whether a given matrix is a diagonal matrix. [KTU,
n

#include<stdio.h>
Downloaded from Ktunotes.in MODEL 2020]
la

void main()
IIPE 14 4. Write a C program to perform bubble sort. [KTU, MODEL 2020], [KTU, JULY 2017],
{

You might also like