Thread: 1-D array

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    You're also assigning into arr_new[5] which is out of bounds
    yes i correct it and it worked

    thanks

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    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);
    }

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    i think i wont be able to submet it to the prof.

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    thank u
    Salem

  9. #24
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Huh?
    Think about this better.

    [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?

  10. #25
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I'm sorry, I didn't notice you were using two different arrays on the count of how unnecessarily inefficient that is.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM