CPC - Module - 4
CPC - Module - 4
Introduction to Arrays:
A.
-
Syntax of Array Declaration:
a
Example:
#include <stdio.h>
sh
int main() {
// Declare an array of integers with 5 elements
int numbers[5] = {1, 2, 3, 4, 5};
return 0;
}
Pr
- 1-D Array: are the arrays that contain values in a single row.
- They are also known as single arrays.
- Each element in 1-D is accessed by its index value.
- Syntax and Example for this are the same as above.
2-D Array: are the arrays that represent a matrix with rows and columns.
- it stores elements in a grid-like format, where each element is identified by its row and
column indices.
1
Computer Programming in C - Pratiksha A.
-
Syntax of 2-D Array Declaration:
Example:
#include <stdio.h>
int main() {
// Declaration and initialization of a 2-D array of
integers
int matrix[3][3] = {{1, 2, 3},
A.
{4, 5, 6},
{7, 8, 9}};
a
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next row
sh
}
return 0;
}
ik
Product of 2-D Arrays (2 matrices):
#include<stdio.h>
at
int main()
{
int a[3][3], b[3][3], product[3][3];
int arows = 3, acolumns = 3, brows = 3, bcolumns =3;
Pr
int sum = 0;
//part 1
printf("Value for A rows and Columns is: %d &
%d\n",arows,acolumns);
printf("Value for B rows and Columns is: %d &
%d",brows,bcolumns);
2
Computer Programming in C - Pratiksha A.
scanf("%d",&a[i][j]);
}
}
A.
{
for(int j = 0; j < bcolumns; j++)
{
scanf("%d",&b[i][j]);
}
}
}
a
printf("\n");
sum = 0;
printf("= %d\n",product[i][j]);
}
}
Pr
//resultant matrix
printf("\n\n");
for(int i = 0; i < arows; i++)
{
for(int j = 0; j < bcolumns; j++)
{
printf("%d ",product[i][j]);
}
printf("\n");
}
return 0;
}
3
Computer Programming in C - Pratiksha A.
Practical Questions:
A.
University Asked Questions:
a
May’2018:
Q.4. a) WAP using recursion to find sum of array of 5 (it is the 2nd part of
sh the 10 mark question)
elements of size n.
Q.4. b) 10
i) Create a 2D array (Matrix) [in main function]
ii) Write a function to read 2D array (Matrix)
ik
iii) Write a function that will return true(1) if the entered
matrix is symmetric or false (0) is not symmetric.
iv) Print whether the entered matrix is symmetric or not
[in main function].
at
Dec’2018:
Q.2. a) WAP to multiply 2 matrices after checking 10
compatibility.
Pr
May’2019: 10
Q.3. a) Write a program to find the frequency of digits in a
set of numbers and remove duplicates from an array. For
ex. an array A = {1,2,3,4,2,5,2} frequency of 2 is 3 and
the resultant array is A = {1,2,3,4,5}.
4
Computer Programming in C - Pratiksha A.
For ex:
123
598
147
Calculate the sum of diagonal elements = 3 + 9 + 1 + =
13
Dec’2019:
Q.1. a) iii) What will happen if in a C program you assign 1
a value to an array element whose subscript exceeds the
size of array?
A.
(a)The element will be set to 0
(b) The compiler would report an error
(c) The program may crash if some important data gets
overwritten
(d)The array size would appropriately grow.
a
Q.3. a) What is an array? What does an array name 10
signify? Can array index be negative? Write
sh
a c program to arrange the number stored in an array in
such a way that the array will have the odd numbers
followed by even numbers.
May’2022:
ik
Q.2. F) Write a program to accept elements of one
dimensional array from user and sort and display them in 4
ascending order.
at
symmetric or not.
Dec’2022:
Q.3. B) WAP in C to find average of N elements entered 5
by user using an array.
May’2023:
5
5
Computer Programming in C - Pratiksha A.
A.
a
sh
ik
at
Pr