我的任务是使用一个函数打印一个完整的数组,也是半个数组,也是一个只有奇数的array.我使用ASSERT和PRINT语句进行调试,但没有成功.

这是我的问题

#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;
}

这就是我要执行函数的地方

#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");
}

这是我的测试结果

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

我试着用

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
    }

来修复半数组测试,但它不起作用.

推荐答案

在测试您的代码(禁用Assert测试的情况下)时,在测试100个元素数组中的6个元素和100个元素数组中的7个元素的数组打印时,我在终端收到了以下输出.

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]

偶数子集似乎非常接近预期结果,但奇数子集缺少一个元素和"..."间隔器.似乎主要的症结是中间值的确定,以及如何对奇数子集进行处理.

考虑到这一点,下面是在函数中重构的两段代码.

    // 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++;

发生的附加测试是,如果要打印奇数子集,则由于整数除法,中途值需要增加"1". 因此,在打印七个元素的示例中,前半部分需要是四个(根据您注意到的较大的一半是前半部分的要求).

另一项重构工作是印制《...》.间隔器和随后印刷的第二部分.

        /* 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--;

从上面描述的测试输出中判断所需的答案,任何数组子集都应包括"..."间隔符,而不仅仅是编号的子集.此外,如果子集大小是奇数,则需要随后将中值递减"1".

有了这些重构,下面是利用上面叙述中记录的数组大小和子集大小进行的一些测试,以及打印完整数组的测试.

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]

从你的叙述来看,这似乎是预期结果应该是什么样子.

对此进行审查,以确定是否符合任务的标准.

C++相关问答推荐

strftime函数中%s的历史意义是什么?为什么没有记录?

如何启用ss(另一个调查套接字的实用程序)来查看Linux主机上加入的多播组IP地址?

将指针作为参数传递给函数

为什么I2C会发送错误的数据?

如何跨平台处理UTF-16字符串?

带有sigLongjMP中断I/O的异常处理程序

将常量转换为指针会增加.数据大小增加1000字节

在另一个函数中使用realloc和指针指向指针

如何在VS 2022中正确安装额外的C头文件

当b是无符号字符时,int a=(b<;<;2)>;>;2;和int a=b&;0x3F;之间有什么区别?

覆盖读取函数,但当文件描述符为3或4时,我有问题

仅从限制指针参数声明推断非混叠

不使用任何预定义的C函数进行逐位运算

如何使这个While循环在新行上结束

合并对 struct 数组进行排序

Realloc():中止的下一个大小无效(核心转储)

在C中交换字符串和数组的通用交换函数

macos/arm64 上地址空间不使用第一位吗?

添加/删除链表中的第一个元素

在带中断的循环缓冲区中使用 易失性