Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions exercises/ex1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
#include <iostream>
using namespace std;

void printDay(int week){
switch(week){
case 1:
cout<<"Monday"<<endl;
break;
case 2:
cout<<"Tuesday"<<endl;
break;
case 3:
cout<<"Wednesday"<<endl;
break;
case 4:
cout<<"Thursday"<<endl;
break;
case 5:
cout<<"Friday"<<endl;
break;
case 6:
cout<<"Saturday"<<endl;
break;
case 7:
cout<<"Sunday"<<endl;
break;
default:
cout<<"Error: Inser a number between 1-7"<<endl;
break;
}
}

int main()
{
int week;
Expand All @@ -11,38 +40,7 @@ int main()
cout << "Enter week number(1-7): " << endl;
cin >> week;

if (week == 1)
{
cout << "Monday" << endl;
}
else if (week == 2)
{
cout << "Tuesday" << endl;
}
else if (week == 3)
{
cout << "Wednesday" << endl;
}
else if (week == 4)
{
cout << "Thursday" << endl;
}
else if (week == 5)
{
cout << "Friday" << endl;
}
else if (week == 6)
{
cout << "Saturday" << endl;
}
else if (week == 7)
{
cout << "Sunday" << endl;
}
else
{
cout << "Invalid input! Please enter week number between 1-7." << endl;
}
printDay(week);

return 0;
}
}
42 changes: 8 additions & 34 deletions exercises/ex2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,19 @@
#include <iostream>
using namespace std;


int main()
{
int week;

string arr[7]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
cout << "Enter week number(1-7): " << endl;
cin >> week;

if (week == 1)
{
cout << "Monday" << endl;
}
else if (week == 2)
{
cout << "Tuesday" << endl;
}
else if (week == 3)
{
cout << "Wednesday" << endl;
}
else if (week == 4)
{
cout << "Thursday" << endl;
}
else if (week == 5)
{
cout << "Friday" << endl;
}
else if (week == 6)
{
cout << "Saturday" << endl;
}
else if (week == 7)
{
cout << "Sunday" << endl;
}
else
{
cout << "Invalid input! Please enter week number between 1-7." << endl;
if(week<1){
cout << "Error: Insert number between 1-7" <<endl;
return -1;
}
cout << arr[week-1] <<endl;


return 0;
}
}
48 changes: 19 additions & 29 deletions exercises/ex3.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
/* Could you still use a switch case here? May you can use a map. */

#include <iostream>
#include <unordered_map>
using namespace std;

void initFamousPeople(unordered_map<string,string>& famousPeople){
famousPeople["BarackObama"]="44th president of the United States";
famousPeople["SandroPertini"]="Former President of the Italian Republic";
famousPeople["NelsonMandela"]="Former President of South Africa";
famousPeople["MahatmaGandhi"]="Bapu";
famousPeople["DonaldKnuth"]="Creator of LaTeX";
famousPeople["DennisRitchie"]="Creator of C";
}

template<typename T, typename U> bool contains(unordered_map<T,U>& famousPeople, T key){
return (famousPeople.count(key)==0 ? false : true);
}

int main()
{
string textInput;
unordered_map<string,string> famousPeople;
initFamousPeople(famousPeople);

cout << "Enter a famous name+surname, ex. BarackObama " << endl;
cin >> textInput;

if (textInput == "BarackObama")
{
cout << "44th president of the United States" << endl;
}
else if (textInput == "SandroPertini")
{
cout << "Former President of the Italian Republic" << endl;
}
else if (textInput == "NelsonMandela")
{
cout << "Former President of South Africa" << endl;
}
else if (textInput == "MahatmaGandhi")
{
cout << "Bapu" << endl;
}
else if (textInput == "DonaldKnuth")
{
cout << "Creator of LaTeX" << endl;
}
else if (textInput == "DennisRitchie")
{
cout << "Creator of C" << endl;
}
else
{
cout << "Invalid input! Please enter a good name!" << endl;
}
if(contains(famousPeople, textInput))
cout<<famousPeople[textInput]<<endl;

return 0;
}
}