Dimentioning Arrays

This is a discussion on Dimentioning Arrays within the C Programming forums, part of the General Programming Boards category; Need your opinion about this one: (currently using Turbo C v2.01) Code: #include <stdio.h> int process; main() { char choice; ...

  1. #1
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21

    Dimentioning Arrays

    Need your opinion about this one: (currently using Turbo C v2.01)

    Code:
    #include <stdio.h>
    
    int process;
    
    main() {
        char choice;
        clrscr();
    
        printf ("Process Scheduling\nProgrammed by: Sanchez, Anthony Ray D.\n");
        printf ("\nSelect CPU Scheduling type:\n\n");
        printf ("\t1. First Come, First Served (FCFS)\n");
        printf ("\t2. Shortest Job First (SJF)\n");
        printf ("\t3. Priority Scheduling (PS)\n");
        printf ("\t4. Exit Program\n\n");
        printf ("Enter your desired type: ");
        scanf  ("%c", &choice);
        printf ("Enter no. of process: ");
        scanf  ("%d", &process);
    
        switch (choice) {
            case '1':
                FCFS();
                getch();
                break;
            case '2':
                SJF();
                getch();
                break;
            case '3':
                PS();
                getch();
                break;
            case '4':
                clrscr();
                printf ("The program has been successfully closed.");
                getch();
                exit();
            default:
                printf ("Invalid entry, please try again.\n");
                printf ("Press any key to continue.");
                getch();
        }
    }
    
    FCFS() {
        int ctr, PT[process]; /*I want this to have dimensions based on the value of variable "process" */
        float WT=0, AWT=0;
        clrscr();
    
        printf ("First Come, First Served Algorithm\n");
    
        for (ctr=1; ctr<=process; ctr++) {
            printf ("Enter Processing time for P%d: ", ctr);
            scanf  ("%d", &PT[ctr-1]);
        }
    
        printf ("\nProcess\t\tArrival Time\tCPU Burst\n");
    
        for (ctr=0; ctr<=process-1; ctr++)
            printf ("%d\t\t%d\t\t%d\n", ctr+1, ctr, PT[ctr]);
    
        for (ctr=0; ctr<=process-1; ctr++)
            WT = WT + PT[ctr];
    
        AWT = WT/process;
        printf ("\nAve. Waiting Time (AWT) = %.2f", AWT);
        return;
    } /* Other program lines follows... */
    Although someone here already pointed out the use of dynamic memory, but I dont have idea on how to use/make it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Get a new compiler. There are links around for newer (free) compilers. You're going to have an unnecessary pain in the ass working with near and far pointers if you keep this fossile. Unless you're into that, in which case, to each their own.

    2 - Globals? No need. Pass the value to the function.

    3 - You have to actually prototype the function FCFS() before you can use it. You'll have to do the same with your other functions also. The reason is, when you use them, they're not in scope. Your current function has no record of them, knows nothing about them, and as such, cannot call them correctly.

    4 - You cannot do what you want:
    Code:
    PT[process]; /*I want this to have dimensions based on the value of variable "process" */
    Want to know why? Your compiler is too old. Seriously. It doesn't understand C99, and as such, you cannot use VLAs. Update your compiler, compile as C99, and you'll be able to.

    5 - Use dynamic memory allocation if you really don't feel like upgrading your compiler. There are plenty of tutorials that will explain how. Oh wait, you don't think it's good to use sample code. Hm... I wonder how you'll ever learn anything.


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

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    a sample code which i had

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int process,i;
        int *array;
        
        printf("Enter the array size\n?");
        scanf("%d",&process);
        
        array = malloc(sizeof(int) * process);
        
        for(i=0;i<process;i++)
            scanf("%d",&array[i]);
        
        printf("Your array elements are as follow\n");
        for(i=0;i<process;i++)
            printf("%d\t",array[i]);
        
        free(array);
        getchar();
        return 0;
    }
    /* my ouput    
    Enter the array size
    ?5
    1
    2
    3
    4
    5
    Your array elements are as follow
    1       2       3       4       5
    */
    ssharish2005
    Last edited by ssharish2005; 08-19-2006 at 03:48 AM.

  4. #4
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by quzah
    1 - Get a new compiler. There are links around for newer (free) compilers. You're going to have an unnecessary pain in the ass working with near and far pointers if you keep this fossile. Unless you're into that, in which case, to each their own.

    2 - Globals? No need. Pass the value to the function.

    3 - You have to actually prototype the function FCFS() before you can use it. You'll have to do the same with your other functions also. The reason is, when you use them, they're not in scope. Your current function has no record of them, knows nothing about them, and as such, cannot call them correctly.

    4 - You cannot do what you want:
    Code:
    PT[process]; /*I want this to have dimensions based on the value of variable "process" */
    Want to know why? Your compiler is too old. Seriously. It doesn't understand C99, and as such, you cannot use VLAs. Update your compiler, compile as C99, and you'll be able to.

    5 - Use dynamic memory allocation if you really don't feel like upgrading your compiler. There are plenty of tutorials that will explain how. Oh wait, you don't think it's good to use sample code. Hm... I wonder how you'll ever learn anything.


    Quzah.
    It would be my very pleasure if you'll recommend a good compiler for beginners like me. And oh, in response to your last statement sir, I do use "sample codes" for reference, but never use them as if they were mine.

    (My post in the other thread was misunderstood, i guess.)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish2005
    a sample code which i had
    ...
    ssharish2005
    If you're going to attempt to illustrate dynamic memory, do it correctly. free what you allocate!


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

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > (currently using Turbo C v2.01)
    Get a real compiler, not some fossil
    Dev-c++ seems a popular choice, as does Borland 5.5 command line tools and visual studio express.

    > int process;
    Make this a local variable to main.
    Then pass it as a parameter to FCFS().

    When you have a better compiler, this will force you to prototype ALL your functions instead of this lazy "pass nothing, return nothing, use global vars for everything" style you've got going on.

    > int PT[process];
    Start with
    int *PT;

    Then to create the space, do
    PT = malloc ( process * sizeof *PT );

    When you're all done with the array,
    free( PT );
    You must do this before the function returns, OR you return PT as the result of the function.

    > for (ctr=1; ctr<=process; ctr++)
    > for (ctr=0; ctr<=process-1; ctr++)
    Pick ONE style for indexing through arrays, this mix and match approach will fail you at some point.
    Personally, I prefer
    for (ctr=0; ctr<process; ctr++)

    > printf ("Enter Processing time for P%d: ", ctr);
    Rather than munging the for loop, I would do
    printf ("Enter Processing time for P%d: ", ctr+1);
    Because
    - it's less of a problem if the printout is off by 1
    - it's more obvious that it's wrong when you run the program.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by ssharish2005
    a sample code which i had

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int process,i;
        int *array;
        
        printf("Enter the array size\n?");
        scanf("%d",&process);
        
        array = malloc(sizeof(int) * process);
        
        for(i=0;i<process;i++)
            scanf("%d",&array[i]);
        
        printf("Your array elements are as follow\n");
        for(i=0;i<process;i++)
            printf("%d\t",array[i]);
        
        getchar();
        return 0;
    }
    /* my ouput    
    Enter the array size
    ?5
    1
    2
    3
    4
    5
    Your array elements are as follow
    1       2       3       4       5
    */
    ssharish2005
    Thank you for your time, Sir.
    It answered my question.

    Code:
    array = malloc(sizeof(int) * process);
    /Respect

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > array = malloc(sizeof(int) * process);
    But mine's better....
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by Salem
    > (currently using Turbo C v2.01)
    Get a real compiler, not some fossil
    Dev-c++ seems a popular choice, as does Borland 5.5 command line tools and visual studio express.

    > int process;
    Make this a local variable to main.
    Then pass it as a parameter to FCFS().

    When you have a better compiler, this will force you to prototype ALL your functions instead of this lazy "pass nothing, return nothing, use global vars for everything" style you've got going on.

    > int PT[process];
    Start with
    int *PT;

    Then to create the space, do
    PT = malloc ( process * sizeof *PT );

    When you're all done with the array,
    free( PT );
    You must do this before the function returns, OR you return PT as the result of the function.

    > for (ctr=1; ctr<=process; ctr++)
    > for (ctr=0; ctr<=process-1; ctr++)
    Pick ONE style for indexing through arrays, this mix and match approach will fail you at some point.
    Personally, I prefer
    for (ctr=0; ctr<process; ctr++)

    > printf ("Enter Processing time for P%d: ", ctr);
    Rather than munging the for loop, I would do
    printf ("Enter Processing time for P%d: ", ctr+1);
    Because
    - it's less of a problem if the printout is off by 1
    - it's more obvious that it's wrong when you run the program.
    Thank you, Sir.

    I'll remember all that, its a good thing that I've registered here.
    (2hrs since registration, yet I learned SO MUCH.)

    /Respect

  10. #10
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by quzah
    If you're going to attempt to illustrate dynamic memory, do it correctly. free what you allocate!


    Quzah.
    This caught my interest, I'd love to learn what does it mean.
    "Free what you allocate."

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    > PT = malloc ( process * sizeof *PT );

    this is good which is more dymainc. it would work for all sort of datatypes which u dont need to specify the type like sizeof(int) which is not dynmaic.

    ssharish2005

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Xero
    This caught my interest, I'd love to learn what does it mean.
    "Free what you allocate."
    that post was actually pointing out me for not freed my allocated array. i have updated the code above.

    ssharish2005

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Xero
    This caught my interest, I'd love to learn what does it mean.
    "Free what you allocate."
    There is a function called free which is to be used when you're all done with whatever you've malloc-ed, or calloc-ed. (Those are two functions used to allocate memory.) As Salem also mentioned previously.


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

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