0% found this document useful (0 votes)
37 views10 pages

Hand 6

The document contains examples of C code functions for performing operations on one and two dimensional arrays. These include initializing arrays to zero, allowing user input of array elements, finding minimum/maximum values, determining if an element exists, copying arrays, and reversing arrays. For two dimensional arrays, it shows functions to initialize to zero, allow user input, display all elements, count negative elements, and print the main diagonal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views10 pages

Hand 6

The document contains examples of C code functions for performing operations on one and two dimensional arrays. These include initializing arrays to zero, allowing user input of array elements, finding minimum/maximum values, determining if an element exists, copying arrays, and reversing arrays. For two dimensional arrays, it shows functions to initialize to zero, allow user input, display all elements, count negative elements, and print the main diagonal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ma. Oliva Ross T.

Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
EXERCISE - 1 Dimensional Array

1. Write a function that will initialize a double data array elements to 0.0. Pass the
array and a size identified by an integer n as parameters.

#include<stdio.h>

void InitializeArray(double a[])


{
int i;

for(i=0;i<5;i++)
{
printf("Value of a[%d] is %.3f\n", i,a[i]);
}
}

void main()
{
double a[5] = {1.2,1.4,1.6,1.8,2.0};
int i;

InitializeArray(a);
}

2.Same as the problem above, but allow the user to input the value via scanf()
statement.

#include<stdio.h>

void InitializeArray(double a[], int n)


{
int i;

for(i=0;i<n;i++)
{
scanf("%lf", &a[i]);
}
}

void main(void)
{
double a[7];
int i, n;

printf("Enter size of array:");


scanf("%d", &n);

printf("\nEnter %d elements\n", n);


InitializeArray(a, n);

printf("\nArray values:\n");

for(i=0;i<n;i++)
{
printf("a[%d] = %.3f\n", i,a[i]);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
}
}

3. Write a function that will return the lowest value in a integer data type array.
Pass the array and a size identified by an integer n as parameters.

#include<stdio.h>

int getSmallest(int arr[], int size)


{
int i=0, smallest =0;

smallest = arr[0];

for (i=1; i<size; i++)


{
if (arr[i]< smallest)
smallest = arr [i];
}

return smallest;
}

int main()
{
int arr[10];
int i = 0;

printf("\nEnter elements:\n");

for (i=0; i<10; i++)


{
printf("Enter arr[%d] :", i);
scanf("%d", &arr [i]);
}

printf("\nSmallest element of array is : %d\n", getSmallest(arr,10));

4. Write a function that will return the sum of all the values in a double data type
array. Pass the array and a size identified by an integer n as parameters.

5. Write a function that will return the count of negative values in an integer data
type array. Pass the array and a size identified by an integer n as parameters.

#include<stdio.h>

int main()
{
int n, i, j[20], count=0, count1=0;

printf("Enter the number of elements:\n");


scanf("%d", &n);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
for (i=0; i<n; i++)
{
printf("Enter values\n");
scanf("%d", &j[i]);

if (j[i]<0)
count++;
else
count1++;
}

printf("Number of negative numbers entered is %d", count);

return count;

6. Write a function that will determine whether an integer say x exists or not in an
integer array. The function should return 1 if x is in the array, otherwise, it
should return 0. Pass the array, a size identified by n, and the integer x as
parameters.

#include<stdio.h>
#define MAX 20

int findElement (int arr[], int n, int item)


{
int i;
for (i=0; i<n; i++)
{
if (arr[i]==item)
return 1;
else
return 0;
}
}

int main()
{
int arr[MAX];
int n, i, item;

printf("\nEnter size of an Array:");


scanf("%d", &n);

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

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


{
printf("Enter %d element:", i+1);
scanf("%d", &arr[i]);
}

printf("Enter an item to find:");


scanf("%d", &item);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
findElement(arr, n, item);
}

7.Let A and B be two arrays of integers. Assume that A has been initialized. Write a
function that will copy the contents of A to array B. Pass the two arrays and a size
identified by n as parameters.

#include<stdio.h>

int main ()
{
int arrA[30], arrB[30], i, num;

printf("\nEnter no. of elements:");


scanf("%d", &num);

printf("\nEnter the values:");


for (i=0; i<num; i++)
{
scanf("%d", &arrA[i]);
}

for (i=0; i<num; i++)


{
arrB[i]=arrA[i];
}

printf("\nElements of Array A:\n");


for (i=0;i<num;i++)
printf("%5d", arrA[i]);

printf("\nElements of Array B:\n");


for (i=0;i<num;i++)
printf("%5d", arrB[i]);

return 0;
}

8.Same as in the problem above, but the contents of B should be in reverse order as
that of array A, i.e., the first element of A should appear as the last element of B,
the second element of A should appear as the second to the last element of B and so
on. Pass the two arrays and a size identified by n as parameters.

#include<stdio.h>
#define MAX 20

void readArray(int a[], int size)


{
int i;
for (i=0; i<size; i++)
{
printf("Enter %d element:", i+1);
scanf("%d", &a[i]);
}
}
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
void printArray(int a[], int size)
{
int i;
for (i=0; i<size;i++)
printf("%5d", a[i]);
}

void reverseArray(int a[], int size)


{
int i, temp;
for (i=0; i<size/2; i++)
{
temp=a[i];
a[i]=a[((size -1)-i)];
a[((size-1)-i)]=temp;
}
}

int main()
{
int arr[MAX];
int n;

printf("\nEnter size of an Array:");


scanf("%d", &n);

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


readArray(arr,n);

printf("\nElements of Array A:\n");


printArray(arr, n);

reverseArray(arr,n);

printf("\nElements of Array B:\n");


printArray(arr, n);

printf("\n\n");
return 0;
}

9.Write a function that will return 1 if two character arrays A and B have the same
array elements, otherwise it should return 0. Assume that A and B have been
initialized. Pass the arrays and a size identified by n as parameters.

void AB();
#include<stdio.h>
#include<conio.h>

void main()
{
AB();
getch();
}

void AB(void)
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
{
char x,y,z;
char A[3] = {x,y,z};
char B[3];

A[3] = B[3];

if (A[3] = B[3]){
printf("Same array\n");
}
else{
printf("Not the same\n");
}
getch();
}

EXERCISE - 2 Dimensional Array

1.Write a function that will initialize the contents of a double data type 2D array to
0.0. Pass the array as parameter.

#include<stdio.h>
#include<conio.h>

void main()
{
double a[3][3];
int i,j;
clrscr();
printf("Enter value: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%lf", &a[i][j]);
}
}
printf("\n The matrix is ::: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.1lf ", a[i][j]);
}
printf("\n");
}
getch();
}

2.Same as the problem above, but allow the user to input the value of the array
element via scanf() statement.

#include<stdio.h>
#include<conio.h>

void main(){
double a[10][10];
int n,i,j;
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
clrscr();
printf("Enter N: \n");
scanf("%d",&n);
printf("Enter %d*%d matrix\n", n,n);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%lf\t", &a[i][j]);
}
}
printf("\n The matrix is ::: \n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%.1lf ", a[i][j]);
}
printf("\n");
}
getch();
}

3.Write a function that will display all the elements of the 2D array. Elements
should be printed starting from the first row up to the last row, and from the first
column to the last column within the same row. Elements of the same row should be
printed on the same line separated by two spaces. Pass the array as parameter.

(The same with no. 2)

4.Write a function that will return the number of negative elements in the 2D array.
Pass the array as parameter.

#include<stdio.h>
#include<conio.h>

void main()
{
double m[10][10];
int i,j, pos=0, neg=0, zero=0, n;
clrscr();
printf("How many elements? \n");
scanf("%d", &n);
printf("Enter %d*%d matrix elements \n",n,n);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%lf ", &m[i][j]);
if(m[i][j]>0){
pos++;
}
else if (m[i][j]<0){
neg++;
}
else{
zero++;
}
}
}
printf("\n positive elements = %d\n", pos);
printf("\n negative elements = %d\n", neg);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
printf("\n zero elements = %d\n", zero);

getch();
}

5.Write a function that will print only the elements on the main diagonal of the 2D
array. Pass the array as parameter.

#include<stdio.h>

void main()
{
double array1[10][10];
int i,j,m,n,sum = 0;

printf("Enter no. of rows :: ");


scanf("%d", &m);
printf("\nEnter no. of cols :: ");
scanf("%d",&n);
printf("\nEnter values to the matrix :: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter a[%d][%d] value :: ",i,j);
scanf("%lf", &array1[i][j]);
}
}
printf("\nThe Diagonals elements of a matrix are :: \n\n");
if(m==n)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
printf("\t%.1lf", array1[i][j]);
else
printf("\t");
}
printf("\n\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}
}
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
6.Write a function that will return the sum of the elements on a specified row.
Pass the array and the row as parameters.

7.Write a function that will return the sum of the elements on a specified column.
Pass the array and the column as parameters.

(for no. 6 & 7)


#include <stdio.h>

void main ()
{
double array[10][10];
int i, j, m, n;
double sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%lf", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %.1lf\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %.1lf\n", j, sum);
sum = 0;
}
}

8.Assume three matrices of the same size, say matrix A, B and C. Write a function
that will add the two matrices A and B and store the sum to C. Matrix addition is
done component-wise, i.e., C[i ,j] = A[i, j] + B[i, j] where i, j are the row and
column index respectively. Pass the three arrays as parameters.

#include <stdio.h>

int main()
{
int m, n, c, d;
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 6
AE 713
double first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++){
for (d = 0; d < n; d++){
scanf("%lf", &first[c][d]);
}
}
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++){
for (d = 0 ; d < n; d++){
scanf("%lf", &second[c][d]);
}
}
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%.1lf\t", sum[c][d]);
}
printf("\n");
}
return 0;
}

You might also like