Thread: returning an array of int

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    returning an array of int

    Before I had problems passing an int array, now it is returning it...

    Areas of importance is the last function "int* ReadPrices(void)" and lien 25.

    Thanks!

    data.dat is:
    Code:
    5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
    My code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void seatChart (int AvailableSeats[15][20]);
    int* ReadPrices(void);
    
    int main()
    {
    
    
        int choice;
    
        int row;
        int seat;
    
        int i;
    
        int AvailableSeats[15][20] = {0}; //seats[row][seat]
    
        int ticketsSold = 0;
    
        int sale;
        int PriceList[16];
    
        PriceList = ReadPrices();
    
    
        for(;;)
        {
            seatChart(AvailableSeats);
    
            printf("\n\nMenu:");
            printf("\n1) Buy ticket");
            printf("\n2) Total sell and exit");
            printf("\n\nEnter your choice : ");
            scanf("%d", &choice);
    
            if (choice == 1)
            {
                printf("\n\nEnter row: ");
                scanf("%d", &row);
    
                printf("\nEnter seat: ");
                scanf("%d", &seat);
    
    
    
                if (row<0 || row >14 || seat<0 || seat>19)
                {
                    printf("Invalid seat choice");
                }
    
                else if(AvailableSeats[row][seat] == 0)
                {
    
                    AvailableSeats[row][seat] = 1;
                    ticketsSold++;
                    sale = sale + PriceList[row];
                }
    
    
                else
                {
                    printf("\nSeat is already taken");
                }
    
    
    
    
                continue;
            }
    
    
    
            if(choice == 2)
            {
                printf("\n\nTOTAL TICKETS SOLD: %d", ticketsSold);
                printf("\nTOTAL REVENUE: Y");
                break;
            }
    
            printf("Invalid choice");
            ticketsSold--;
    
        }
    
    
        return 0;
    }
    
    void seatChart (int AvailableSeats[15][20])
    {
        int i;
        int o;
    
        printf("\n* Seats available");
        printf("\n# Reserved Seats");
        printf("\nSeats:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19");
    
    
    
        for(o = 0; o<=14; o++)
        {
            printf("\nRow %2d", o);
    
            for(i = 0; i<=19; i++)
            {
    
                if(AvailableSeats[o][i] == 1)
                {
                    printf("  #");
                }
    
                else
                {
                    printf("  *");
                }
            }
        }
    
    }
    
    int* ReadPrices(void)
    {
        int i;
        int tempPriceList[16];
    
        FILE* fts;
        fts = fopen("data.dat", "r");
    
        for(i = 0;i<=14;i++)
        {
            fscanf(fts, "%d ", &tempPriceList[i]);
        }
    
        for(i=0;i<=14;i++)
        printf("%d ", tempPriceList[i]);
    
        return tempPriceList;
    }
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Get the caller to pass a pointer to the first element of the array, then modify the array through that pointer. What you are doing now is returning a pointer to the first element of an array that is about to be destroyed as it goes out of scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Also take note that on line 25 you are trying to change the address of the array which will not work, think about array as a constant pointer you cannot modify it. Do what laserlight suggested pass the array as a function parameter.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Why return an array? Doesnt the function change the original in main?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2010, 06:33 AM
  2. Help with returning an array
    By HIT_Braga in forum C Programming
    Replies: 16
    Last Post: 04-09-2010, 03:24 AM
  3. Returning Array
    By Suchy in forum C Programming
    Replies: 4
    Last Post: 03-02-2008, 11:05 PM
  4. Returning Array
    By baffa in forum C Programming
    Replies: 26
    Last Post: 02-01-2008, 10:08 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM