Hand 6
Hand 6
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>
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>
for(i=0;i<n;i++)
{
scanf("%lf", &a[i]);
}
}
void main(void)
{
double a[7];
int i, 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>
smallest = arr[0];
return smallest;
}
int main()
{
int arr[10];
int i = 0;
printf("\nEnter elements:\n");
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;
if (j[i]<0)
count++;
else
count1++;
}
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 main()
{
int arr[MAX];
int n, i, 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;
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
int main()
{
int arr[MAX];
int n;
reverseArray(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();
}
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.
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;
7.Write a function that will return the sum of the elements on a specified column.
Pass the array and the column as parameters.
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];