1

So I'm trying to complete a competitive programming problem called "The Grand Dinner" (uva 10249) and I'm having a problem with displaying correct the output because of my temporary list, can anyone please help me out? when I print cout << *(temp[j].t); it displays 7405056 instead of the values of seats[] array. Here's the full code:

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdio>

using namespace std;

int M, N;

struct temp_type
{
    int t;
    int index;
} temp[51];

bool cmp(temp_type a, temp_type b)
{
    return a.t > b.t;
}

void assignTable(string input)
{
    int assign[71][100];
    bool valid = true;
    int members[M], seats[N]; // NO. OF MEMBERS, SEAT CAPACITY OF EACH TABLE
    
    for(int i = 0; i < M + N; i++)
    {
        int num; 
        
        if(i < M)
        {
            stringstream ss;  
            ss << input[i];  
            ss >> num;  
            members[i] = num;
        }   
        if(i < M + N && i >= M)
        {
            stringstream ss;  
            ss << input[i];  
            ss >> num;  
            seats[i] = num;
        }
    }
    
    
    for(int i = 0; i < M && valid; i++)
    {
        if(members[i] > N)
        {
            valid = false;
            break;
        }
        
        for(int j = 0; j < N; j++)
        {
            temp[j].t = seats[j];
            temp[j].index = j;
        }
        
        sort(temp, temp + N, cmp);
        
        int ans = 0;
        
        for(int j = 0; j < members[i]; j++)
        {

            if(temp[j].t <= 0)
            {
                valid = false;
                break;
            }
            else
            {
                temp[j].t--;
                assign[i][ans++] = temp[j].index;
            }
        }   
        sort(assign[i], assign[i] + ans);
    }
    
    if(valid)
    {
        cout << "1" << endl;
        
        for(int i = 0; i < M; i++)
        {
            for(int j = 0; j < members[i]; j++)
            {
                cout << assign[i][j] + 1;
            }
            cout << endl;
        }
    }
    else
    {
        cout << "0" << endl;
    }
}

Just to note, we are only allowed to use this template to call the function assignedTable(string input), so we need to have strings as input then parse those into integer array and we are only allowed to get input from main and we are not allowed to code everything inside main.

  • 1
    `seats` is a ***local*** variable inside the `assignTable` function. The life-time of `seats` will end as soon as the function returns, leaving any pointers to `seats` or elements in the array invalid. Using the invalid pointer later leads to *undefined behavior*. – Some programmer dude May 19 '21 at 13:14
  • You might want to [get some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) about C++ to read and learn, and maybe even take a few classes. – Some programmer dude May 19 '21 at 13:15
  • did you use a debugger? Your way of reading input looks suspicious. Not clear why you dont read numbers from the start when you need numbers. Your concatenation `input = input + line;` will turn eg `123\n456` into `123456` – 463035818_is_not_a_number May 19 '21 at 13:15
  • Turn on your compiler's warnings. Fix those warnings. – Eljay May 19 '21 at 13:15
  • @largest_prime_is_463035818 hello! I added a note. – Nicole Cheng May 19 '21 at 13:34
  • @Someprogrammerdude hello! I added a note! – Nicole Cheng May 19 '21 at 13:34
  • ok you cannot change signature, but then it is still odd that you read individual lines just to immediately concatenate them again. Just read all the file into one string – 463035818_is_not_a_number May 19 '21 at 13:41
  • 1
    Does this answer your question? https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope?rq=1. Its what the first comment said. – 463035818_is_not_a_number May 19 '21 at 13:51
  • Why is `temp_type::t` a pointer to begin with? There's no need for it to be a pointer. – Some programmer dude May 19 '21 at 14:04
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight May 19 '21 at 14:07
  • @Someprogrammerdude I just changed it because at first I was planning to repeatedly ask for the user input then display the result then ask for user input again but it got complicated for me, I'll update my post in a sec. Thank you! – Nicole Cheng May 19 '21 at 14:10
  • 1
    Your question still talks about `cout << *(temp[j].t)` even though that won't even compile now that the type of `t` is `int`. – j_random_hacker May 19 '21 at 14:28

0 Answers0