① c语言中二维数组如何交换行
1.使用循环语句对于指定行的每列的数一个一个进行交换即可。
2.例程:
#include<stdio.h>
#include<string.h>
ints[1000][1000];
intn,m,i,j,k,temp;
intmain(){
scanf("%d%d,&n,&m);//二维数组的行数n,列数m
for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",s[i]+j)//得到二维数组
for(j=0;j<m;j++){//交换第三行和第五行的数
temp=s[3][j];
s[3][j]=s[5][j];
s[5][j]=temp;
}
for(i=0;i<n;i++){
for(j=0;j<m;j++)printf("%d",s[i][j])//输出交换后的数组
printf(" ")
}
return0;
}
② C语言中,怎么交换指针数组里的值
程序主要通过冒泡法使用函数strcmp比较两指针所值字符串大小,通过临时指针交换两指针指向,实现字符串排序,代码如下,
//程序功能,从小到大排序10个字符串
#include <stdio.h>
#include <string.h>
void bubble_sort(char *a[], int n);
int main(int argc, char *argv[])
{
char a[10][20]={0};//存储10个字符串
char *p[10];//指向10个字符串
int i=0;
for(i=0;i<10;i++)
{
gets(a[i]);//输入字符串
p[i]=a[i];//指向字符串
}
for(i=0;i<10;i++)
{
puts(a[i]);//输出原字符串
}
bubble_sort(p,10);
for(i=0;i<10;i++)
{
puts(p[i]);//输出排序后字符串
}
return 0;
}
//冒泡法排序,从小到大排序
void bubble_sort(char *a[], int n)
{
int i, j;
char *temp;
for (j = 0; j < n - 1; j++)
for (i = 0; i < n - 1 - j; i++)
{
if(strcmp(a[i],a[i+1])>0)//交换指针
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
int strcmp( const char *str1, const char *str2 );比较字符串str1、str2,返回值大于0,str1>str2,返回值小于0,str1<str2,返回值等于0,str1==str2。
③ 在C语言中怎么把一个数组(一维数组)的后面的数与前面的数交换
数组的大小知道吗?要是知道大小n的话对于数组a[n]
那第一个就是a[0],最后一个是a[n-1];
做交换这样
定义一个和数组内相同的数据类型的ttype
temp;
temp=a[n-1];
a[n-1]=a[0];
a[0]=temp;
这样就交换了
④ 在c语言中如何将数组的最大值与最小值互换位置,求大神解答!!!
找到最大和最小值的下标,然后交换,例如:
#include <stdio.h>
main()
{
int a[10]={10,2,3,4,5,6,7,8,9,10},n=10;
int i1,i2,i;
i1=0;
for (i=0;i<n;i++)if (a[i]<a[i1]) i1=i;
i2=0;
for (i=0;i<n;i++)if (a[i]>a[i2]) i2=i;
i=a[i1]; a[i1]=a[i2];a[i2]=i;
for (i=0;i<n;i++) printf("%d ",a[i]);
return 0;
}
⑤ C语言中实现数组间数据交换的方法有哪些谢谢!
一般都用引入第三个变量做为中间参数,如a,b的交换引入变量c,a=c;b=a;b=c;
⑥ C语言数组数字交换
程序完全没有问题啊。
⑦ 在C语言中如何将两个数进行互换
将两个数进行互换的方法有两种形式:
1、借助中间变量完成,此方法直观,易理解,使用最多
2、不需要中间变量,通过变量身的运算完成交换。
参考代码:
方法1:
inta=2,b=3,t;
t=a;//先将a存储到临时变量t中
a=b;//将b存储到a中
b=t;//将临时变量中的原a值存储到b中
printf("a=%db=%d ",a,b);
方法2:
inta=2,b=3,t;
a+=b;//把两数之和存到a中
b=a-b;//用两数和减去b可得原a,存储到b中
a=a-b;//因为b现在是原a值,所以,用两数和减去b(原a)可得原b,存储到a中
此方法,还可以用异或运算来实现,原理相同。
⑧ c语言数组元素交换问题
数组交换的swap函数中
voidSWAP(double*a,double*b)//用于交换数组的元素
{
doubletemp;
temp=*a;
*a=*b;
*b=temp;
}
你传进去的是double*a
但你定义的是doubletemp
类型都不一样怎么赋值!!
⑨ c语言结构体数组怎么交换
定义一个struct cj temp;
然后就直接交换啊。
我把那块做成函数,楼主直接调用吧,主函数就没写完整了。
#include <stdio.h>
struct cj
{
int No;
char Name[20];
int cj1;
int cj2;
int cj3;
int pingjun;
int Number;
};
int main(int argc, char *argv[])
{
struct cj student[50];
void sort(struct cj *student,int n);
sort(student,50);
return 0;
}
void sort(struct cj *student,int n)/*从大到小的*/
{
int flag;
int i,j;
struct cj temp;
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-i-1;j++)
if(student[j].pingjun<student[j+1].pingjun)
{
flag=1;
temp=student[j+1];
student[j+1]=student[j];
student[j]=temp;
}
if(flag==0)
break;
}
}
⑩ C语言数值的交换
楼上的这个
a
=
a+b;
b
=
a-b;
a
=
a-b;
是个方法,是符合我们数学思维的方法,也是最初接触C语言的人可能想到的方法。
但是这样编程很不直观,不如t=a;a=b;b=t;来得快。
似乎在C++中有swap(a,
b)模板函数,直接实现a,b交换。
想玩高级一点的话,可采用“换标不换值”的方法,用数组元素作为数组的下标,这种方法换逻辑不换存储。
#include
<stdio.h>
void
main()
{
int
a=10,b=20;
int
array1[2]
=
{10,20};
//存a、b值
int
array2[2]
=
{0,1};
//存下标
b
=
array1[array2[0]];
a
=
array1[array2[1]];
printf("a=%d,
b=%d\n",a,b);
}
这个方法在对结构体数组值交换中非常好用!因为结构体数组一般每个成员都有很多个值,如:
struct
student
{
int
num;
double
score;
char
name[20];
}stu[5]={{1,98,"ziguowen"},{2,88,"dongda"},{3,78,"haha"}};
//交换stu[0]和stu[1],需要
int
n;
double
s;
char
n[20];
n
=
stu[0].num;
stu[0].num
=
stu[1].num;
stu[1].num
=
n;
s
=
stu[0].score;
stu[0].score
=
stu[1].score;
stu[1].score
=
s;
strcpy(n,
stu[0].name);
strcpy(stu[0].name,
stu[1].name);
strcpy(stu[0].name,s);
//而用下标的话,一个赋值语句即可,直接交换stu[0]
和
stu[1]
下标后全部交换!