帮助我排序字符。 [英] HELP ME on Sorting Characters.
问题描述
我的教授给我这个任务。在一个
数组中对R''B'和W'进行排序。例如,用户输入:
RBWWBBRWWRRWRBW
i需要交换数组中的字符并将其排列成
RRRRRWWWWWWBBBB
------------------------------- -------------------------------------
i我在想,如果可以的话设置R'= 1 W'= 2 B'= 3然后将其从小到大排序为
。
这就是我到目前为止。
#include< stdio.h>
#include< string.h>
#define MAX 81
void getchars(char * flag){
char''R''= 1;
char''W''= 2;
char''B''= 3;
printf(" Please Enter Letters R''W'' s B''Only \ n);
得到(旗帜);
}
int getlargest(double array [],int size){
int index = 0;
double big = array [index];
int i ;(b = b; i< size; i ++){
if(array [i]>大;
指数= 1;
}
}
返回指数;
}
void sortFlag(char array [],int size){
int end;
int large;
int lastplace = size -1;
double temp;
for(end = lastplace; end> 0; end - ){
large = getlargest(array,end + 1);
temp = array [large];
array [index] = array [end];
数组[end] = temp;
}
}
int main(){
char flag [MAX];
getchars(flag);
printf(flag);
返回0;
}
my professor give me this assignment. Sort the R''s B''s and W''s in an
array. for example, the user enter:
R B W W B B R W W R R W R B W
i need to swap the characters in the array and arrange it into
R R R R R W W W W W W B B B B
--------------------------------------------------------------------
i am thinking if i can set the R''s = 1 W''s=2 B''s =3 then sort it out as
number from small to largest.
this is what i have so far.
#include <stdio.h>
#include <string.h>
#define MAX 81
void getchars(char *flag ) {
char ''R''=1;
char ''W''=2;
char ''B''=3;
printf("Please Enter Letters R''s W''s B''s Only\n");
gets(flag);
}
int getlargest(double array[ ], int size) {
int index=0;
double big = array [ index];
int i;
for( i=1; i < size; i++) {
if (array[i]> big;
index=1;
}
}
return index;
}
void sortFlag( char array[ ], int size) {
int end;
int large;
int lastplace= size -1 ;
double temp;
for (end=lastplace; end> 0; end --) {
large=getlargest(array, end+1);
temp=array[large];
array[index]=array[end];
array[end]=temp;
}
}
int main () {
char flag[MAX];
getchars(flag);
printf(flag);
return 0;
}
推荐答案
Ba ******** @ gmail.com 写道:
我的教授给我这个任务。在一个
数组中对R''B'和W'进行排序。例如,用户输入:
我需要交换数组中的字符并将其排列成
RRRRRWWWWWWBBBB
---------------------------------------------- ----------------------
我在想是否可以设置R'= 1 W'= 2 B' = 3然后将数字从小到大排序。
这就是我到目前为止。
#include< stdio.h>
#include< string.h>
#define MAX 81
void getchars(char * flag){
char''R''= 1;
char''W''= 2;
char''B''= 3;
printf(请输入字母R''W''B''只有\ n;
得到(旗帜);
my professor give me this assignment. Sort the R''s B''s and W''s in an
array. for example, the user enter:
R B W W B B R W W R R W R B W
i need to swap the characters in the array and arrange it into
R R R R R W W W W W W B B B B
--------------------------------------------------------------------
i am thinking if i can set the R''s = 1 W''s=2 B''s =3 then sort it out as
number from small to largest.
this is what i have so far.
#include <stdio.h>
#include <string.h>
#define MAX 81
void getchars(char *flag ) {
char ''R''=1;
char ''W''=2;
char ''B''=3;
printf("Please Enter Letters R''s W''s B''s Only\n");
gets(flag);
1st,永远不会使用得到。你可以使用fgets或scanf(如果你需要小心的话)
另外,正确的缩进会有所帮助。
< SNIP>
至于
1st, Never EVER EVER use gets. You can use fgets, or scanf (if you are
careful)
Also, proper indenting would help.
<SNIP>
As for th
ooo谢谢。因为当我试图编译它。它一直给我错误
直到我把*放在旗帜前。但我有一个问题。
如果我指定R'= 1,W'= 2,B'= 3然后我对它进行排序是否有效br />
从小到大?
ooo thanks. because when i try to complie it. it keep giving me errors
not until i put *in front of flag. but i have one question.
Does it work if i assign the R''s = 1 , W''s =2 , B''s = 3 then i sort it
out from small to biggest?
Ba ******** @ gmail.com 写道:
我的教授给我这个作业。在一个
数组中对R''B'和W'进行排序。例如,用户输入:
我需要交换数组中的字符并将其排列成
RRRRRWWWWWWBBBB
---------------------------------------------- ----------------------
我在想是否可以设置R'= 1 W'= 2 B' = 3然后将数字从小到大排序。
这就是我到目前为止。
#include< stdio.h>
#include< string.h>
#define MAX 81
void getchars(char * flag){
char''R''= 1;
char''W''= 2;
char''B''= 3;
printf(请输入字母R''W''B''只有\ n");
获取(标记);
} int getlargest(double array [],int size){
int index = 0;
double big = array [index];
int i;
for(i = 1; i< size; i ++){
if(array [i]&g t; big;
index = 1;
}
返回索引;
}
void sortFlag(char array [], int size; {
int end;
int large;
int lastplace = size -1;
double temp;
for(end = lastplace;端> 0;结束 - ){
large = getlargest(array,end + 1);
temp = array [large];
array [index] = array [end];
array [end] = temp;
}
int main(){
char flag [MAX];
getchars(flag );
printf(flag);
返回0;
}
my professor give me this assignment. Sort the R''s B''s and W''s in an
array. for example, the user enter:
R B W W B B R W W R R W R B W
i need to swap the characters in the array and arrange it into
R R R R R W W W W W W B B B B
--------------------------------------------------------------------
i am thinking if i can set the R''s = 1 W''s=2 B''s =3 then sort it out as
number from small to largest.
this is what i have so far.
#include <stdio.h>
#include <string.h>
#define MAX 81
void getchars(char *flag ) {
char ''R''=1;
char ''W''=2;
char ''B''=3;
printf("Please Enter Letters R''s W''s B''s Only\n");
gets(flag);
}
int getlargest(double array[ ], int size) {
int index=0;
double big = array [ index];
int i;
for( i=1; i < size; i++) {
if (array[i]> big;
index=1;
}
}
return index;
}
void sortFlag( char array[ ], int size) {
int end;
int large;
int lastplace= size -1 ;
double temp;
for (end=lastplace; end> 0; end --) {
large=getlargest(array, end+1);
temp=array[large];
array[index]=array[end];
array[end]=temp;
}
}
int main () {
char flag[MAX];
getchars(flag);
printf(flag);
return 0;
}
只是看了一下代码,我注意到了数组flag [MAX]是
local。让我们看看我是否可以正确地说出这一点。当这传递给
getchars()和getchars()存储
信息时,它在一个局部变量中。换句话说,当
函数返回时,信息将不再出现在数组中。
也许我错了。
Chad
Just glancing over the code, I noticed that the array flag[MAX] is
local. Let''s see if I can word this correctly. When this gets passed to
getchars() and getchars() stores the
information, it is in a local variable. In other words, when the
function returns, the information will no longer be in the array.
Maybe I''m wrong.
Chad
这篇关于帮助我排序字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!