Scheduling algorithms in C++Algorithm scheduling is the process of determining which algorithm or line of code will be executed in the Central Processing Unit (CPU) and which one will be put on hold awaiting processing. In C + + programming, scheduling ensures that there is always an algorithm avail...
Two-dimensional arrays
import java.util.Scanner;
public class Assignment {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
// We have 4 days, and 5 classes per day
// Fill the lecture rooms
String[][] Woodward106 = new String[4][5];
String[][] Cameron171 = new String[4][5];
// CSC110 is in mondays and wednesdays, at the first class of day
Woodward106[0][0] = "CSC1100";
Woodward106[0][1] = "CSC1213";
Woodward106[0][2] = "CSC2175";
Woodward106[0][3] = "ENGL2116";
Woodward106[0][4] = "CSC3155";
Woodward106[1][0] = "CSC2200";
Woodward106[1][1] = "ITCS3688";
Woodward106[1][2] = "CSC3146";
Woodward106[1][3] = "MATH1241";
Woodward106[1][4] = "CSC3181";
Woodward106[2][0] = "CSC1100";
Woodward106[2][1] = "CSC1213";
Woodward106[2][2] = "CSC2175";
Woodward106[2][3] = "ENGL2116";
Woodward106[2][4] = "CSC3155";
Woodward106[3][0] = "CSC1100";
Woodward106[3][1] = "CSC1213";
Woodward106[3][2] = "CSC2175";
Woodward106[3][3] = "ENGL2116";
Woodward106[3][4] = "CSC3155";
Woodward106[4][0] = "CSC2200";
Woodward106[4][1] = "CSC3688";
Woodward106[4][2] = "ITSC3146";
Woodward106[4][3] = "MATH1241";
Woodward106[4][4] = "CSC3181";
Cameron171[0][0] = "ITIS3130";
Cameron171[0][1] = "ITCS3134";
Cameron171[0][2] = "ITIS3200";
Cameron171[0][3] = "ITCS3153";
Cameron171[0][4] = "ITIS3216";
Cameron171[1][0] = "ITCS2600";
Cameron171[1][1] = "ITIS3135";
Cameron171[1][2] = "ITCS3160";
Cameron171[1][3] = "ITIS3320";
Cameron171[1][4] = "ITCS3166";
Cameron171[2][0] = "ITIS3130";
Cameron171[2][1] = "ITCS3134";
Cameron171[2][2] = "ITIS3200";
Cameron171[2][3] = "ITCS3153";
Cameron171[2][4] = "ITIS3216";
Cameron171[3][0] = "ITCS2600";
Cameron171[3][1] = "ITIS3135";
Cameron171[3][2] = "ITCS3160";
Cameron171[3][3] = "ITIS3320";
Cameron171[3][4] = "ITCS3166";
// Now, ask student for courses
int stop = 0;
int N = 5; // number of courses to be entered
while(stop == 0)
{
String courses[] = new String[N];
for(int i = 0; i< N; i++)
{
System.out.print("Enter course No. " + (i+1) + ": ");
courses[i] = sc.next();
}
// Now, check if the courses are valid
booleanvalid_schedule = true;
for(int i = 0; i< N; i++)
{
boolean valid = false;
// Iterate through days
for(int day = 0; day < 4; day++ )
{
// Iterate through classes
for(int cl = 0; cl < 5; cl++)
{
if(Woodward106[day][cl].toLowerCase().compareTo(courses[i].toLowerCase()) == 0 || Cameron171[day][cl].toLowerCase().compareTo(courses[i].toLowerCase()) == 0) // Course is valid
{
valid = true;
break;
}
if(valid)
break;
}
}
if(!valid) // course not valid
{
System.out.println("Course " + courses[i] + " is not offered in the lecture rooms.");
valid_schedule = false;
}
}
if(!valid_schedule)
{
System.out.print("Some of the courses you entered are not valid. Do you want to enter the courses again? (y/n): ");
String input = sc.next();
if(input == "n")
stop = 1;
}
// At this point, we know that the courses are valid. Now, let's check if there is conflict with hours
for(int i = 0; i< N; i++)
{
for(int j = 0; j < N; j++)
{
if(i != j)
{
String coursei = courses[i];
String coursej = courses[j];
// Check that both courses are not present at the same time
for(int day = 0; day < 4; day++ )
{
// Iterate through classes
for(int cl = 0; cl < 5; cl++)
{
if(Woodward106[day][cl].toLowerCase().compareTo(coursei.toLowerCase()) == 0 || Cameron171[day][cl].toLowerCase().compareTo(coursei.toLowerCase()) == 0) // Course is valid
{
if(Woodward106[day][cl].toLowerCase().compareTo(coursej.toLowerCase()) == 0 || Cameron171[day][cl].toLowerCase().compareTo(coursej.toLowerCase()) == 0) // Course is valid
{
if(coursei.toLowerCase().compareTo(coursej.toLowerCase()) != 0)
{
valid_schedule = false;
System.out.println("Courses " + coursei + " and " + coursej + " are time conflict.");
break;
}
}
}
}
}
}
}
}
if(!valid_schedule)
{
System.out.print("Some of the courses you entered are not valid. Do you want to enter the courses again? (y/n): ");
String input = sc.next();
if(input == "n")
stop = 1;
}
else
{
// The schedule is valid
System.out.println("Congratulations! Your schedule has been settled!");
stop = 1;
}
}
}
}
Arrays and pointers in C++
#include <iostream>
#include <stdio.h>
using namespace std;
class game_record
{
public:
string game_name;
int times_played;
int highest_score;
};
int main(int argc, char **argv)
{
int gameCount;
cout<<"Enter total no. of games:";
cin>>gameCount;
while(gameCount<=0)
{
cout<<"No. of games cannot be zero or negative. Enter new value";
cin>>gameCount;
}
game_record *all_games= new game_record[gameCount];
int currentGame = -1;
bool isRunning = true;
int *each_score;
while (isRunning)
{
int yourOption;
cout<< "Please select a choice:" <<endl; cout<< " 1. enter new game" <<endl;
2. update times played or high score" 3. see all game stats" 4.
quit" "you choice: " ; cin>>yourOption;
switch (yourOption)
{
case 1:
if ((currentGame+1) == gameCount)
{
cout<< "You reach the limit of game. Game is reset!";
currentGame = 0;
cout<<"Enter game name for "<<currentgame+1<<" game:"; fflush(stdin); getline(cin,
all_games[currentgame].game_name); cout<<"enter no. of times you want to
play for game"<<currentgame+1<<":"; cin>>all_games[currentGame].times_played;
each_score=new int(all_games[currentGame].times_played);
cout<<"Enter score for each game played"<<endl; for(int
i="0;i<all_games[currentGame].times_played;i++)" cin>>each_score[i];
all_games[currentGame].highest_score=-1;
}
else
{
currentGame++;
cout<<"Enter game name for "<<currentgame+1<<" game:"; fflush(stdin);
getline(cin,all_games[currentgame].game_name); cout<<"enter no. of times
you want to play for game"<<currentgame+1<<":"; cin>
>all_games[currentGame].times_played;
each_score=new int(all_games[currentGame].times_played);
cout<<"Enter score for each game played"<<endl; for(int
i="0;i<all_games[currentGame].times_played;i++)" cin>>each_score[i];
all_games[currentGame].highest_score=-1;
}
break;
case 2:
if(currentGame == -1)
{
cout<<"Please select a new game first"<<endl; break; } if
(all_games[currentgame].times_played> 0)
{
for (int i = 0; i<all_games[currentgame].times_played; i++) { if
(each_score[i]>all_games[currentGame].highest_score)
all_games[currentGame].highest_score = each_score[i];
}
}
else
{
cout<< "Game "<<currentgame+1<<" was not played even
once. please enter no. of times you want to
play:"; cin>>all_games[currentGame].times_played;
}
break;
case 3: if(currentGame == -1) { cout<<"Please select a new game first"<<endl; break; }
if (all_games[currentgame].times_played> 0)
{
cout<< "The stats for game "<<currentgame+1<<": " <<endl;
for (int i=" 0;" i<all_games[currentgame].times_played; i++) { cout<<i + 1 << play have score: <<each_score[i] }
cout<< "highest <<all_games[currentgame].highest_score<<endl; "
current game: currentgame+1 else "game " <<currentgame+1<<" was not played even once.";
break;
}
case 4: isrunning="false;" "bye bye!" {
default: "invalid choice. please try again."
return 0;
}