arrays on c

This is a discussion on arrays on c within the C Programming forums, part of the General Programming Boards category; hello guys i have read the c primer plus 5th edition by stephen prata and he give a problem exercises ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    arrays on c

    hello guys i have read the c primer plus 5th edition by stephen prata and he give a problem exercises on chapter 10 about arrays and function on no. 6 problem i don't know how to do it....because he says that you can copy a 2 array with a 1 array function....can some please teach how to do it...or for further info..read the "c primer plus"thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Consider an M by N array an array with M * N elements.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    i think i didn't make my questions clear..... Here's the question in chapter 10..problem 2....
    Problem 2:
    Write a program that initializes an array-of-double and then copies the contents of the array into two other arrays. (All three arrays should be declared in the main program.) To make the first copy, use a function with array notation. To make the second copy, use a function with pointer notation and pointer incrementing. Have each function take as arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations:
    Code:
    double source[5] = {1.1, 2.2, 3.3., 4.4, 5.5};
    double target1[5];
    double target2[5];
    copy_arr(source, target1, 5);
    copy_ptr(source, target1, 5);
    -------
    and here's my answer to that problem...
    Code:
    #include <stdio.h>
    void show(double *,int);
    void copy_ptr(double *, double *, int);
    void copy_arr(double [],double [], int);
    int main(void)
    {
        double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
        double target1[5];
        double target2[5];
        copy_arr(source,target1,5);
        copy_ptr(source, target1, 5);
        show(source,5);
        show(target1,5);
        show(target1,5);
        return 0;
    }
    void copy_arr(double ar[],double b[], int a)
    {
         int c;
         for(c=0;c<a;c++)
         b[c]=ar[c];
    }
    void copy_ptr(double *ar, double *b, int a)
    {
         int c;
         for(c=0;c<a;c++)
         *(ar+c)=*(b+c);
    }
    void show(double *ar, int a)
    {
         int c;
         for(c=0;c<a;c++)
         printf("&#37;g\t",*(ar+c));
         printf("\n");
    }
    -------
    no problem in that question...it is very simple...but on problem 6....here it goes
    Write a program that initializes a two-dimensional array-of-double and uses one of the copy functions from exercise 2 to copy it to a second two-dimensional array. (Because a two-dimensional array is an array of arrays, a one-dimensional copy function can be used with each subarray.)

    to use one of the function of my answer in problem 2....like for example this kind of function that i've arrived in problem 2....
    -------------
    Code:
    void copy_arr(double ar[][], double *, int);
    <---in this line i change the 1st argument because we gonna pass a 2-dimensional array....but it would error in the 2nd argument because of the compiler warning...saying that the argument is not right to pass an 2-dimensional array on a 1 dimensional array......did you get my question???thnx for replying

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Code:
    void copy_arr(double ar[],double b[], int a)
    {
         int c;
         for(c=0;c<a;c++)
         b[c]=ar[c]; // You failed to indent here
    }
    Code:
    void copy_ptr(double *ar, double *b, int a)
    {
         int c;
         for(c=0;c<a;c++)
         *(ar+c)=*(b+c); // This is wrong. You were supposed to increment the pointer! You also forgot to indent.
    }
    but it would error in the 2nd argument because of the compiler warning...saying that the argument is not right to pass an 2-dimensional array on a 1 dimensional array......did you get my question???thnx for replying
    Yes, because a 2D array is not a 1D array, so you can't expect the compiler to make it work if you pass a 2D array where it expects a 1D array.
    Anyhow, you don't need to modify the function at all.
    Take a closer look at the description:
    (Because a two-dimensional array is an array of arrays, a one-dimensional copy function can be used with each subarray.)
    (It basically means a 2D array is an array of 1D arrays.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Nagash's Avatar
    Join Date
    Sep 2008
    Location
    Brazil - SC, Florianopolis
    Posts
    3
    Hey guys do u know a way I can capitalize everything writen in a specific type of char array?...For example:

    Code:
    typedef char Str[50]; //I want everything in this array to be TOUPPERed
    
    typedef struct {
    	Str title, director, genre, year, available, code; // all will be in a struct
    	}MOVIE;
    I couldn't manage to make common toupper when I read them... the following allways shows an error: "request for member `title' in something not a structure or union"

    Code:
    void movielist (MOVIE list[50]){
        
    	puts("Type in the movie Title:");
    	gets((*list).title);
                    list.title=toupper(list.title);   
    }
    Isn't there a way I could allready toupper as soon as I declared my Str[50] or even the struct MOVIE?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    4x3:

    [0x0][0x1][0x2][1x0][1x1][1x2][2x0][2x1][2x2][3x0][3x1][3x2]

    12 element array:

    [0][1]2][3][4][5][6][7][8][9][10][11]

    4x3 as 12 element array

    [0x0][0x1][0x2][0x3][0x4][0x5][0x6][0x7][0x8][0x9][0x10][0x11]

    4 * 3 = 12

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Nagash View Post
    Hey guys do u know a way I can capitalize everything writen in a specific type of char array?...For example:

    Code:
    typedef char Str[50]; //I want everything in this array to be TOUPPERed
    
    typedef struct {
    	Str title, director, genre, year, available, code; // all will be in a struct
    	}MOVIE;
    I couldn't manage to make common toupper when I read them... the following allways shows an error: "request for member `title' in something not a structure or union"

    Code:
    void movielist (MOVIE list[50]){
        
    	puts("Type in the movie Title:");
    	gets((*list).title);
                    list.title=toupper(list.title);   
    }
    Isn't there a way I could allready toupper as soon as I declared my Str[50] or even the struct MOVIE?
    toupper works on a character-by-character basis, not on a string as a whole. So you would have to go through your title character-by-character and toupper each character.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    And don't use gets! It's leads to the abyss!
    http://cpwiki.sourceforge.net/Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just compile my sig to see what Elysia is talking about. And as an extra added bonus, when it prompts you for user input, type in something like:

    I like getting 100&#37;s better than 50%s on my tests.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    Code:
    void copy_arr(double ar[],double b[], int a)
    {
         int c;
         for(c=0;c<a;c++)
         b[c]=ar[c]; // You failed to indent here
    }
    i don't get it elysia.....as far as i know my codes are 100% working...this is just a sample of my answer in problem 2....
    Code:
    void copy_ptr(double *ar, double *b, int a)
    {
         int c;
         for(c=0;c<a;c++)
         *(ar+c)=*(b+c); // This is wrong. You were supposed to increment the pointer! You also forgot to indent.
    }
    and you're correction about the pointer...that is the pointer that i am incrementing....
    it goes like this right...
    Code:
    int ar[5]; \\an array of 5 1d array
    int *ptr; \\a pointer
    ptr=ar; \\assigning the address of the ar to the ptr....
    so if you would like to access the 2nd element you would have to increment the pointer right....
    then here it is....
    *(ptr+1) \\since 0 is the 1st element then we add 1 to get to the 2nd element....
    yah...i got the description but it would make a compiler error....compile it to belief it..that's why i am posting it here so that i could find the answer....btw thanks for you're effort

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    void copy_ptr(double *ar, double *b, int a)
    {
         double *end = ar + a;
    
         for(;ar < end;)
             *ar++=*b++;
    }
    Is what I believe she is telling you to do.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You may also wish to double check the parameters to make sure they are not NULL.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by sick View Post
    i don't get it elysia.....as far as i know my codes are 100&#37; working...this is just a sample of my answer in problem 2....
    Yes, it works, but it's a matter of readability.

    and you're correction about the pointer...that is the pointer that i am incrementing....
    it goes like this right...
    Code:
    int ar[5]; \\an array of 5 1d array
    int *ptr; \\a pointer
    ptr=ar; \\assigning the address of the ar to the ptr....
    so if you would like to access the 2nd element you would have to increment the pointer right....
    then here it is....
    *(ptr+1) \\since 0 is the 1st element then we add 1 to get to the 2nd element....
    No...
    This isn't incrementing at all.
    You're just dereferencing a relative location.
    You know of the increment operators, yes?

    yah...i got the description but it would make a compiler error....compile it to belief it..that's why i am posting it here so that i could find the answer....btw thanks for you're effort
    Well, if you understood the description, then you wouldn't modifying your function at all...
    As I mentioned, a 2D array is an array of arrays.
    So int n[5][50] would be 5 arrays of arrays consisting of 50 elements.
    So in other words, to access an array in that array, you simply use its index, like n[0] gives you the first array.
    You don't need to modify the function at all - but instead modify the code that calls the function!

    Quote Originally Posted by master5001 View Post
    Example:
    Is what I believe she is telling you to do.
    Yeah, but you don't need to hand out the solution.
    But you're right in that it increments the pointer instead of dereferencing a relative location.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well handing out solutions is my middle name. I had it legally changed back in 2006 after a mishap in Mexico. I was going to change the code to psuedo code to appease your perfectly valid point, however its hard to make this particular concept into psuedo code...

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You can show an example of what incrementing a pointer means.
    const char* p = "My love!";
    printf("&#37;c", *p);
    p++;
    printf("%c", *p);
    // etc
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 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