I have been tasked with using a function to print a full array, also a half array, also an array with only odd numbers. I used assert and print statements to debug but I haven't had success.
This is my problem.c
#include <stdio.h>
#include <stdlib.h>
#include "pprint.h"
// DO NOT EDIT MAIN
/* Usage: ./mysolution [np] [n]*/
int main(int argc, char *argv[])
{
int n = atoi(argv[2]);
int arr[n];
for (int i = 0; i < n; i++)
{
arr[i] = i;
}
pprintarr(arr, n, atoi(argv[1]));
return 0;
}
This is where I am executing my function
#include "pprint.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
/*
* pprintarr
* AKA Pretty Print Array
*
* Input:
* arr -- ptr to an integer array
* n -- number of members in the array
* np -- number of members to print out
*
* Constraints:
* np <= n
* np > 0
* n > 0
*
* Output:
* if np == 5, then print:
* [a0 a1 a2 ... an-1 an]
* Note that "..." should be placed in the middle
* For odd numbers, make the head longer than the tail.
* if n == np, then "... should not be printed."
*/
void pprintarr(int *arr, int n, int np) {
// Ensure n and np are within valid constraints
assert(n > 0 && "The elements in the array have to be more than zero");
assert(np <= n && "Only able to print the numbers in the array and not beyond");
assert(np >= 0 && "Print an empty array");
assert(arr != NULL && "Array pointer is NULL");
assert(np == 10 && "np should be equal to 10");
// If np is 0, print an empty array and return
if (np == 0) {
printf("[]\n");
return;
}
// Begin printing the array
printf("[");
if (np < n) {
int half = np / 2; // Determine the middle point
for (int i = 0; i < half; i++) {
printf("%d", arr[i]);
if (i < half - 1) {
printf(" "); // Add a space between elements in the first half
}
}
if (np % 2 == 0) {
printf("... "); // If np is even, add "..." between the two middle elements
} else {
printf(" "); // Add a space in the middle for odd numbers
}
for (int i = n - half; i < n; i++) {
printf("%d", arr[i]);
if (i < n - 1) {
printf(" "); // Add a space between elements in the second half
}
}
} else {
// If np is greater than or equal to n, print the entire array
for (int i = 0; i < n; i++) {
printf("%d", arr[i]);
if (i < n - 1) {
printf(" "); // Add a space between elements except for the last one
}
}
}
// Close the array and add a newline character
printf("]\n");
}
here are the results of my test
Dots Placement - Points: 10
------------------------------------------------------------
Even (100)
['6', '100']
FAIL ✗
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
YOURS stdout:
b''
~~~~~~~~~~
OURS :
[0 1 2 ... 97 98 99]
b'[0 1 2 ... 97 98 99]\n'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Odd (100)
['7', '100']
FAIL ✗
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
YOURS stdout:
b''
~~~~~~~~~~
OURS :
[0 1 2 3 ... 97 98 99]
b'[0 1 2 3 ... 97 98 99]\n'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Points per Test: 5.0 pts
Total Points for Suite: 0.0 / 10 pts
I tried using
if (np < n) {
int half = np / 2; // Determine the middle point
for (int i = 0; i < half; i++) {
printf("%d ", arr[i]); // Print the first half with spaces
}
to fix the half array test but it didn't work.
In trying out your code (with the assert testing deactivated), I received the following output at the terminal when testing for an array print of 6 elements in a one hundred element array and 7 elements in a one hundred element array.
craig@Vera:~/C_Programs/Console/Arrays/bin/Release$ ./Arrays 6 100
[0 1 2... 97 98 99]
craig@Vera:~/C_Programs/Console/Arrays/bin/Release$ ./Arrays 7 100
[0 1 2 97 98 99]
The even subset seemed to closely approximate the expected result, but the odd numbered subset was missing an element and the " ... " spacer. What appeared to be the main sticking point was the determination of the halfway value and how it needed to be manipulated for odd numbered subsets.
With that in mind, following are the two sections of code that were refactored in the function.
// Begin printing the array
printf("[");
if (np < n) {
int half = np / 2; // Determine the middle point
if (np % 2 != 0) /* Add "1" to the halfway value if np is an odd number */
half++;
The additional testing that happens is that if an odd numbered subset is to be printed, the halfway value needed to be incremented by "1" due to the integer division. So, in the example of having seven elements printed, the first half would need to be four (per the requirements you noted of the larger half being the first half).
The other bit of refactoring was at the printing of the " ... " spacer and the subsequent printing of the second half.
/* Deactivated this block of code and use the following block
if (np % 2 == 0) {
printf("... "); // If np is even, add "..." between the two middle elements
} else {
printf(" "); // Add a space in the middle for odd numbers
}
*/
if (np < n)
printf(" ... "); /* Print the ellipses if a subset of the array is requested */
if (np % 2 != 0) /* Subsequently, subtract "1" from the halfway value if np is an odd number */
half--;
Evaluating the desired answer from your test output in the narrative above, any array subset was to include the " ... " spacer, not just even numbered subsets. Also, if the subset size was an odd number the halfway value needed to subsequently be decremented by "1".
With those bits of refactoring, following were some tests utilizing the array size and subset sizes noted in the narrative above, along with a test where the full array is printed.
craig@Vera:~/C_Programs/Console/Arrays/bin/Release$ ./Arrays 6 100
[0 1 2 ... 97 98 99]
craig@Vera:~/C_Programs/Console/Arrays/bin/Release$ ./Arrays 7 100
[0 1 2 3 ... 97 98 99]
craig@Vera:~/C_Programs/Console/Arrays/bin/Release$ ./Arrays 10 10
[0 1 2 3 4 5 6 7 8 9]
Going from your narrative, that appears to be what the desired results are supposed to look like.
Give that a review to see meets the criteria of the task.