0% found this document useful (0 votes)
41 views35 pages

Lab 4 - 221012 - 095918

1. The documents contain C code for performing various operations on arrays and matrices including: inputting/outputting elements, counting positive/negative/even/odd elements, copying arrays, inserting elements into arrays at a given position, calculating sums of diagonals and rows/columns of matrices, and checking if a matrix is upper triangular. 2. The code examples demonstrate basic array and matrix manipulation in C including using for loops to iterate over elements, if/else conditional statements to check conditions on elements, and functions like scanf() and printf() for input/output. 3. The examples cover a range of common array/matrix tasks to help learn fundamental C programming concepts for working with these data structures.

Uploaded by

Mihai Lipcan
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)
41 views35 pages

Lab 4 - 221012 - 095918

1. The documents contain C code for performing various operations on arrays and matrices including: inputting/outputting elements, counting positive/negative/even/odd elements, copying arrays, inserting elements into arrays at a given position, calculating sums of diagonals and rows/columns of matrices, and checking if a matrix is upper triangular. 2. The code examples demonstrate basic array and matrix manipulation in C including using for loops to iterate over elements, if/else conditional statements to check conditions on elements, and functions like scanf() and printf() for input/output. 3. The examples cover a range of common array/matrix tasks to help learn fundamental C programming concepts for working with these data structures.

Uploaded by

Mihai Lipcan
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
You are on page 1/ 35

1.2.

#include <stdio.h> #define


MAX_SIZE 100 //
Maximum array size int
main() { int
arr[MAX_SIZE]; // Declare
array of MAX_SIZE int i,
N; /* Input size of the array
*/ printf("Enter size of the
array : "); scanf("%d", &N);
/* Input elements in the
array */ printf("Enter
elements in array : ");
for(i=0; i<N; i++)
{ scanf("%d", &arr[i]); }
printf("\nAll negative
elements in array are : ");
for(i=0; i<N; i++) { /* If
current array element is
negative */ if(arr[i] < 0)
{ printf("%d\t", arr[i]); } }
return 0; }

2.6.
#include <stdio.h> #define
MAX_SIZE 100
//Maximum size of the
array int main() { int
arr[MAX_SIZE]; int i, size,
even, odd; /* Input size of
the array */ printf("Enter
size of the array: ");
scanf("%d", &size); /*
Input array elements */
printf("Enter %d elements
in array: ", size); for(i=0;
i<size; i++) { scanf("%d",
&arr[i]); } /* Assuming that
there are 0 even and odd
elements */ even = 0; odd
= 0; for(i=0; i<size; i++) { /*
If the current element of
array is even then
increment even count */
if(arr[i]%2 == 0) { even+
+; } else { odd++; } }
printf("Total even
elements: %d\n", even);
printf("Total odd elements:
%d", odd); return 0; }
3.7
#include <stdio.h> #define
MAX_SIZE 100 //
Maximum
array size int main() { int
arr[MAX_SIZE]; //
Declares array of size 100
int i, size, count = 0; /*
Input size of array */
printf("Enter size of the
array : "); scanf("%d",
&size); /* Input array
elements */ printf("Enter
elements in array : ");
for(i=0; i<size; i++)
{ scanf("%d", &arr[i]); } /* *
Count total negative
elements in array */
for(i=0; i<size; i++) { /*
Increment count if current
array element is negative
*/ if(arr[i] < 0) { count++; } }
printf("\nTotal negative
elements in array = %d",
count); return 0; }

4.8
#include <stdio.h> #define
MAX_SIZE 100 int main()
{ int source[MAX_SIZE],
dest[MAX_SIZE]; int i,
size; /* Input size of the
array */ printf("Enter the
size of the array : ");
scanf("%d", &size); /*
Input array elements */
printf("Enter elements of
source array : "); for(i=0;
i<size;
i++) { scanf("%d",
&source[i]); } /* * Copy all
elements from source
array to dest array */
for(i=0; i<size; i++) { dest[i]
= source[i]; } /* * Print all
elements of source array
*/ printf("\nElements of
source array are : ");
for(i=0; i<size; i++)
{ printf("%d\t",
source[i]); } /* * Print all
elements of dest array */
printf("\nElements of dest
array are : "); for(i=0;
i<size; i++) { printf("%d\t",
dest[i]); } return 0; }
5.9 #include <stdio.h>
#define MAX_SIZE 100 int
main() { int
arr[MAX_SIZE]; int i, size,
num, pos; /* Input size of
the array */ printf("Enter
size of the array : ");
scanf("%d", &size); /*
Input elements in array */
printf("Enter elements in
array : "); for(i=0; i<size; i+
+) { scanf("%d",
&arr[i]); } /* Input new
element and position to
insert */ printf("Enter
element to insert : ");
scanf("%d", &num);
printf("Enter the element
position : "); scanf("%d",
&pos); /
* If position of element is
not valid */ if(pos > size+1
|| pos <= 0) { printf("Invalid
position! Please enter
position between 1 to %d",
size); } else { /* Make
room for new array
element by shifting to right
*/ for(i=size; i>=pos; i--)
{ arr[i] = arr[i-1]; } /* Insert
new element at given
position and increment
size */ arr[pos-1] = num;
size++

II.
1.6
#include <stdio.h> #define
SIZE 3 // Matrix size int
main() { int A[SIZE][SIZE];
int row, col, sum = 0; /*
Input elements in matrix
from user */ printf("Enter
elements in matrix of size
%dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE;
row++) { for(col=0;
col<SIZE; col++)
{ scanf("%d", &A[row]
[col]); } } /* Find sum of
main diagonal elements */
for(row=0; row<SIZE;
row++) { sum = sum +
A[row][row]; } printf("\
nSum of main diagonal
elements = %d", sum);
return 0; }
2.7
#include <stdio.h> #define
SIZE 3 // Matrix size int
main() { int A[SIZE][SIZE];
int row, col, sum = 0; /*
Input elements in matrix
from user */ printf("Enter
elements in matrix of size
%dx%d: \n"); for(row=0;
row<SIZE; row++)
{ for(col=0; col<SIZE; col+
+) { scanf("%d", &A[row]
[col]); } } /* Find sum of
minor diagonal elements */
for(row=0; row<SIZE;
row++) { for(col=0;
col<SIZE; col++) { /* * If it
is minor diagonal of
matrix * Minor diagonal: i+j
== N + 1 * Since array
elements starts from 0
hence i+j == (N + 1)-2 */
if(row+col == ((SIZE+1)-
2)) { sum += A[row][col]; } }
} printf("\nSum of minor
diagonal elements = %d",
sum); return 0; }
3.8
#include <stdio.h> #define
SIZE 3 // Matrix size int
main() { int A[SIZE][SIZE];
int row, col, sum = 0; /*
Input elements in matrix
from user */ printf("Enter
elements in matrix of size
%dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE;
row++) { for(col=0;
col<SIZE; col++)
{ scanf("%d", &A[row]
[col]); } } /* Calculate sum
of elements of each row of
matrix */ for(row=0;
row<SIZE; row++) {
sum = 0; for(col=0;
col<SIZE; col++) { sum +=
A[row][col]; } printf("Sum
of elements of Row %d =
%d\n", row+1, sum); } /*
Find sum of elements of
each columns of matrix */
for(row=0; row<SIZE;
row++) { sum = 0;
for(col=0; col<SIZE; col+
+) { sum += A[col][row]; }
printf("Sum of elements of
Column %d = %d\n",
row+1, sum); } return 0; }
4.9
int main() { int
A[MAX_ROWS]
[MAX_COLS]; int row, col,
size, temp; /* Input
elements in matrix from
user */ printf("Enter
elements in matrix of size
%dx%d: \n", MAX_ROWS,
MAX_COLS); for(row=0;
row<MAX_ROWS; row++)
{ for(col=0;
col<MAX_COLS; col++)
{ scanf("%d", &A[row]
[col]); } } size =
(MAX_ROWS <
MAX_COLS) ?
MAX_ROWS :
MAX_COLS; /* *
Interchange
diagonal of the matrix */
for(row=0; row<size; row+
+) { col = row; temp =
A[row][col]; A[row][col] =
A[row][(size-col) - 1];
A[row][(size-col) - 1] =
temp; }
printf("\nMatrix after
diagonals interchanged: \
n"); for(row=0;
row<MAX_ROWS; row++)
{ for(col=0;
col<MAX_COLS; col++)
{ printf("%d ", A[row]
[col]); } printf("\n"); } return
0; }

5.10
#include <stdio.h> #define
MAX_ROWS 3 #define
MAX_COLS 3 int main()
{ int array[MAX_ROWS]
[MAX_COLS]; int row, col,
isUpper; /* Input elements
in matrix from user */
printf("Enter elements in
matrix of size %dx%d: \n",
MAX_ROWS,
MAX_COLS); for(row=0;
row<MAX_ROWS; row++)
{ for(col=0;
col<MAX_COLS; col++)
{ scanf("%d", &array[row]
[col]); } } /* Check Upper
triangular matrix condition
*/ isUpper = 1; for(row=0;
row<MAX_ROWS; row++)
{ for(col=0;
col<MAX_COLS; col++)
{ /* * If elements below the
main diagonal (col<row) *
is not equal to zero then it
is not upper triangular
matrix */ if(col<row &&
array[row][col]!=0)
{ isUpper = 0; } } } /* Print
elements of upper
triangular matrix */
if(isUpper == 1) { printf("\
nThe matrix is Upper
triangular matrix.\n");
for(row=0;
row<MAX_ROWS; row++)
{ for(col=0;
col<MAX_COLS; col++)
{ printf("%d ", array[row]
[col]); }
printf("\n"); } } else
{ printf("\nThe matrix is not
Upper triangular
matrix."); } return 0; }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy