Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 2 (Array Formatting): Written in C code. Write a program that takes in a

ID: 675527 • Letter: P

Question

Program 2 (Array Formatting): Written in C code.
Write a program that takes in an NxM dimensional array of integers and prints it out formatted
according to some user defined specifications. The user can define the following parameters:
the number of empty (new) lines between rows
the number of characters that each array element can span
alignment: left or right justification
what character should be placed in between the elements of each row
Your program should:
prompt a user to enter the sizes (rows and columns) of the array, and read in the
dimensions
prompt the user to enter the array row-by-row and read in each row. Reuse your
read_row function from Program 1.
ask for the formatting specifications and check that they can be obeyed
print the formatted array
Requirements: your program should use the following user defined functions:
the function read_row from above
a function format_row that formats and prints an individual row based on specifications
(these should be given as parameters to the function)
a function print_array that uses format_row to print the entire array
a function check_formatting that checks the feasibility of the formatting
specifications: for example, if the user allows only 3 characters for each array element,
but the array contains a number that is 4 digits long, the function should return false. This
should cause the program to ask again for new formatting specifications.
Here is an example of how the program should behave (items underlined are entered by the
user):
This program prints a formatted NxM array
Enter the size of your array: 2 6
Enter row 0: 0 1 2 3 4 5234
Enter row 1: 0 1 6 7 8 9
Enter the number of new lines between rows: 2
Enter the span of each array element: 3
The array contains elements longer than 3 digits long.
Enter the span of each array element: 6
Enter the type of justification (l ­ left, r ­ right): r
Enter the character to be placed between elements: #
Your formatted array is:
_ _ _ _ _0#_ _ _ _ _ 1#_ _ _ _ _ 2#_ _ _ _ _ 3#_ _ _ _ _ 4#_ _ 5234
_ _ _ _ _0#_ _ _ _ _ 1#_ _ _ _ _ 6#_ _ _ _ _ 7#_ _ _ _ _ 8#_ _ _ _ _ 9 Program 2 (Array Formatting): Written in C code.
Write a program that takes in an NxM dimensional array of integers and prints it out formatted
according to some user defined specifications. The user can define the following parameters:
the number of empty (new) lines between rows
the number of characters that each array element can span
alignment: left or right justification
what character should be placed in between the elements of each row
Your program should:
prompt a user to enter the sizes (rows and columns) of the array, and read in the
dimensions
prompt the user to enter the array row-by-row and read in each row. Reuse your
read_row function from Program 1.
ask for the formatting specifications and check that they can be obeyed
print the formatted array
Requirements: your program should use the following user defined functions:
the function read_row from above
a function format_row that formats and prints an individual row based on specifications
(these should be given as parameters to the function)
a function print_array that uses format_row to print the entire array
a function check_formatting that checks the feasibility of the formatting
specifications: for example, if the user allows only 3 characters for each array element,
but the array contains a number that is 4 digits long, the function should return false. This
should cause the program to ask again for new formatting specifications.
Here is an example of how the program should behave (items underlined are entered by the
user):
This program prints a formatted NxM array
Enter the size of your array: 2 6
Enter row 0: 0 1 2 3 4 5234
Enter row 1: 0 1 6 7 8 9
Enter the number of new lines between rows: 2
Enter the span of each array element: 3
The array contains elements longer than 3 digits long.
Enter the span of each array element: 6
Enter the type of justification (l ­ left, r ­ right): r
Enter the character to be placed between elements: #
Your formatted array is:
_ _ _ _ _0#_ _ _ _ _ 1#_ _ _ _ _ 2#_ _ _ _ _ 3#_ _ _ _ _ 4#_ _ 5234
_ _ _ _ _0#_ _ _ _ _ 1#_ _ _ _ _ 6#_ _ _ _ _ 7#_ _ _ _ _ 8#_ _ _ _ _ 9 Program 2 (Array Formatting): Written in C code.
Write a program that takes in an NxM dimensional array of integers and prints it out formatted
according to some user defined specifications. The user can define the following parameters:
the number of empty (new) lines between rows
the number of characters that each array element can span
alignment: left or right justification
what character should be placed in between the elements of each row
Your program should:
prompt a user to enter the sizes (rows and columns) of the array, and read in the
dimensions
prompt the user to enter the array row-by-row and read in each row. Reuse your
read_row function from Program 1.
ask for the formatting specifications and check that they can be obeyed
print the formatted array
Requirements: your program should use the following user defined functions:
the function read_row from above
a function format_row that formats and prints an individual row based on specifications
(these should be given as parameters to the function)
a function print_array that uses format_row to print the entire array
a function check_formatting that checks the feasibility of the formatting
specifications: for example, if the user allows only 3 characters for each array element,
but the array contains a number that is 4 digits long, the function should return false. This
should cause the program to ask again for new formatting specifications.
Here is an example of how the program should behave (items underlined are entered by the
user):
This program prints a formatted NxM array
Enter the size of your array: 2 6
Enter row 0: 0 1 2 3 4 5234
Enter row 1: 0 1 6 7 8 9
Enter the number of new lines between rows: 2
Enter the span of each array element: 3
The array contains elements longer than 3 digits long.
Enter the span of each array element: 6
Enter the type of justification (l ­ left, r ­ right): r
Enter the character to be placed between elements: #
Your formatted array is:
_ _ _ _ _0#_ _ _ _ _ 1#_ _ _ _ _ 2#_ _ _ _ _ 3#_ _ _ _ _ 4#_ _ 5234
_ _ _ _ _0#_ _ _ _ _ 1#_ _ _ _ _ 6#_ _ _ _ _ 7#_ _ _ _ _ 8#_ _ _ _ _ 9

Explanation / Answer

Below is the required code.

#include <stdio.h>

void read_row(int *arr[][], int M, int i)
{
int j;
for(j=0; j<M; j++)
{
scanf("%d", &arr[i][j]);
}
}

void check_formatting(int* noel, char* alignment, char* elemSep)
{
if((*noel<0)) *noel=0;
if(!(*alignment == 'L' || *alignment == 'l' || *alignment == 'R' || *alignment == 'r')) *alignment = 'L';
}

void print_array(int arr[][], int N, int M, int noel, char alignment, char elemSep)
{
int k1, k2, k3;
for(i=0; i<N; i++)
{
for(j=0; j<M; j++)
{
printf("%d%c", arr[i][j],elemSep);
}
for(k1=0; k1<noel; k1++)
{
printf(" ");
}
}
}

int main()
{
int N,M, i,j;

printf("Enter the sizes (rows and columns) of the array : ");
scanf("%d %d", &N, &M);

int arr[N][M];

printf("Enter the array row by row : ");
for(i=0; i<N; i++)
{
read_row(&arr[][], M, i);
}

int noel;
char alignment, elemSep;

printf("Enter the number of empty lines between the rows"); scanf("%d", &noel);
printf("What kind of alignment you want for the array - Left/Right - enter L or R"); scanf("%c", &alignment;);
printf("What character to be placed in between the elements of each row ?"); scanf("%c", &elemSep);

check_formatting(&noel, &alignment, &elemSep);
print_array(arr, N, M, noel, alignment, elemSep);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote