#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    12

    how do I turn this arrayinto a larger array?


    I have this array and I want to make it a 240 by 300 array instead, how would I do that?

    #include<stdio.h>

    #define YOUR_SIZE_LIMIT 32
    #define YOUR_ARRAY_SIZE 100

    int
    main()
    {
    typedef struct {
    char x1[YOUR_SIZE_LIMIT];
    char x2[YOUR_SIZE_LIMIT];
    float x3;
    char x4;
    } yourStruct;

    yourStruct a[YOUR_ARRAY_SIZE];

    char input_fname[256];
    int i = 0;
    int records_read;
    FILE *fp;

    printf("\nWelcome........\n");
    printf("Please enter the name of the text file: ");

    scanf("%s", input_fname);

    if((fp = fopen(input_fname, "r")) == NULL)
    {
    printf("Error opening data file\n");
    exit(-1);
    }

    while(i<YOUR_ARRAY_SIZE &&
    fscanf(fp, "%s %s %f %c", a[i].x1, a[i].x2, &a[i].x3, &a[i].x4) != EOF)
    {
    i++;
    }

    fclose(fp);

    records_read = i;

    for(i=0;i<records_read;i++)
    {
    printf("%s %s %f %c\n", a[i].x1, a[i].x2, a[i].x3, a[i].x4);
    }
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Allocate memory for a larger array, then copy all elements from array1 to array2. You can also use the C function realloc() (dunno the C++ equivelent) which effectively does the same thing for you (of course, that only works on dynamically allocated memory, not on statically allocated memory). If you use realloc() be sure to use a temp pointer and check the return value, if it fails your original memory is still intact and needs to be free'ed.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: how do I turn this arrayinto a larger array?


    Originally posted by clowns119
    I have this array and I want to make it a 240 by 300 array instead, how would I do that?
    Please use code tags for posting code. I am not sure what you are asking. Are you asking to change your program to have a 240x300 sized array? I see no 2 dimensional arrays in your program, so I assume this is not the case. If it is, please show only the relevant code for your problem. Or are you asking how to change the size of an array at runtime? This cannot be done. Either allocate enough space originally for the maximum future size, or use dynamic allocation for the original, then dynamically allocate the new size, copy the old to the new (if you wish to maintain the data from the old), and delete the old.
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by mitakeet
    ...You can also use the C function realloc()...
    I did not realize until now that realloc maintains the data in the new block. From the MSDN realloc() page: "The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location." :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    12
    I don't know much about arrays, I got this from someone who was trying to help me. I have a .txt file which lists a lot of numbers and I want those numbers to be displayed in a 240X300 array. I guess I am going about it the wrong way so how would I do it?
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Generally speaking we don't do your work for you, we help you with problems in your code. Ask the person who gave you the code or hope someone will be kind here. Or, you can take a stab at doing it yourself and then ask us for help with YOUR code.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by clowns119
    I guess I am going about it the wrong way so how would I do it?
    Yes, you are. You should start coding something from scratch. First of all, start with a small program that deals with a single dimension array. Then move on to 2-dimensional arrays. Then move on to reading information from a file. And, finally combine the last 2 to make your program. You should learn incrementally, so you can learn the details of each step - rather than be blasted with 100 errors in one large program of which you know nothing about. mitakeet is right - we are not going to do this work for you. If you have problems with a specific thing in one of your own programs, let us know, and we are here to help.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    12
    Can you at least recomend any good tutorials or books for learning these steps?
  16. #9
  17. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    I can recommend a decent book, that is well worth buying:
    Beginning Visual C++ 6 by Ivor Horton

    The only other option is to learn from an online tutorial. Try searching google:
    Goole - C++ Tutorial

    I highly recommend reading a decent book. Online tutorials are never really that great, in my experience.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    12
    ok I have a new question, I am tryin to learn from scatch and first I learned how to create a 1D array and that worked great and I am good at that now. The next step is creating a 2D array and I tried with this code:
    #include<iostream.h>

    int main()

    {
    int Temp [4][5]={33,34,34,34,34,45,56,67,78,79,78,65,65,65,65,65,65,65,65,65};

    for(int x=0;x<4;++x);
    for(int y=0;y<5;++y)
    cout<<Temp[x][y]<<endl;

    return 0;
    }

    and I didn't get any errors and the program compiled and ran but the output it gave me was not a 2D array it was a few lines of weird numbers. What am I doing wrong?
  20. #11
  21. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I am going to stop responding to your posts if you keep asking the same question in different posts.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  22. #12
  23. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Yes, please stop asking the same question in more than one thread. The questions in each thread you have started are all basically the same problem, and you should have never started more than one thread on this problem.

    It looks to me like you need a tutorial on C/C++. You are doing the right thing by starting with smaller programs, but you should not be asking about problems dealing with syntax, when it is obvious that you have not even looked at a tutorial, yet.

    The proper way to initialize a 2D array is like so:
    Code:
    long data[2][4] = {
                         { 1,  2,  3,  5 },
                         { 7, 11, 13, 17 }
                      };

IMN logo majestic logo threadwatch logo seochat tools logo