0% found this document useful (0 votes)
5 views

CPC - Module - 4

The document discusses arrays and strings in C programming. It explains what arrays are, how to declare and initialize one-dimensional and two-dimensional arrays, and provides examples. It also discusses sorting arrays, multiplying matrices, and other practical questions often asked in university exams.

Uploaded by

Mahi Gawde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CPC - Module - 4

The document discusses arrays and strings in C programming. It explains what arrays are, how to declare and initialize one-dimensional and two-dimensional arrays, and provides examples. It also discusses sorting arrays, multiplying matrices, and other practical questions often asked in university exams.

Uploaded by

Mahi Gawde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Programming in C - Pratiksha A.

ARRAYS AND STRINGS

Introduction to Arrays:

- An array is a fixed-size sequenced collection of elements of the same data type.


- These are stored at contiguous memory locations.
- It can be used to represent a list of numbers or names.
- Examples of array can include:
- list of employees in an organization
- test scores of a class of students
- calculation of matrices data
- list of products and their cost

A.
-
Syntax of Array Declaration:

datatype arrayName[size] = {list of values};

a
Example:

#include <stdio.h>
sh
int main() {
// Declare an array of integers with 5 elements
int numbers[5] = {1, 2, 3, 4, 5};

// Print the elements of the array


ik
printf("Elements of the array:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
at

return 0;
}
Pr

Initialization of One-Dimensional & Two-Dimensional Array:

- 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:

datatype arrayName[row_size][column_size] = {list of values};

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}};

// Accessing and printing elements of the 2-D array


printf("Elements of the 2-D array:\n");
for (int i = 0; i < 3; i++) { //row
for (int j = 0; j < 3; j++) { //column

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);

printf("\n\nEnter elements of matrix A: \n");


for(int i = 0; i < arows; i++)
{
for(int j = 0; j < acolumns; j++)
{

2
Computer Programming in C - Pratiksha A.

scanf("%d",&a[i][j]);
}
}

//to save from having uneven matrices


if(brows != acolumns)
{
printf("Cannot multiply");
}
else
{
printf("\n\nEnter elements of matrix B: \n");
for(int i = 0; i < brows; i++)

A.
{
for(int j = 0; j < bcolumns; j++)
{
scanf("%d",&b[i][j]);
}
}
}

a
printf("\n");

//resultant matrix calculation


sh
for(int i = 0; i < arows; i++)
{
for(int j = 0; j < bcolumns; j++)
{
for(int k = 0; k < brows; k++)
{
ik
printf("(%d * %d) + ", a[i][k], b[k][j]);
sum += a[i][k] * b[k][j];
}
product[i][j] = sum;
at

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:

1. WAP to arrange elements of an array in Ascending order.


2. WAP to arrange elements of an array in Descending order.
3. WAP to delete an element from the array.
4. WAP to find Standard Deviation and Variance using Arrays.
5. WAP to add two matrices.
6. WAP to multiply two matrices.
7. WAP to find the transpose of a matrix.

A.
University Asked Questions:

Question Paper Marks

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

Q.3. a) WAP to find the transpose of a matrix without 10


making use of another matrix.

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}.

Q.5. b) Write a program to calculate the sum of diagonal 10


elements of a matrix.

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

Q.3. D) Explain compile time and run time initialization of 4


an array with proper example.

Q.3. E) Write a program to accept elements of two 4


dimensional square matrix and check whether matrix is
Pr

symmetric or not.

Dec’2022:
Q.3. B) WAP in C to find average of N elements entered 5
by user using an array.

Q.6. A) WAP to check whether square matrix is 6


symmetric or not.

May’2023:
5

5
Computer Programming in C - Pratiksha A.

Q.2. B) Write a program to sort the elements of one


dimensional array in ascending order.

Q.6. A) Write a program to find transpose of a square 5


matrix using only one matrix.

A.
a
sh
ik
at
Pr

You might also like