思考与练习题3.27编程实习题:假设有一个字符数组 char num [10][]=(" hello "," world "," we "," dgut ","…
- User
思考与练习题3.27编程实习题:假设有一个字符数组 char num [10][]=(" hello "," world "," we "," dgut "," university "," abc "," china "," Dongguan "," Guangdong "," Songshanhu "," computer ")。 (1)写一个程序,调用 Linux 系统的 qsort 库函数,对数组 num 进行排序后,按顺序输出各字符串元素的值。
- Assistant
#include <stdio.h> #include <stdlib.h> #include <string.h>
int compare(const void* a, const void* b) { return strcmp((char**)a, (char)b); }
int main() { char num[10][20] = {"hello", "world", "we", "dgut", "university", "abc", "china", "Dongguan", "Guangdong", "Songshanhu"}; qsort(num, 10, sizeof(char*), compare); for (int i = 0; i < 10; i++) { printf("%s ", num[i]); } printf("\n"); return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.