Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include  <bits/stdc++.h>
#include <vector>
using namespace std; 

#define N 11
struct Activity 
{
  int start, finish; 
}; 	
bool Sort_activity(Activity s1, Activity s2) 
{ 
return (s1.start< s2.start);

} 


 
void print_Max_Activities(Activity arr[], int n) 
{ 
	sort(arr, arr+n, Sort_activity); 

	cout<< "Following activities are selected \n"; 
    int i = 0; 
	cout<< "(" <<arr[i].start<< ", " <<arr[i].finish << ")\n";
 for (int j = 1; j < n; j++) 
    { 
	   if (arr[j].start>= arr[i].finish) 
      	{	 
cout<< "(" <<arr[j].start<< ", "<<arr[j].finish << ") \n"; 
			i = j;
			 
			
	}   } 
} int main() 

   struct Activities =
    {
        {1, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 8}, {5, 9},
        {6, 10}, {8, 11}, {8, 12}, {2, 13}, {12, 14}
    };
{ 
    Activity arr[N];
   
	for(int i=0; i<=N-1; i++)
	{
cout<<"Enter the start and end time of "<<i+1<<" activity \n";
		cin>>arr[i].start>>arr[i].finish;
}
print_Max_Activities(arr, N); 
 return 0; 
}


What I have tried:

I've tried just declaring struct, but I still just get the same error.
Posted
Updated 27-Jan-22 5:18am
v2

You have compilation errors, which i have fixed, however the code is ambiguous.
C++
#include  <bits/stdc++.h>
#include <vector>
using namespace std; 

#define N 11
struct Activity 
{
  int start, finish; 
}; 	

bool Sort_activity(Activity s1, Activity s2) 
{ 
return (s1.start< s2.start);

} 


void print_Max_Activities(Activity arr[], int n) 
{ 
	sort(arr, arr+n, Sort_activity); 

	cout<< "Following activities are selected \n"; 
    int i = 0; 
	cout<< "(" <<arr[i].start<< ", " <<arr[i].finish << ")\n";
    for (int j = 1; j < n; j++) 
    { 
	   if (arr[j].start>= arr[i].finish) 
       {	 
            cout<< "(" <<arr[j].start<< ", "<<arr[j].finish << ") \n"; 
			i = j;
	   }   
    } 
} 

int main() 
{
   Activity Activities[11] =
    {
        {1, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 8}, {5, 9},
        {6, 10}, {8, 11}, {8, 12}, {2, 13}, {12, 14}
    };

    Activity arr[N];
   
	for(int i=0; i<=N-1; i++)
	{
        cout<<"Enter the start and end time of "<<i+1<<" activity \n";
		cin>>arr[i].start>>arr[i].finish;
    }
    print_Max_Activities(arr, N); 
    return 0; 
}
 
Share this answer
 
It is difficult to figure out what you are trying to do.
I'm showing you a piece of working code (using a vector which header you included but not used)
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Activity
{
  int start, finish;
};


ostream  & operator << ( ostream & os, const Activity & a)
{
  os << "start = " << a.start << ", finish = " << a.finish;
  return os;
}

bool compare_activity(Activity s1, Activity s2)
{
  return (s1.start < s2.start);
}

void print_Max_Activities(vector<Activity> & va)
{
  sort(va.begin(), va.end(), compare_activity);

  cout << "ordered activities\n";

  for (auto & a : va )
  {
    cout << a << "\n";
  }
}
int main()
{
   vector <Activity> va {
        {1, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 8}, {5, 9},
        {6, 10}, {8, 11}, {8, 12}, {2, 13}, {12, 14}
    };

  print_Max_Activities(va);
}
 
Share this answer
 
Look at your code:
} int main() 

   struct Activities =
    {
        {1, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 8}, {5, 9},
        {6, 10}, {8, 11}, {8, 12}, {2, 13}, {12, 14}
    };
{ 
    Activity arr[N];
You can't declare anything between the function signature for main and the function body!
Move this:
int main() 
below the definition:
}

   struct Activities =
    {
        {1, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 8}, {5, 9},
        {6, 10}, {8, 11}, {8, 12}, {2, 13}, {12, 14}
    };
int main() 
{ 
    Activity arr[N];
That will get rid of the problem, but will cause another error since neither the compiler or I have any idea what the struct Activities = ... is supposed to be ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900