早教吧作业答案频道 -->其他-->
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)对一...的网友还看了以下:
1.使用高级语言(C、C++、C#语言)实现一个加密/解密程序,调试并通过该程序。(1)算法描述( 2020-05-17 …
公元1924年7月25号是星期几?请用s=x-1+[x-1/4]-[x-1/100]+[x-1/4 2020-05-17 …
1.已知3X-4Y-2Z=0,2X-Y-8Z=0求(X^2+Y^+Z^)/XY+YZ+2XZ2.已 2020-06-06 …
...急急急随着每年10亿m³的天然气输到北京,北京市每年将少烧300万吨煤.某单位1个月用煤30 2020-06-29 …
f(x)在[a,b]连续,在(a,b)二阶连续可导,证明存在c,使f(a)+f(b)-2f((a+ 2020-07-25 …
已知{an}是首项为2,公比为1/2的等比数列,Sn为它的前n项和.⑴,用Sn表示S(n+1)⑵, 2020-07-28 …
光年是天文学使用的距离单位.主要用于度量太阳系外天体的距离.1光年是指光在真空中一年所走的距离,真 2020-08-02 …
有理数abc在数轴上的对应点如图所示------c----b--0----a---------→(1 2020-11-21 …
已知A(0,2,4),B(-2,1,6),C(1,-1,5),用向量方法求三角形ABC的面积S急用! 2020-12-04 …
急急急急,求这些英语题答案!1.aim是动词。意为“瞄准,对准”,常与介词at连用,意为“针对,瞄准 2021-01-11 …