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.