用户输入保存在矢量中的N个数字序列。程序首先向向量元素的左侧移动,并将0分配给最后一个元素。然后它转移到新向量元素的右侧,并将0分配给第一个元素。
问题:我无法正确转移到正确的位置,导致输出错误,但我不明白我犯了什么错误。请参阅//SHIFT TO THE RIGHT//
和vet[0]=0
。
#include <stdio.h>
int main(void)
{
const int maxn=300; //max vector dimension
int N; //vector dimension
int vet[maxn];
int i; //index
do
{
printf("how many numbers will be entered?");
scanf("%d", &N);
if (N>maxn || N<=0)
{
printf("ERROR.The number must be between 0 and %d\n", maxn);
}
printf("\n");
}
while (N>maxn || N<=0);
printf("Enter a sequence of %d numbers\n", N);
printf("\n");
for (i=0; i<N; i++)
{
printf("element number %d: ", i+1);
scanf("%d", &vet[i]);
}
printf("\n");
printf("the sequence entered is:\n");
for (i=0; i<N; i++)
{
printf("%d ", vet[i]);
}
printf("\n");
//SHIFT TO THE LEFT//
//1 2 3 4 -> 2 3 4 0//
for(i=0; i<N-1; i++)
{
vet[i]=vet[i+1];
}
vet[N-1]=0;
printf("The resulting sequence from the shift to the left is:\n");
printf("\n");
for (i=0; i<N; i++)
{
printf("%d ", vet[i]);
}
printf("\n");
//SHIFT TO THE RIGHT//
// 2 3 4 0 -> 0 2 3 4//
for (i=N-1; i>0; i--);
{
vet[i]=vet[i-1];
}
vet[0]=0;
printf("The resulting sequence from the shift to the right is: \n");
printf("\n");
for (i=0; i<N; i++)
{
printf("%d ", vet[i]);
}
printf("\n");
return(0);
}