arrays doubt

This is a discussion on arrays doubt within the C Programming forums, part of the General Programming Boards category; I've a little doubt in arrays, i'm trying to make a program that change, the 1st element of an array ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    6

    arrays doubt

    I've a little doubt in arrays,

    i'm trying to make a program that change, the 1st element of an array ( v[0] ) for the last one ( v[5] for example).

    i call the funcion change:
    Code:
    void change(int v[], int x){
    int i , t , x;
    for(i=0;i<=n-1;i++){
    x=v[0];
    t=v[n-1];
    v[0]=t;
    v[n-1]=x;
    }
    for(i=0;i<=n-1;i++){
    printf("%d\n", v[i]);
    }
    }
    and i use these main:
    Code:
    int main(){
    int a[SIZE]={8,5,6,9,1,3}, m=6, i;
    
    for(i=0;i<=m-1;i++){
    printf("%d\n"a[i]);
    }
    
    printf("\n\n");
    
    change(a,m);
    }
    Can anybode tell me what's wrong in the function??
    thank u, johnny!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by j0nnyX
    I've a little doubt in arrays,
    i call the funcion change:
    Code:
    void change(int v[], int x){
    int i , t , x;
    for(i=0;i<=n-1;i++){
    x=v[0];
    t=v[n-1];
    v[0]=t;
    v[n-1]=x;
    }
    for(i=0;i<=n-1;i++){
    printf("%d\n", v[i]);
    }
    }
    Can anybode tell me what's wrong in the function??
    thank u, johnny!
    Yeah, your compiler can. Did you even try to compile that? For starters, there is no variable named n in the function. Yeah. That'll be a problem...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    so, i should substitute n for the variable x?

    yes i try to compile, and the program print 2 equal arrays, equal to the original one.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    You're shadowing x that's being passed to your function by declaring a second one.
    Code:
    void change(int v[], int x){
    int i , t , x;
    You should really pay attention to your compiler warnings and errors.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by j0nnyX
    yes i try to compile, and the program print 2 equal arrays, equal to the original one.
    No it didn't. Not that code. There is no variable n, as I've stated. It will not compile the way you've posted it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 11:06 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21