Starting from:

$34.99

CZ1007 ASSIGNMENT 2– ARRAYS Solution


Assignment on Coding Questions

For this assignment – you will need to answer 4 questions (randomly generated via APAS) from the following question list:

1. absoluteSum1D
2. find2Max1D
3. findMinMax1D
4. specialNumbers1D
5. platform1D
6. swapMinMax1D 7. findAverage2D
8. computeTotal2D
9. transpose2D
10. symmetry2D
11. compress2D
12. minOfMax2D

Questions

1. (absoluteSum1D) Write a C function that returns the sum of the absolute values of the elements of a vector with the following prototype:

float absoluteSum1D(int size, float vector[]);

where size is the number of elements in the vector.

A sample program template is given below to test the function:

#include <stdio.h> #include <math.h>
float absoluteSum1D(int size, float vector[]); int main() {
float vector[10]; int i, size;

printf("Enter vector size: "); scanf("%d", &size);
printf("Enter %d data: ", size); for (i=0; i<size; i++) scanf("%f", &vector[i]);
printf("absoluteSum1D(): %.2f", absoluteSum1D(size, vector)); return 0;
}
float absoluteSum1D(int size, float vector[])
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter vector size:
5
Enter 5 data: 1.1 3 5 7 9 absoluteSum1D(): 25.10

(2) Test Case 2:
Enter vector size:
6
Enter 6 data: 1 -3 5 -7 9 -2 absoluteSum1D(): 27.00

void find2Max1D(int ar[], int size, int *max1, int *max2);
A sample program is given below to test the function:

#include <stdio.h>
void find2Max1D(int ar[], int size, int *max1, int *max2); int main() {
int max1,max2; int ar[10],size,i;

printf("Enter array size: ");
scanf("%d", &size); printf("Enter %d data: ", size); for (i=0; i<size; i++) scanf("%d", &ar[i]); find2Max1D(ar,size,&max1,&max2); printf("Max1: %d Max2: %d ",max1,max2); return 0; }
void find2Max1D(int ar[], int size, int *max1, int *max2)
{
/* Write your code here */
}

A sample input and output session is given below:

(1) Test Case 1:
Enter array size:
5
Enter 5 data:
1 2 3 5 6
Max1: 6
Max2: 5

(2) Test Case 2:
Enter array size:
6
Enter 6 data:
-4 0 -7 3 2 1
Max1: 3
Max2: 2

(3) Test Case 3:
Enter array size:
2
Enter 2 data:
6 5
Max1: 6
Max2: 5

(4) Test Case 4:
Enter array size:
3
Enter 3 data:
-4 -7 -3
Max2: -4

3. (findMinMax1D) Write a C function that takes in an one-dimensional array of integers ar and size as parameters. The function finds the minimum and maximum numbers of the array. The function returns the minimum and maximum numbers through the pointer parameters min and max via call by reference. The function prototype is given as follows:

void findMinMax1D(int ar[], int size, int *min, int *max);
A sample program template is given below to test the function:

#include <stdio.h>
void findMinMax1D(int ar[], int size, int *min, int *max); int main() { int ar[40]; int i, size; int min, max;

printf("Enter array size: ");
scanf("%d", &size); printf("Enter %d data: ", size); for (i=0; i<size; i++) scanf("%d", &ar[i]); findMinMax1D(ar, size, &min, &max); printf("min = %d; max = %d ", min, max); return 0;
}
void findMinMax1D(int ar[], int size, int *min, int *max)
{
/* Write your code here */ }

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter array size:
5
Enter 5 data: 1 2 3 5 6 min = 1; max = 6

(2) Test Case 2:
Enter array size:
6
Enter 6 data: -4 0 -7 3 2 1 min = -7; max = 3

(3) Test Case 3:
Enter array size:
1
Enter 1 data:
1 min = 1; max = 1

4. (specialNumbers1D) A special number is a 3 digit positive integer, in which the sum of the cubes of each digit is equal to the number. For example, the number 407 is a special number as 407 = 4×4×4 + 0×0×0 + 7×7×7. Write a C function that computes special numbers from 100 up to the a specified number. The function takes an array of integers ar, num and size as parameters. The parameter num is the specified integer number. The parameter size is declared as a pointer which will return the size of the array storing the special numbers to the caller. The prototype of the function is given as follows:
void specialNumbers1D(int ar[], int num, int *size);

A sample program template is given below to test the function:

#include <stdio.h>
void specialNumbers1D(int ar[], int num, int *size); int main() {
int a[20],i,size=0,num;

printf("Enter a number (between 100 and 999): "); scanf("%d", &num); specialNumbers1D(a, num, &size); printf("specialNumbers1D(): "); for (i=0; i<size; i++) printf("%d ",a[i]);
return 0;
}
void specialNumbers1D(int ar[], int num, int *size)
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter a number (between 100 and 999):
300
specialNumbers1D(): 153

(2) Test Case 2:
Enter a number (between 100 and 999):
400 specialNumbers1D(): 153 370 371

(3) Test Case 3:
Enter a number (between 100 and 999):
500 specialNumbers1D(): 153 370 371 407

(4) Test Case 4:
Enter a number (between 100 and 999):
999 specialNumbers1D(): 153 370 371 407

5. (platform1D) The number of consecutive array elements in an array that contains the same integer value forms a ‘platform’. Write a C function that takes in an array of integers ar and size as parameters, and returns the length of the maximum platform in ar to the calling function. The function prototype is given as follows:

int platform1D(int ar[], int size);

A sample program template is given below to test the function:

#include <stdio.h>
int platform1D(int ar[], int size); int main() {
int i,b[50],size;

printf("Enter array size: ");
scanf("%d", &size); printf("Enter %d data: ", size); for (i=0; i<size; i++) scanf("%d",&b[i]);
printf("platform1D(): %d ", platform1D(b,size)); return 0; }
int platform1D(int ar[], int size)
{
/* Write your code here */ }

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter array size:
5
Enter 5 data: 1 2 2 2 3 platform1D(): 3

(2) Test Case 2:
Enter array size:
1
Enter 1 data:
2
platform1D(): 1

(3) Test Case 3:
Enter array size:
10
Enter 10 data: 1 2 3 4 5 6 7 8 9 0 platform1D(): 1

(4) Test Case 4:
Enter array size:
10
Enter 10 data: 1 2 3 4 4 4 7 8 8 0 platform1D(): 3

6. (swapMinMax1D) Write the C function swapMinMax1D() that takes in an array of integers ar and size (>1) as parameters, finds the index positions of the largest number and smallest number in the array, swaps the index positions of these two numbers, and passes the array to the calling function via call by reference. For example, if ar is {1,2,3,4,5}, then the resultant array ar will be {5,2,3,4,1} after executing the function. If there are more than one largest or smallest number in the array, we will swap the last occurrence of the largest and smallest numbers. For example, if ar is {5,2,1,1,8,9,9}, then the resultant array ar will be {5,2,1,9,8,9,1} after executing the function. The function prototype is:
void swapMinMax1D(int ar[], int size);

A sample program is given below to test the function:

#include <stdio.h>
void swapMinMax1D(int ar[], int size); int main() {
int ar[50],i,size;

printf("Enter array size: ");
scanf("%d", &size); printf("Enter %d data: ", size); for (i=0; i<size; i++) scanf("%d",ar+i); swapMinMax1D(ar, size); printf("swapMinMax1D(): "); for (i=0; i<size; i++) printf("%d ",*(ar+i)); return 0;
}
void swapMinMax1D(int ar[], int size)
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter array size: 5
Enter 5 data:
1 2 3 4 5
swapMinMax1D(): 5 2 3 4 1

(2) Test Case 2:
Enter array size:
2
Enter 2 data: 5 5
swapMinMax1D(): 5 5

(3) Test Case 3:
Enter array size:
7
Enter 7 data: 1 1 1 5 5 5 5
swapMinMax1D(): 1 1 5 5 5 5 1

(4) Test Case 4:
Enter array size:
9
Enter 9 data: 9 1 1 9 9 5 5 5 5
swapMinMax1D(): 9 1 9 9 1 5 5 5 5

7. (findAverage2D) Write a C function that takes a 4x4 two-dimensional array of floating point numbers matrix as a parameter. The function computes the average of the first three elements of each row of the array and stores it at the last element of the row. The function prototype is given as follows:

void findAverage2D(float matrix[4][4]);
A sample program template is given below to test the function:

#include <stdio.h>
void findAverage2D(float matrix[4][4]); int main()
{
float ar[4][4]; int i,j;

printf("Enter data: "); for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) scanf("%f", &ar[i][j]);
}
findAverage2D(ar); printf("findAverage2D(): : "); for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) printf("%.2f ", ar[i][j]); printf(" ");
} return 0; }
void findAverage2D(float matrix[4][4])
{
/* Write your code here */ }

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter data:
1 2 3 0
4 5 6 0
7 8 9 0 1 2 3 0 findAverage2D(): 1.00 2.00 3.00 2.00
4.00 5.00 6.00 5.00
7.00 8.00 9.00 8.00
1.00 2.00 3.00 2.00

(2) Test Case 2:
Enter data:
1 2 3 0
4 5 6 0
-4 -5 -6 0 1 2 3 0 findAverage2D(): 1.00 2.00 3.00 2.00
4.00 5.00 6.00 5.00
-4.00 -5.00 -6.00 -5.00
1.00 2.00 3.00 2.00

(3) Test Case 3:
Enter data:
1.5 2 2.5 0 4 5 6 0
-4 -5 -6 0 1 2 3 0 findAverage2D(): 1.50 2.00 2.50 2.00
4.00 5.00 6.00 5.00
-4.00 -5.00 -6.00 -5.00
1.00 2.00 3.00 2.00

8. (computeTotal2D) Write a C function that takes a 4x4 two-dimensional array matrix of integer numbers as a parameter. The function computes the total of the first three elements of each row of the array and stores it at the last element of the row. The function prototype is given as follows:
void computeTotal2D(int matrix[SIZE][SIZE]);

A sample program template is given below to test the function:

#include <stdio.h> #define SIZE 4
void computeTotal2D(int matrix[SIZE][SIZE]); int main() {
int a[SIZE][SIZE]; int i,j;
printf("Enter the matrix data (%dx%d): ", SIZE, SIZE); for (i=0; i<SIZE; i++) for (j=0; j<SIZE; j++) scanf("%d", &a[i][j]); printf("computeTotal2D(): "); computeTotal2D(a); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%d ", a[i][j]); printf(" "); } return 0; }
void computeTotal2D(int matrix[SIZE][SIZE])
{
/* Write your code here */
}

Some test input and output sessions are given below:

(1) Test Case 1
Enter the matrix data (4x4):
1 2 3 4
2 3 4 5
3 4 5 6
6 7 8 9
computeTotal2D():
1 2 3 6
2 3 4 9
3 4 5 12
6 7 8 21

(2) Test Case 2
Enter the matrix data (4x4):
1 2 3 -4
2 3 -4 5
3 -4 5 6 -6 7 8 9
computeTotal2D():
1 2 3 6
2 3 -4 1
3 -4 5 4
-6 7 8 9

(3) Test Case 3
Enter the matrix data (4x4):
1 -2 3 4
2 -3 4 5
3 -4 5 6 6 -7 8 9
computeTotal2D():
1 -2 3 2
2 -3 4 3
3 -4 5 4
6 -7 8 7

9. (transpose2D) Write a function that takes a square matrix ar, and the array sizes for the rows and columns as parameters, and returns the transpose of the array via call by reference. For example, if the rowSize is 4, colSize is 4, and the array ar is {1,2,3,4, 1,1,2,2, 3,3,4,4, 4,5,6,7}, then the resultant array will be {1,1,3,4, 2,1,3,5, 3,2,4,6, 4,2,4,7}. The function prototype is given below:
 void transpose2D(int ar[][SIZE], int rowSize, int colSize);

A sample program template is given below to test the function:

#include <stdio.h> #define SIZE 10
void transpose2D(int ar[][SIZE], int rowSize, int colSize); void display(int ar[][SIZE], int rowSize, int colSize); int main() {
int ar[SIZE][SIZE], rowSize, colSize; int i,j;

printf("Enter row size of the 2D array: "); scanf("%d", &rowSize);
printf("Enter column size of the 2D array: "); scanf("%d", &colSize);
printf("Enter the matrix (%dx%d): ", rowSize, colSize); for (i=0; i<rowSize; i++) for (j=0; j<colSize; j++) scanf("%d", &ar[i][j]); printf("transpose2D(): "); transpose2D(ar, rowSize, colSize); display(ar, rowSize, colSize); return 0; }
void display(int ar[][SIZE], int rowSize, int colSize)
{ int l,m;
for (l = 0; l < rowSize; l++) { for (m = 0; m < colSize; m++) printf("%d ", ar[l][m]); printf(" ");
} }
void transpose2D(int ar[][SIZE], int rowSize, int colSize)
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter row size of the 2D array:
4
Enter column size of the 2D array:
4
Enter the matrix (4x4):
1 2 3 4
1 1 2 2
3 3 4 4
4 5 6 7
transpose2D():
1 1 3 4
2 1 3 5
3 2 4 6
4 2 4 7

(2) Test Case 2:
Enter row size of the 2D array:
3
Enter column size of the 2D array:
3
Enter the matrix (3x3):
1 2 3
3 4 5 5 6 7
transpose2D():
1 3 5
2 4 6
3 5 7

(3) Test Case 3:
1 2 3 4 5
1 1 2 2 5
3 3 4 4 5
4 5 6 7 5
1 1 2 2 5

Enter row size of the 2D array:
5
Enter column size of the 2D array:
5
Enter the matrix (4x4):
transpose2D(): 1 1 3 4 1 2 1 3 5 1
3 2 4 6 2
4 2 4 7 2
5 5 5 5 5

10. (symmetry2D) Write the C function that takes in a square two-dimensional array of integer numbers M and the array sizes for rows and columns as parameters, and returns 1 if M is symmetric or 0 otherwise. A square two-dimensional matrix is symmetric iff it is equal to its transpose. It means that M[i][j] is equal to M[j][i] for 0<=i<=rowSize and 0<=j<=colSize. For example, if rowSize and colSize are 4, and M is {{1,2,3,4},{2,2,5,6},{3,5,3,7},
{4,6,7,4}}, then M will be symmetric. The function prototype is given as follows:
int symmetry2D(int M[][SIZE], int rowSize, int colSize);

A sample program template is gven below to test the function:

#include <stdio.h>
#define SIZE 10 #define INIT_VALUE 999
int symmetry2D(int M[][SIZE], int rowSize, int colSize); int main() {
int M[SIZE][SIZE],i,j, result = INIT_VALUE;
int rowSize, colSize;

printf("Enter the array size (rowSize, colSize): "); scanf("%d %d", &rowSize, &colSize);
printf("Enter the matrix (%dx%d): ", rowSize, colSize); for (i=0; i<rowSize; i++)
for (j=0; j<colSize; j++) scanf("%d", &M[i][j]); result=symmetry2D(M, rowSize, colSize); if (result == 1)
printf("symmetry2D(): Yes "); else if (result == 0) printf("symmetry2D(): No "); else
printf("Error "); return 0; }
int symmetry2D(int M[][SIZE], int rowSize, int colSize)
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter the array size (rowSize, colSize):
4 4
Enter the matrix (4x4):
1 2 3 4
2 2 5 6
3 5 3 7
4 6 7 4 symmetry2D(): Yes

(2) Test Case 2:
Enter the array size (rowSize, colSize):
4 4
Enter the matrix (4x4):
1 2 3 4
2 2 5 6
3 5 3 7
5 6 7 4 symmetry2D(): No

(3) Test Case 3:
Enter the array size (rowSize, colSize):
3 3
Enter the matrix (3x3):
1 2 3 2 6 7
3 7 3 symmetry2D(): Yes

(4) Test Case 4:
Enter the array size (rowSize, colSize):
5 5
Enter the matrix (5x5):
1 2 3 4 5
2 2 5 6 7
3 5 3 7 8
4 6 7 4 5 5 7 8 5 5
symmetry2D(): Yes


void compress2D(int data[SIZE][SIZE], int rowSize, int colSize);
A sample program template is given below to test the function:

#include <stdio.h> #define SIZE 100
void compress2D(int data[SIZE][SIZE], int rowSize, int colSize); int main() {
int data[SIZE][SIZE]; int i,j;
int rowSize, colSize;

printf("Enter the array size (rowSize, colSize): "); scanf("%d %d", &rowSize, &colSize);
printf("Enter the matrix (%dx%d): ", rowSize, colSize); for (i=0; i<rowSize; i++) for (j=0; j<colSize; j++) scanf("%d", &data[i][j]); printf("compress2D(): "); compress2D(data, rowSize, colSize); return 0; }
void compress2D(int data[SIZE][SIZE], int rowSize, int colSize)
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter the array size (rowSize, colSize):
4 4
Enter the matrix (4x4):
1 1 1 0
0 0 1 1
1 1 1 1 0 0 0 0 compress2D():
1 3 0 1
0 2 1 2
1 4
0 4

(2) Test Case 2:
Enter the array size (rowSize, colSize):
5 5
Enter the matrix (5x5):
1 1 1 0 0
0 0 1 1 1
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 compress2D():
1 3 0 2
0 2 1 3
1 5
0 5
1 5

(3) Test Case 3:
Enter the array size (rowSize, colSize):
5 5
Enter the matrix (5x5):
0 0 0 0 0
1 1 1 1 1
1 1 1 1 1
0 0 0 0 0 1 1 1 1 1
compress2D():
0 5
1 5
1 5
0 5
1 5

(4) Test Case 4:
Enter the array size (rowSize, colSize):
10 10
Enter the matrix (10x10):
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 compress2D():
1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1
0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1
1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1
0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1
1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1
0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1
1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1
0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1
1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1
0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1

12. (minOfMax2D) Write a C function minOfMax2D() that takes a two-dimensional array matrix of integers ar, and the array sizes for the rows and columns as parameters. The function returns the minimum of the maximum numbers of each row of the 2-dimensional array ar. For example, if the rowSize is 4, colSize is 4, and ar is {{1,3,5,2}, {2,4,6,8}, {8,6,4,9}, {7,4,3,2}}, then the maximum numbers will be 5, 8, 9 and 7 for rows 0, 1, 2 and 3 respectively, and the minimum of the maximum numbers will be 5. The prototype of the function is given as follows:
int minOfMax2D(int ar[][SIZE], int rowSize, int colSize);

A sample program is given below to test the function:

#include <stdio.h> #define SIZE 10
int minOfMax2D(int ar[][SIZE], int rowSize, int colSize); int main() {
int ar[SIZE][SIZE], rowSize, colSize; int i,j,min;

printf("Enter row size of the 2D array: "); scanf("%d", &rowSize);
printf("Enter column size of the 2D array: "); scanf("%d", &colSize);
printf("Enter the matrix (%dx%d): ", rowSize, colSize); for (i=0; i<rowSize; i++)
for (j=0; j<colSize; j++) scanf("%d", &ar[i][j]); min=minOfMax2D(ar, rowSize, colSize); printf("minOfMax2D(): %d ", min); return 0; }
int minOfMax2D(int ar[][SIZE], int rowSize, int colSize)
{
/* Write your code here */
}

Some sample input and output sessions are given below:

(1) Test Case 1:
Enter row size of the 2D array:
4
Enter column size of the 2D array:
4
Enter the matrix (4x4):
1 2 3 4
2 3 4 5
5 6 7 8
8 10 2 4 minOfMax2D(): 4

(2) Test Case 2:
Enter row size of the 2D array:
3
Enter column size of the 2D array:
3
Enter the matrix (3x3):
1 -3 3
-3 2 4
3 6 -8
minOfMax2D(): 3

(3) Test Case 3:
Enter row size of the 2D array:
5
Enter column size of the 2D array:
5
Enter the matrix (5x5):
1 2 3 4 5
2 3 4 5 6
5 6 7 8 9
8 10 2 4 7 2 3 4 5 8 minOfMax2D(): 5




More products