早教吧作业答案频道 -->其他-->
c++数组急求答案(1)对一维数组中的元素进行排序,使其按照升序输出。数组元素的值使用随机数函数得到10到99之间的数。(使用冒泡排序)(2)设有字符序列"Alan","Rose","Jack","Tom","Jod
题目详情
c++ 数组 急求答案
(1)对一维数组中的元素进行排序,使其按照升序输出。数组元素的值使用随机数函数得到10到99之间的数。(使用冒泡排序)
(2)设有字符序列"Alan", "Rose", "Jack", "Tom", "Jodan", "John", "Bill", "Bird", "Farmer", "Fisher"将这些字符序列保存在一维数组中,并对其使用简单选择法进行排序,要求字符长度小的排列在前面,若长度相同,则按照字母序列a~z排列(不区分大小写)。
(1)对一维数组中的元素进行排序,使其按照升序输出。数组元素的值使用随机数函数得到10到99之间的数。(使用冒泡排序)
(2)设有字符序列"Alan", "Rose", "Jack", "Tom", "Jodan", "John", "Bill", "Bird", "Farmer", "Fisher"将这些字符序列保存在一维数组中,并对其使用简单选择法进行排序,要求字符长度小的排列在前面,若长度相同,则按照字母序列a~z排列(不区分大小写)。
▼优质解答
答案和解析
不知道是否还来得及帮到你? 写了一个纯C的, 用的都是之前发布过的几个函数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
/* define dimensions */
const unsigned int NUM_ELMS = 10;
const unsigned int MAX_STR_NUM = 10;
const unsigned int MAX_STR_LEN = 20;
/* function declare */
void BubbleSort(int *data, const size_t num);
void SelectionSort(char [][MAX_STR_LEN], const size_t num_str);
void print_array(const int arr[], const size_t len);
void show_strings(char [][MAX_STR_LEN], const size_t num_str);
int stricmp (const char *p1, const char *p2);
int main(int argc, char** argv)
{
/* 1. Bubble sort demo for int array */
int values[NUM_ELMS] = {0};
size_t len_array;
srand(time(NULL)); /* random generator initial */
for (int i = 0; i < NUM_ELMS; i ++)
values[i] = rand() % 90 + 10; /* random range 10~99 */
len_array = sizeof(values)/sizeof(values[0]);
printf("1) Bubble sort before & after:\n");
print_array(values, len_array);
BubbleSort(values, len_array);
print_array(values, len_array);
/* 2. Selection sort demo for string array */
char szNames[][MAX_STR_LEN] = {"Alan", "Rose", "Jack", "Tom", "Jodan",
"John", "Bill", "Bird", "Farmer", "Fisher"};
size_t total_str_num = sizeof(szNames) / sizeof(szNames[0]);
printf("\n2) Selection sort before & after:\n");
show_strings(szNames, total_str_num);
SelectionSort(szNames, total_str_num);
show_strings(szNames, total_str_num);
return 0;
}
int stricmp (const char *p1, const char *p2)
{
register unsigned char *s1 = (unsigned char *) p1;
register unsigned char *s2 = (unsigned char *) p2;
unsigned char c1, c2;
do
{
c1 = (unsigned char) toupper((int)*s1++);
c2 = (unsigned char) toupper((int)*s2++);
if (c1 == '\0')
{
return c1 - c2;
}
}
while (c1 == c2);
return c1 - c2;
}
void SelectionSort(char strlist[][MAX_STR_LEN], size_t num_str)
{
char sztemp[MAX_STR_LEN] = "0\0";
int i, j, min;
for (i = 0; i < num_str - 1; i ++)
{
min = i;
for (j = i+1; j < num_str; j ++)
if (strlen(strlist[min]) > strlen(strlist[j]) )
min = j;
else if (strlen(strlist[min]) == strlen(strlist[j]) )
{
if ( stricmp(strlist[min], strlist[j]) > 0 )
min = j;
}
if ( min != i)
{
strcpy(sztemp, strlist[min]);
strcpy(strlist[min], strlist[i]);
strcpy(strlist[i], sztemp);
}
}
}
void show_strings(char strlist[][MAX_STR_LEN], size_t num_str)
{
for (int i = 0; i < num_str; i ++)
printf("%s ", strlist[i]);
printf("\n");
}
void BubbleSort(int *data, const size_t num)
{
int i, j, _temp;
i = num;
while (i > 0)
{
for (j = 0; j < i-1; j++)
{
if (data[j] > data[j+1])
{
_temp = data[j];
data[j] = data[j+1];
data[j+1] = _temp;
}
}
i--;
}
}
void print_array(const int arr[], const size_t len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", arr[i]);
printf("\n");
}
看了 c++数组急求答案(1)对一...的网友还看了以下:
设函数f(x)=x+a/x定义域为(0,+∞),且f(2)=5/2.设点P是函数图像上的任意一点, 2020-05-12 …
下述哪一条不属于数据库设计的内容?A.设计数据库管理系统B.设计数据库概念结构C.设计数据库逻辑 2020-05-23 …
设一数列a,b,c,d,e,f,通过栈结构不可能不可能排成的顺序数列为()A)c,b,e,f,d, 2020-06-28 …
设一数列的顺序为1,2,3,4,5,6,通过栈操作可以得到()的输出序列?A3,2,5,6,4,1 2020-07-09 …
学习线段后,杨老师要求同学们自己设计一个图形,且所设计图形中线段的总条数是8条.(1)如图是某个同 2020-07-25 …
考研数学中的证明题~假设辅助函数的问题~考研数学中的证明题~如果根据题设~假设一个函数~这个函数完 2020-08-02 …
两个数和为8,积为9.75,求这连个数.设一数为X另一数为(8-X)X(8-X)=9.75这样的列式 2020-11-18 …
一个关于高一数列的小问题为什么奇数个数成等差数列,可设为a-2d,a-d,a,a+d,a+2d?而偶 2020-11-20 …
概率论与数理统计为保证设备的正常运行,必须配备一定数量的设备维修人员,现有同类设备180台,且各台设 2020-11-21 …
假设你叫Alan,根据以下个人情况写一篇自我介绍.Name:AlanBrown.Age:16.Add 2020-11-23 …