Basic programs using array
(1) Insert and display values
#include <stdio.h>
int main() {
int arr[10];
// Input 10 values into the array
printf("Enter 10 values: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Display the values stored in the array
printf("The values in the array are: ");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
(2)Program to display sum of elements in an array
#include <stdio.h>
int main() {
int arr[10];
int sum = 0;
// Input 10 values into the array
printf("Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Calculate the sum of the elements in the array
for (int i = 0; i < 10; i++) {
sum += arr[i];
}
// Display the sum
printf("The sum of the elements is: %d\n", sum);
return 0;
}
……………………………………………………………………………………………
(The following programs are to perform in the lab and to take print)
AIM : To study 1D and 2D arrays
Theory:(write on ruled sheet)
What is an array?
Use of array
1D array declaration syntax
2D array declaration sytax
Problem Statement:
Q1. Write a program to find the maximum and minimum values in a 1D array of integers.
#include <stdio.h>
int main() {
int arr[10],i;
int max_val , min_val;
// Input the elements of the array
printf("Enter 10 elements: ");
for ( i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Initialize max and min values with the first element of the array
max_val = arr[0];
min_val = arr[0];
// Iterate through the array to find max and min
for (i = 1; i < 10; i++) {
if (arr[i] > max_val) {
max_val = arr[i];
}
if (arr[i] < min_val) {
min_val = arr[i];
}
}
// Output the results
printf("Maximum value: %d\n", max_val);
printf("Minimum value: %d\n", min_val);
return 0;
}
Problem Statement:
Q2. Create a program to calculate the transpose of a 2D matrix.
#include <stdio.h>
int main() {
int rows, cols;
// Input the dimensions of the matrix
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols], transpose[cols][rows];
// Input the matrix elements
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// Calculate the transpose
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Print the original matrix
printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Print the transposed matrix
printf("\nTransposed Matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Problem Statement:
Q3. Develop a program to sort an array of integers in ascending order using bubble
sort.
#include <stdio.h>
int main() {
int arr[10];
// Input the elements of the array
printf("Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort to sort the array in ascending order
for (int i = 0; i < 10 - 1; i++) {
for (int j = 0; j < 10 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Output the sorted array
printf("Sorted array in ascending order: ");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}