Function could replace first n elements from array A with last n elements of array B.
Output is
Array A
1 2 3 4 5
Array B
6 7 8 9 10
I tried to put printf (i)
in every loop and it seems to be work fine but it do nothing with arrays a and b :(
void reparray(int *a, int *b, int n){
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i<n; i++){
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element>=n; last_element--){
help[last_element] = b[last_element];
}
i = 0;
for (i; i<n; i++){
a[i] = help[i];
}
last_element = 4;
for (last_element;last_element >= n; last_element--){
b[last_element] = help[last_element];
}
}
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int b[5] = {6, 7, 8, 9, 10};
int n,i;
reparray(a, b, 2);
printf("Array A\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", a[i]);
}
printf("Array B\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", b[i]);
}
return 0;
}
your 2 last loops in void reparray
do nothing except copying exact things which were already in a and b
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4-1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
i = 0;
for (i; i< n; i++) {
b[i] = help[i];
}
}
for other input change void reparray
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4 - 1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
last_element = 4;
int first_element = 0;
for (i = last_element - n + 1; i < last_element + 1; i++) {
b[i] = help[first_element];
first_element++;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.