Write a function, which takes an array and two other paramters of int type, that
ID: 3824952 • Letter: W
Question
Write a function, which takes an array and two other paramters of int type, that can move the previous (n-m) elements in a n-element array towards the end in sequence, and the last m elements move to the front in sequence.
Example:
How many numbers? 8
Input 8 numbers:
1 2 3 4 5 6 7 8
How many place you want to move? 4
Now, they are:
5 6 7 8 1 2 3 4
#include <stdio.h>
move(int array[20], int n, int m){
//write your code here
}
void main(){
int number[20], n, m, i;
printf("How many number?");
scanf("%d", &n);
printf("Input %d numbers: ", n);
for (i = 0; i < n; i++){
scanf("%d", &number[i]);
}
i = 0;
printf("How many place you want to move?");
scanf("%d", &m);
move(number, n, m);
printf("Now, they are: ");
for (i = 0; i < n; i++){
printf("%d ", number[i]);
}
getchar();
getchar();
}
Explanation / Answer
#include <stdio.h>
move(int array[20], int n, int m)
{int i,j,k,new[20],result[20];
for( i=0;i<=m;i++)
new[i]=array[i];
for(i=0, j=m+1;i<=m,j<=n;i++,j++)
{result[i]=array[j];
if(i>m)
{for(i=m+1,k=0;i<=n,k<=m;i++,k++)
result[i]=new[k];
}
}
}
void main(){
int number[20], n, m, i;
printf("How many number?");
scanf("%d", &n);
printf("Input %d numbers: ", n);
for (i = 0; i < n; i++){
scanf("%d", &number[i]);
}
i = 0;
printf("How many place you want to move?");
scanf("%d", &m);
move(number, n, m);
printf("Now, they are: ");
for (i = 0; i < n; i++){
printf("%d ", number[i]);
}
getchar();
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.