0% found this document useful (0 votes)
6 views4 pages

C Programs for Arrays and Matrices

The document contains several C programming examples demonstrating the use of arrays, including taking user input to store integers, calculating the average of numbers, performing matrix addition with multidimensional arrays, and printing elements of a three-dimensional array. It highlights the advantages of multidimensional arrays for structured data representation, such as in graphics and student marks. Each example includes code snippets and explanations of the functionality.

Uploaded by

vickykumawat1221
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

C Programs for Arrays and Matrices

The document contains several C programming examples demonstrating the use of arrays, including taking user input to store integers, calculating the average of numbers, performing matrix addition with multidimensional arrays, and printing elements of a three-dimensional array. It highlights the advantages of multidimensional arrays for structured data representation, such as in graphics and student marks. Each example includes code snippets and explanations of the functionality.

Uploaded by

vickykumawat1221
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) First C Program to take values from the user and store them in an

array Print the elements stored in the array

#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; i++) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");

// printing elements of the array


for(int i = 0; i < 5; i++) {
printf("%d\n", values[i]);
}
return 0;
}

Example 2: Calculate Average

// Program to find the average of n numbers using arrays

#include <stdio.h>

int main() {

int marks[10], i, n, sum = 0;


double average;

printf("Enter number of elements: ");


scanf("%d", &n);

for(i=0; i < n; i++) {


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum += marks[i];
}

// explicitly convert the sum to double


// then calculate average
average = (double) sum / n;

printf("Average = %.2lf", average);

return 0;
}

3)C Program on multidimensional array to perform matrix addition

#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

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


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

4)Multidimensional array:

int arr[2][3][4];

2-Number of tables
3-Rows per table
4-Columns per row

#include <stdio.h>
int main() {
int arr[2][2][3] = {
{ {1, 2, 3}, {4, 5, 6} },
{ {7, 8, 9}, {10, 11, 12} }
};

// Printing all elements


for (int i = 0; i < 2; i++) { // table
for (int j = 0; j < 2; j++) { // row
for (int k = 0; k < 3; k++) { // column
printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
}
}
}
return 0;
}
Advantage of multi dimensional array:
Images/volumes in graphics (x, y, z coordinates).

Storing marks of students in different subjects across semesters.

Helps to represent data in a structured way (layers → rows → columns).

You might also like