cp unit-3 notes
cp unit-3 notes
Unit-3 arrays
1 a) What is an array? How to initialize, access the elements of array show with examples?
Answer:
An array is a variable that can store multiple values of same type which occupies continuous memory
locations
• An integer array contains all integer values
• A float array contains all float values
• A double array contains all double values
• A char array contains all characters
Declaring array:
Syntax:
datatype arrayname[size];
- datatype specifies type of data array contains.
- arrayname is name of the variable
- size specifies no:of elements that an array can store
- size must be specified inside „[ ]‟
Examples:
int a[10];
• array name is „a‟ which can hold upto 10 elements.
• 10 memory locations are allocated each of 2 bytes as integer size is 2 bytes.Total 10*2=20 bytes
of memory is allocated.
• Index starts from 0 to size-1
Runtime initialization:
• An array can also be initialized at the time of execution using scanf() function
Example: Reading 10 elements
for (i=0; i<10; i++)
{
scanf(“%d”,&a[i]);
}
b) Develop a c program to read and print the 1-D array elements?
Program:
#include<stdio.h>
void main()
{ Output:
int a[20], i; Enter length of array
printf(“enter length of array\n”); 5
scanf(“%d”,&n); Enter array elements
printf("Enter array elements");
1 3 4 5 6
for(i = 0; i < n; i++)
Elements of array are
{
1
scanf("%d", &a[i]);
3
}
4
printf(“elements of array are\n”);
5
for(i = 0; i < n; i++)
6
{
printf("%d\n", a[j]);
}
}
2 a) What is an 2D array? How to initialize, access the elements of 2D array show with examples?
Answer:
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.
Declaration:
Datatype arrayname[m][n];
M – row size and N – Column size
Eg - int a[3][3] ;
2DArray Initialization:
Initializing means assigning values to the array.After an array is declared it must be initialized.
Otherwise, it will contain garbage value(any random value)
• An array can be initialized at either
– compile time
– at runtime
compile time Initialization:
Here array is initialized at the time of declaration of the array
Syntax: Datatype arrayname[m][n] = { {row1values},----{row n values} };
Here m is rowsize and n is column size
Example:
int a[3][3]={ { 2,4,5}, {3,1,9},{10,8,6}};
Eg Marix: 2 4 5
3 1 9
10 8 6
Runtime initialization:
• An array can also be initialized at the time of execution using scanf() function
Example: Reading elements 3X3 matrix
for(i=0; i<3 ;i++)
{
for( j=0; j<3; j++)
{
scanf (“%d”,&a[i][j]);
}
}
b) Develop a c program to read and print the 2-D array elements?
Program:
#include<stdio.h>
void main()
{ Output:
int a[20][20], m,n,i, j;
enter the rowsize and column size
printf(“enter the rowsize and column size\n”);
scanf(“%d%d”,&m,&n); 3 3
printf(“enter the matrix values\n”);
for(i=0; i<m ;i++) enter the matrix values
{
for( j=0; j<n; j++) 2 3 5
{
5 6 9
scanf (“%d”,&a[i][j]);
} 1 4 10
}
printf(“the matrix values are\n”); the matrix values are
for(i=0; i<m ;i++)
{ 2 3 5
for( j=0; j<n; j++) 5 6 9
{
printf (“%d\t”,a[i][j]); 1 4 10
}
printf(“\n”);
}
}
Operations on arrays: programs
Program:
#include <stdio.h>
int main()
{
Int a[20], large, n, i, index ;
printf("Enter size of array\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
large = a[0];
for (i = 1; i < n; i++)
{
if (a[i] > large)
{
large = a[i];
index=i;
}
}
printf("largest element is present at location %d and its value is %d.\n", index, large);
return 0;
}
output:
Enter the number of elements in array
4
Enter 4 integers
6 7 1 2
largest element is present at location 1 and its value is 7.
2. C program to find smallest element in array
Program:
#include <stdio.h>
int main()
{
int array[100], small, n, i, index ;
printf("Enter size of array\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
small = array[0];
for (i = 1; i < n; i++)
{
if (array[i] < small)
{
small = array[i];
index=i;
}
}
printf("smallest element is present at location %d and its value is %d.\n", index, small);
return 0;
}
output:
Enter the number of elements in array
4
Enter 4 integers
6 7 1 2
Smallest element is present at location 2 and its value is 1
3. C program to perform matrix addition
Program:
#include <stdio.h>
int main()
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < m; i++)
for (j = 0 ; j < n; j++)
scanf("%d", &second[i][j]);
printf("Sum of entered matrices:-\n");
for (i = 0; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
sum[i][j] = first[i][j] + second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows and columns of matrix
33
Enter the elements of first matrix
111
111
111
Enter the elements of second matrix
111
111
111
Sum of entered matrices:-
2 2 2
2 2 2
2 2 2
#include <stdio.h>
int main()
{
int m, n, i, j, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of a matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows and columns of a matrix
23
Enter elements of the matrix
123
456
Transpose of the matrix:
1 4
2 5
3 6
5.C program to perform matrix multiplication
Program:
#include <stdio.h>
int main()
{
int R1, C1, R2, C2, i, j, k;
int A[10][10],B[10][10],C[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &R1, &C1);
printf("Enter elements of first matrix\n");
for (i = 0; i < R1; i++)
for (j = 0; j < C1; j++)
scanf("%d", &A[i][j]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &R2, &C2);
if (C1 !=R2 )
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &B[i][j]);
for (i = 0; i < R1; i++)
{
for (j = 0; j < C2; j++)
{
C[i][j]=0;
for (k = 0; k < R2; k++)
{
C[i][j]= C[i][j] +( A[i][k]*B[k][j]);
}
}
}
printf("Product of the matrices:\n");
for (i = 0; i < R1; j++)
{
for (j = 0; j < C2; j++)
{
printf("%d\t", C[i][j]);
}
printf("\n");
}
}
return 0;
}
Output:
Definition:
typedef is a keyword used to create alias name for the existing datatypes.
Syntax:
typedef existing_datatype aliasname
Example:
typedef int number;
number a,b,c;
here number is alias name for integer datatype.
typedef with user defined datatype(structure):
1st method:
struct student
{
char name[20];
int id;
char branch[20] ;
float marks;
};
typedef struct student stu;
int main()
{
stu s;
statements;
}
2nd method:
typedef struct student
{
char name[20];
int id;
char branch[20] ;
float marks;
}stu ;
int main()
{
stu s;
statements;
}
Enumerated Types:
Enumeration is the process of creating user defined datatype by assigning names to integral
constants. There is no such specific use of enum, it is used just to make codes neat and more readable. C
programs can be written without using enumerations also.
Syntax:
enum name { element1,element2…elementN };
Here enum is the keyword
Example:
enum Season {Summer, Spring, Winter, Autumn };
Values of members of enum: All the elements of an enum have a value. By default, the value of the first
element is 0, that of the second element value is previous element value +1 and so on. So in program
instead of using values these elements can be used.
Assigning values:
Program:
#include <stdio.h>
int main()
{
Int a[20], large, n, i, index ;
printf("Enter size of array\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
large = a[0];
for (i = 1; i < n; i++)
{
if (a[i] > large)
{
large = a[i];
index=i;
}
}
printf("largest element is present at location %d and its value is %d.\n", index, large);
return 0;
}
output:
Enter the number of elements in array
4
Enter 4 integers
6 7 1 2
largest element is present at location 1 and its value is 7.
C program to demonstrate Morse code
#include <stdio.h>
void morseCode(char[]);
void morseEncode(char);
int main()
{
char s[15] = "geeksforgeeks";
printf("given string is %s\n",s);
printf("the morse code og given string is\n");
morseCode(s);
return 0;
}
void morseEncode(char x)
{
switch (x)
{
case 'a':
printf(".-");
break;
case 'b':
printf("-...");
break;
case 'c':
printf("-.-.");
break;
case 'd':
printf("-..");
break;
case 'e':
printf(".");
break;
case 'f':
printf("..-.");
break;
case 'g':
printf("--.");
break;
case 'h':
printf("....");
break;
case 'i':
printf("..");
break;
case 'j':
printf(".---");
break;
case 'k':
printf("-.-");
break;
case 'l':
printf(".-..");
break;
case 'm':
printf("--");
break;
case 'n':
printf("-.");
break;
case 'o':
printf("---");
break;
case 'p':
printf(".--.");
break;
case 'q':
printf("--.-");
break;
case 'r':
printf(".-.");
break;
case 's':
printf("...");
break;
case 't':
printf("-");
break;
case'u':
printf("..-");
break;
case 'v':
printf("...-");
break;
case 'w':
printf(".--");
break;
case 'x':
printf("-..-");
break;
case 'y':
printf("-.--");
break;
case 'z':
printf("--..");
break;
}
}
void morseCode(char s[])
{
int i;
for (i=0;s[i]!='\0';i++)
morseEncode(s[i]);
}
Output:
Refer: