the header file:

#include<stdio.h>
struct IntArray
{
 int schedule[30000];
 int nSize;
};
struct IntArray getSchedule(int browsingTime[], int noOfPersons, int timeSlot);

the implementation file:

#include"leastremainingtime.h"
void sort(int *a,int n)
{
  int i=0,j=0,temp=0;
  for(i=0; i<n;i++)
  {
    for(j=0;j<n-1;j++)
    {
              if(a[j] > a[j+1])
             {
                      temp=a[j];
	               a[j]=a[j+1];
 	              a[j+1]=temp;
            }
    }
  }
}
struct IntArray getSchedule(int browsingTime[], int noOfPersons, int timeSlot)
{
 struct IntArray soln;
 soln.nSize=0;
 int i=0,j=0,k=0,toBeSorted[500],sameArr[500],newarr[500],l=0,remUsers=0;
 for(i=0; i<noOfPersons; i++)
 {
    toBeSorted[i]=sameArr[i]=browsingTime[i];
 }
 sort(toBeSorted,noOfPersons);
 for(i=0; i<noOfPersons; i++)
 {
  for(j=0; j<noOfPersons ; j++)
  {
      if(toBeSorted[i]==sameArr[j])
      {
        newarr[k]=j+1; k++; 
        sameArr[j]=-1;
        break;
       }
   }
  }
  /*for(l=0; l<k; l++)
  printf("\nnew:%d",newarr[l]);
  /*for(i=0;i<noOfPersons;i++)
  printf("\nsort:%d\tactual:%d",toBeSorted[i],browsingTime[i]);*/
  //to implement th algo on the sorted array
  i=0,k=0;
  while(1)
  {
     if(remUsers == noOfPersons)  break;
     else if(toBeSorted[i] == -1)
        i=(i+1)%noOfPersons;
     else if(toBeSorted[i] <= timeSlot)
     {
          soln.schedule[k]=newarr[i];
          k++;
          soln.nSize++;
          toBeSorted[i]=-1;
          remUsers++;   // printf("\trem:%d",remUsers);
          i=(i+1)%noOfPersons; 
      }
     else if(toBeSorted[i] > timeSlot)
     {
          soln.schedule[k]=newarr[i];
          toBeSorted[i]=toBeSorted[i]-timeSlot;
          //printf("\tin >::%d",toBeSorted[i]);
          k++;  
 	  soln.nSize++; i=(i+1)%noOfPersons; 
     }
   
  }    
 return soln; 
}

// Following main function contains 3 representative test cases

int main()	
{
	//Testcase 1:
	{
		int i,browsingtime[] = {10,7,3,4,11};
		int size = 5;
		int time_slot = 5;
		struct IntArray res = getSchedule(browsingtime, size, time_slot);
		if(res.schedule != NULL)
		printf("\n");
		for(i=0;i<res.nSize;i++)
		printf ("%d, ",res.schedule[i]);
		printf("\n");
	}

	//Testcase 2:
	{
		int i,browsingtime[] = {3,7,14,4,11};
		int size = 5;
		int time_slot = 7;
		struct IntArray res = getSchedule(browsingtime, size, time_slot);
		if(res.schedule != NULL)
		printf("\n");
		for(i=0;i<res.nSize;i++)
		printf ("%d, ",res.schedule[i]);
		printf("\n");
	}

	//Testcase 3:
	{
		int i,browsingtime[] = {4,2,5};
		int size = 3;
		int time_slot = 1;
		printf("\n");
		struct IntArray res = getSchedule(browsingtime, size, time_slot);
		if(res.schedule != NULL)
		for(i=0;i<res.nSize;i++)
		printf ("%d, ",res.schedule[i]);
	}
}

Can anyone please tell me where the mistake is?I am getting a rumtime error.

Recommended Answers

All 2 Replies

Which run time error would that be?

Which run time error would that be?

I had done a mistake in declaration in the header file as
int *schedule
in the structure.I had used the wrong struct declaration in the actual header file.
Its working now! :)
Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.