# 1-D array

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 05-12-2006
SlyMaelstrom
You're also assigning into arr_new[5] which is out of bounds. Good attempt, but consider what I said about two nested for loops or simply reworking the values of the i and j variables as they loop. ...and to blumfluff, look at his results and see why I said you shouldn't post broken code. Now the guy is even more confused then he was to start.
• 05-12-2006
jack999
Quote:

You're also assigning into arr_new[5] which is out of bounds
yes i correct it and it worked

thanks
• 05-12-2006
jack999
guys i really got confused when u tryed yo solve iy with functions

Code:

```#include<stdio.h> void read(float arr[],int n) { int        i ; for (i=0 ; i<n ; ++i)       { printf ("Enter the %dth element of array :", i) ; scanf ( "%f", &arr[i] ) ;       } void swp(float arr[],float arr_new[],int n) { int i,j, temp; for( i = 0, j = n-1; i<n; i++,j--) { temp = arr[i]; arr[i] = arr_new[j]; arr_new[j] = temp; } } void display(float arr_new[]); { int i; for (i=0 ; i<n ; ++i)       { printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;       } } void main() { float a[],b[], c[]; int m; printf ("Enter the size of array :") ; scanf("%d",&m); read(a,m); swap(a,b,m); display(c); }```
• 05-12-2006
Salem
> void main()
main returns an int

> float a[],b[], c[];
You still need a size when you declare your arrays.

> void display(float arr_new[]);
You don't need that ; at the end of this line
• 05-12-2006
jack999
i think i wont be able to submet it to the prof.
• 05-12-2006
You don't need arr_new in swp(), you only need arr and you only need to swap while j > i, otherwise you swap right back again and end up where you started if you swap until i >= n.
• 05-12-2006
Salem
Well it compiles now
Code:

```#include<stdio.h> void read(float arr[], int n) {     int i;     for (i = 0; i < n; ++i) {         printf("Enter the %dth element of array :", i);         scanf("%f", &arr[i]);     } }                              /*!! this was missing */ /*!! check spelling */ void swap(float arr[], float arr_new[], int n) {     int i, j;     float temp; /*!! same type as the arrays */     for (i = 0, j = n - 1; i < n; i++, j--) {         temp = arr[i];         arr[i] = arr_new[j];         arr_new[j] = temp;     } } void display(float arr_new[], int n)  /*!! added n parameter */ {     int i;     for (i = 0; i < n; ++i) {         printf("the new array value of element #%d is %f \n", i,               arr_new[i]);     } } int main() {     float a[5], b[5];     int m;     printf("Enter the size of array :");     scanf("%d", &m);     read(a, m);     swap(a, b, m);     display(b, m);     return 0; }```
• 05-12-2006
jack999
thank u
Salem
• 05-12-2006
cunnus88
Huh?
Quote:

[0] = 5 - [1] = 3 - [2] = 7 - [3] = 9
N = 3

i_new array[0] = i_new_array[3];

[0] = 9 - [1] = 3 - [2] = 7 - [3] = 9

i_new array[1] = i_new_array[2];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

i_new array[2] = i_new_array[1];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

i_new array[3] = i_new_array[0];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

See? You gotta have that temp variable.
I'm not sure I get the point.

The following works fine for me (though it wouldn't be allowed for the poster's homework):
Code:

```        int arr1[4] = {1, 2, 3, 4};         int arr2[4];         for(int i = 0; i < 4; i++)                 arr2[i] = arr1[3 - i];```
I guess a more correct, generalized version would be:
Code:

```        for(int i = 0; i < N; i++)                 array1[i] = arr2[N - 1 - i];```
Why would it be impossible to switch the ordering on an array without a third temp variable?
• 05-12-2006
SlyMaelstrom
I'm sorry, I didn't notice you were using two different arrays on the count of how unnecessarily inefficient that is. :)
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12