C Programming · Unit 6 · 6 hrs
Arrays
Exam-focused notes for Arrays (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 10 solved past questions from this unit.
Arrays in short
This chapter introduces the array as a way of grouping many values of the same type under one name, and the character array, or string, as C's way of representing text.
What this unit covers
- Introduction to Array
- Types of Array (Single Dimensional and Multidimensional)
- Declaration and Memory Representation of Array
- Initialization of array
- Character Array and Strings
- Reading and Writing Strings
- Null Character
- String Library Functions (string length, string copy, string concatenation, string compare)
Introduction to Array
2081 · 10 marks
What are the characteristics of array?Write a program to input age of 500 persons and display the following: a) Average age b) Age between 25 to 30[2+8]
Show solved answerHide answer
Characteristics of Array and Program for Age Analysis
Characteristics of Array [2 marks]
An array is a collection of similar data type elements stored in contiguous memory locations. Its main characteristics are:
- Homogeneous Elements: All elements in an array must be of the same data type (e.g., all int, all float).
- Contiguous Memory Allocation: Array elements are stored in consecutive memory locations.
- Index/Subscript Based Access: Each element is accessed using an index starting from 0 (lower bound = 0, upper bound = size - 1).
- Fixed Size: The size of an array is fixed at the time of declaration and cannot be changed during execution.
- Random Access: Any element can be directly accessed using its index in constant time.
- Single Variable Name: Multiple values are stored under one variable name with different indices.
Program to Input Age of 500 Persons [8 marks]
#include<stdio.h>
#include<conio.h>
void main()
{
int age[500], i;
int count = 0; /* count of persons with age between 25 to 30 */
float sum = 0, average;
clrscr();
/* Input age of 500 persons */
for(i = 0; i < 500; i++)
{
printf("Enter age of person %d: ", i + 1);
scanf("%d", &age[i]);
}
/* Calculate sum and count persons with age between 25 to 30 */
for(i = 0; i < 500; i++)
{
sum = sum + age[i];
if(age[i] >= 25 && age[i] <= 30)
{
count++;
}
}
/* a) Calculate average age */
average = sum / 500;
printf("\n--- Results ---\n");
/* Display average age */
printf("\na) Average Age = %.2f", average);
/* b) Display ages between 25 to 30 */
printf("\n\nb) Ages between 25 to 30:\n");
for(i = 0; i < 500; i++)
{
if(age[i] >= 25 && age[i] <= 30)
{
printf("Person %d : %d years\n", i + 1, age[i]);
}
}
printf("\nTotal persons with age between 25 to 30: %d", count);
getch();
}
Explanation of the Program
| Step | Description |
|---|---|
| Declaration | int age[500] declares an integer array of size 500 to store ages |
| Input Loop | A for loop runs from i=0 to i<500 to input all 500 ages |
| Sum Calculation | Each age is added to sum to find total |
| Condition Check | age[i] >= 25 && age[i] <= 30 checks if age falls in the range |
| Average | average = sum / 500 gives the average age |
| Output | Average is displayed and ages in range 25-30 are printed with person number |
Sample Output
--- Results ---
a) Average Age = 35.46
b) Ages between 25 to 30:
Person 3 : 27 years
Person 7 : 25 years
Person 12 : 30 years
...
Total persons with age between 25 to 30: 85
2079 · 5 marks
Write a program to find the second largest number in the given array of numbers. [5]
2075 · 10 marks
Define array?What are the benefits of using array?Write a program to add two matrices using array.[1+2+7]
Show solved answerHide answer
Array: Definition, Benefits, and Matrix Addition Program
1. Definition of Array [1 mark]
An array is a collection of similar (same data type) elements stored in contiguous memory locations, referred to by a single variable name and accessed using an index (subscript).
Syntax:
data_type array_name[size];
Example:
int marks[5]; /* declares an array of 5 integers */
2. Benefits of Using Array [2 marks]
| Benefit | Explanation |
|---|---|
| Organized Storage | Multiple values of the same type are stored under one variable name, keeping data organized. |
| Easy Access | Any element can be directly accessed using its index, e.g., arr[2]. |
| Less Code | Instead of declaring many separate variables, a single array declaration handles all values. |
| Loop Friendly | Arrays work efficiently with loops to process large amounts of data quickly. |
| Supports 2D Data | Two-dimensional arrays allow representation of matrices and tables naturally. |
3. Program to Add Two Matrices Using Array [7 marks]
Logic:
- Take two matrices A and B of the same size (r x c).
- Add corresponding elements:
C[i][j] = A[i][j] + B[i][j] - Display the resultant matrix C.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10], B[10][10], C[10][10];
int i, j, r, c;
clrscr();
/* Input: number of rows and columns */
printf("Enter number of rows: ");
scanf("%d", &r);
printf("Enter number of columns: ");
scanf("%d", &c);
/* Input elements of Matrix A */
printf("\nEnter elements of Matrix A:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
/* Input elements of Matrix B */
printf("\nEnter elements of Matrix B:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("B[%d][%d] = ", i, j);
scanf("%d", &B[i][j]);
}
}
/* Addition of two matrices */
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
/* Display the resultant matrix C */
printf("\nResultant Matrix after Addition:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%d\t", C[i][j]);
}
printf("\n");
}
getch();
}
Sample Output:
Enter number of rows: 2
Enter number of columns: 2
Enter elements of Matrix A:
A[0][0] = 1
A[0][1] = 2
A[1][0] = 3
A[1][1] = 4
Enter elements of Matrix B:
B[0][0] = 5
B[0][1] = 6
B[1][0] = 7
B[1][1] = 8
Resultant Matrix after Addition:
6 8
10 12
Explanation of Key Steps:
| Step | Description |
|---|---|
int A[10][10] | Declares a 2D array to store matrix elements |
Nested for loop for input | Reads each element row by row using scanf |
C[i][j] = A[i][j] + B[i][j] | Adds corresponding elements of both matrices |
Nested for loop for output | Displays the result matrix in proper row-column format using printf and \t |
2074 · 5 marks
Write a program to find sum and average of 10 integer numbers stored in an array. [5]
Types of Array
2080 · 5 marks
Write a program to read P*Q matrix of integers and find the largest integer of each row and display it. [5]
2077 · 10 marks
What are the benefits of using arrays?Compare one dimensional array with two dimensional array.Write a program to find transpose of a matrix.[2+2+6]
Character Array and Strings
2080 · 5 marks
Write a program to check whether the entered word is palindrome or not. [5]
Initialization of array
2080 · 5 marks
Write a program to initialize an array of dimension 10 and sort the numbers within the array in ascending order. [5]
String Library Functions
2078 · 10 marks
Explain any three string functions.Write a program to check if two matrices are identical or not.[3+7]
Make Unit 6 stick
Practice CSC115 with flashcards & quizzes