所以我想做几件事。首先我想根据用户输入移动数组。因此,让我们说用户输入(按此顺序):1,2,3,4,5。我想将其移动,使其变为2,3,4,5,1。
如您所见,这个特定的阵列是可扩展的,即尺寸不固定。
#include <stdio.h>
void arrayShift(int *a, int intLength);
int main() {
int arr[10] = {0}; //array for input numbers
int array = 0;
printf("Enter the size of the array (MAX): ");
scanf("%d", &array);
printf("Now please enter your %d values: \n", array);
int i;
for (i = 0; i < array; i++) {
scanf("%d", arr+i);
}
return 0;
}
然后我要打印出然后创建一个函数,将每个数字乘以它之前的数字(在移动之后):所以使用与之前相同的数字(2,3,4) ,5,1),我们应该得到2,6,12,20,5的输出。
答案 0 :(得分:1)
这似乎可以简单地完成。对于所描述的&#39;轮换&#39;我们可以将第一个元素复制到最后一个元素之后,更改起始索引并调用它旋转而不移动所有数据。和其他简化:
#include <stdio.h>
int main() {
int number_elements;
printf("Enter the size of the array (MAX): ");
(void) scanf("%d", &number_elements); // should test return value in case of bad input
int array[number_elements + 1]; // array for input numbers (over allocate by 1 for rotation)
printf("Now please enter your %d values:\n", number_elements);
for (int i = 0; i < number_elements; i++)
{
(void) scanf("%d", array + i); // ditto re return value
}
array[number_elements] = array[0]; // rotated array now starts at 1
for (int i = 1; i <= number_elements; i++)
{
printf("%d ", array[i]); // print the rotated array
array[i - 1] *= array[i]; // multiply the rotated array
}
putchar('\n');
for (int i = 0; i < number_elements; i++) // multiplied array starts at 0 again
{
printf("%d ", array[i]); // printing the multiplied, rotated array
}
putchar('\n');
return 0;
}
<强> USAGE 强>
% ./a.out
Enter the size of the array (MAX): 5
Now please enter your 5 values:
1 2 3 4 5
2 3 4 5 1
2 6 12 20 5
%
我不确定你的文字和你的例子是否一致,所以这是我对你想要的假设,但可以根据需要进行调整。
答案 1 :(得分:0)
希望这会有所帮助..
#include <stdio.h>
void arrayRotate(int a[], int intLength)
{
int temp = a[0],t,i; // storing the first element in a temporary variable
for(i=1;i<intLength;i++)
a[i-1] = a[i]; // Left shifting the elements starting from index 1
a[i-1] = temp; // In the last the first index element is restored back
}
void multArr(int a[],int b[],int n)
{
int i;
b[0] = a[0]; // copying the value at index 0 to the new array
for(i=1;i<n;i++)
{
b[i] = a[i]*a[i-1]; // performing the multiplication on the new array
} // referencing the values from the old array
}
int main() {
int arr[10] = {0}, cpy[10]; //array for input numbers
int array = 0;
printf("Enter the size of the array (MAX): ");
scanf("%d", &array);
printf("Now please enter your %d values: \n", array);
int i;
for (i = 0; i < array; i++)
{
scanf("%d", arr+i);
}
arrayRotate(arr,array); // calling function for rotating
multArr(arr,cpy,array); // calling function for the multiplication stuff
for (i = 0; i < array; i++) // printing the rotated array
{
printf("%d ", arr[i]);
}
printf("\n");
for (i = 0; i < array; i++) // printing the multiplied array
{
printf("%d ", cpy[i]);
}
return 0;
}
答案 2 :(得分:0)
好吧,所以这就是我到目前为止所得到的。它有点工作但它也没有。具体来说,问题是当我运行它时,它打印6,12,20,5 1,而不是2,6,12,20,5(这假设数组的数字是1,2,3,4, 5)
#include <stdio.h>
void shift (int arr[], int num);
void multiply (int arr[], int num);
int main() {
int arr[10] = {0}; //array for input numbers
int array = 0;
printf("Enter the size of the array (MAX): ");
scanf("%d", &array);
printf("Now please enter your %d values: \n", array);
int i;
for (i = 0; i < array; i++) {
scanf("%d", arr+i);
}
shift(arr, array);
return 0;
}
//shift function
void shift (int arr[], int num) {
int tempVar; //temporary variable
int i;
tempVar = arr[0]; //setting the temporary variable to the first element of a
for (i = 0; i < num -1; i++) {
arr[i] = arr[i +1];
printf(" %d ", arr[i]);
arr[i -1] *= arr[i];
} //arr[0] = tempVar * arr[0];
arr[i] = tempVar; //i < num, print(arr[i])
printf("%d \n", arr[i]);
for ( int i = 0; i < num; i++) {
printf(" %d ", arr[i]);
}
printf("\n");
}