Final Project: Sudoku

#TC1017

I choose this project because when I was still in high school I saw some dude programming this and thought it was very cool and just as a personal challenge I tried to figure out on my own if I could make such a programm. Turns out I can!

While there are still some flaws or things I would still change, such as have colored output and make some functions smarter, it still is completely playable and you can download any sudoku text file and try to solve it using my code.

I learned a lot while making this game, specially how easy vectors are to use, as opposite to arrays, which just passing as paramaters is a nightmare. Also reading and opening files was another skill I developed while programming this.

The hardest bit was coming up with a function that would check for repetitions inside a matrix, and I think just doing that was maybe like 6o% of the whole work.

Many ideas and insipration came from this guy’s code online:

Sudoku Program in C++

only I decided to do it my way using something simpler but not as fancy. His code just gave me an idea of the kinds of functions I had to implement, how to implement them was completely up to me.

I have to say I feel satisfied with this project since it was a great way to summarize most of the content of the course.

 

Screenshot:

Captura

GitHub link with code and txt file: https://github.com/diegodamy/Sudoku


WSQ14: SciLab

#Tc1017 #SciLab

SciLab is a numerical computation software which is quite handy and also easier to understand than C++ in my opinion, its graphic options are very useful as well, and the handling of matices is definitely more intuitive than in C++.

Understanding SciLab:

  1. On the console screen, we can use the arrows to change instructions and find the history of used commands, kinda like how we use the arrow keys in the compiler.
  2. When using the console we have to input first –> before any command
  3. Basic operations and inserting comments remain the same as in C++.
  4. We can use %e, %pi and %i to represent euler’s constant, pie and complex numbers.
  5. The semicolon is used to avoid any result from beign displayed, no compiling error will occur here.
  6. More useful than the console screen is the editor, which is basically a word processor in which coding becomes easier.

Plotting in SciLab:

  1. Command: plot.
  2. clf menas clear plot, while scf opens another window.
  3. Colors can be added to curves: letter enclosed by “” –> “b” makes blue
  4. Point styles work the same
  5. Segments go in square brackets, point go in round ones.
  6. Statistics: useful for creating bar graphs
  7. Use bar command –> bar (x,n,color)

Creating functions:

  1. Begin: function
  2. End: endfunction
  3. Variables must always have a value, but we don´t have to declare them in advanced😀
  4. Function’s parameters work the same as in C++
  5. Logical operators remain the same, only != becomes <>

Matrices and vectors:

  1. Al operations are done with matrices.
  2. A space or coma separates columns.
  3. Semicolons separate rows.

Loops:

  1. Beggining:step: value –> this way is much simpler than C++
  2. For and while finish with an end.

Course Review

#TC101 #CourseReview

Programming in C++: Course Review

As we steadily approach the end of the semester, we can now look back on the experiences gained throughout this course and reflect about them.

Since I´m taking this course for the second time now, the experience miaght have been a little different for me than for everyone else, since I was already familiarized with all of the content, yet I didn´t have a perfect understanding of quite a few things.

Before I get into the course topics themselves, I’d like to talk about this new teaching method, the so called “Flipped-Classroom”. For me it was a great idea, it meant I could do my programming whenever I wanted and use the time in class to work on other projects or homework, which I really appreciated since. This had a bad side effect towards the end of the second parcial but I´ll talk about that later.

The fact that we had to upload our Blogs was also a good idea since it forced us to explain ourselves what our programms did, and sometimes that led to us finding mistakes within our codes or a better way to do something. And by making everyone´s code available, it was easy to get past beign stuck by looking at other classmate´s codes.

Now about the bad side effects I mentioned earlier. Since I didn´t go to class all that much, I didn´t get to know many of my classmates, so that really took a hit on the whole social aspect for me. Part of me just wanted to be alone to prove myself I can get this course done by myself, kind of the reason I did the final Project alone, and the less-tech savvy part of myself just wasn´t used to the whole blogging-twitting thing.

Continue reading “Course Review”

QUIZ 7: Dot product

#TC1017 #QUIZ 7

The last of the quizzes. I wanted to import the lists from text files just to practice doing that, but I had a problem with the unknown sizes of the lists, turns out I only managed to get it working properly if I specify before hand how big each list is. Other wise it was a simple quiz for me since I was also working on my sudoku.

Captura.PNG

Source Code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int CheckIfFirstOpen(string list1){
ifstream ListOne;
ListOne.open(list1.c_str());

if (ListOne.is_open()){
return 1;
} else {
return 0;
}
}

int CheckIfSecondOpen(string list2){
ifstream ListTwo;
ListTwo.open(list2.c_str());

if (ListTwo.is_open()){
return 1;
} else {
return 0;
}
}

vector<int>ReadList1(string list1){
vector<int>List1(4);
ifstream ListOne;
ListOne.open(list1.c_str());

for (int i = 0; i < List1.size(); i++) {
ListOne >> List1[i];
}
return List1;
ListOne.close();
}
vector<int>ReadList2(string list2){
vector<int>List2(4);
ifstream ListTwo;
ListTwo.open(list2.c_str());

for (int i = 0; i < List2.size(); i++){
ListTwo >> List2[i];
}
return List2;
ListTwo.close();
}

int DotProduct (vector<int>List1, vector<int>List2){
int size = 4;
int sum;

for (int i = 0; i < size; i++){
sum += List1[i] * List2[i];
}
cout << “Dot product is ” << sum;
}

int main(){
string list1;
string list2;

cout << “Please enter the name of the first list:” << endl;
cin >> list1;
cout << “Please enter the name of the second list:” << endl;
cin >> list2;

while (CheckIfFirstOpen(list1) == 0) {
cout << “Unable to open first file.” << endl;
cout <<“Please enter the name of the first text:” << endl;
cin >> list1;
}
cout << endl;

while (CheckIfSecondOpen(list2) == 0) {
cout << “Unable to open second file.” << endl;
cout <<“Please enter

Continue reading “QUIZ 7: Dot product”

WSQ13: Exam corrections

#TC1017 #WSQ13

Q1:

For this first question we had to print out a kind of rotated pyramid, which I must say I found somewhat complicated because of the nested ifs. All important notes can be found in the source code:

Captura

 


 

Q2:

The superpower one. We had already done this as a quiz, but I didn´t realize my old code wasn´t really working so it took me a while to realize what I was doing wrong. It turned out I had to multiply the result by the base, and I was doing some weird additions. But now it works perfectely.

Captura.PNG


 

Q3:

The fibonacci series was also already done before, it is quite simple.

Captura.PNG


Q4:

This last problem I could not complete, but here is my source code:

#include <iostream>
#include <string>

using namespace std;

int FindPali(string word){
int i, j;
int size = word.size();

/*This cycle should compare all indices to their opposites to chech for symmetry and decide wheter it is a palindrome or not, however it is not doing anything it seems.*/
for (i=0; i<size; i++){
} cout << word[i];
for (j=size; j>0; j–){
} cout << word[j];
if (word[i] == word[j]){
return 1;
}

}

int main(){
string word;
cout << “Please enter the word:” << endl;
getline (cin, word);

if (FindPali(word)==1){
cout << “Palindrome!” << endl;
} else {
cout << “Not a palindrome.”<< endl;
}

}

GitHub link including all questions: https://github.com/diegodamy/Exam2-Q1

Reading from file

#TC1017 #WSQ12

This was quite a cool and rather long problem. I feel quite happy about it, only case sensitivity is still an issue. But other than that it is quite comprehensive, I mean, it is user friendly and allows him to correct any input mistakes by checking whether the desired file was found or not.

To accomplish this many special functions were needed, such as .open, ifstream, .is_open, and.eof. But when I understood how these worked, the rest of the code was very intuitive to finsih.

wsq12.PNG

Source Code below: [GitHub Link: https://github.com/diegodamy/WSQ12 %5D

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int CheckFile(string textname3){
ifstream InList;
InList.open(textname3.c_str());

if (InList.is_open()) {
cout << “File was opened succesfully!” << endl;
return 1;
} else {
return 0;
}
}

int ReadFile(string word2, string textname2){
int counter;
string textword;
ifstream InList;
InList.open(textname2.c_str());

while (!InList.eof()) { // keep counting until you rach the last string
InList >> textword; // create a string out of all words in the text file
if (textword == word2) {
counter++;
}
}

InList.close();
return counter;
}
int KeepRunning(char decision){
if (decision == ‘Y’ || decision == ‘y’){
return 1;
} else {
return 0;
}
}

int main(){
string word;
string textname;
char decision;

do {
cout <<“Please enter the name of the text:” << endl;
cin >> textname;

if (CheckFile(textname)==0) {
cout << “Unable to open file.” << endl;
cout <<“Please enter the name of the text:” << endl;
cin >> textname;
}

cout <<“Please enter the word you wish to find in the text:” << endl;
cin >> word;

cout << endl << word << ” was found ” << ReadFile(word, textname) << ” time(s) in the text.” << endl;
cout <<

Continue reading “Reading from file”

GCD: What were you?

#TC1017 #QUIZ6

Soooo I had no idea what a GDC was, well I had forgotten what it was. Lucky for me Euclide’s formula was quite easy to understand anfd implement, so I chose to do it with arrays, just cause.

It was simple and effective, but I did struggle with a compiling error that was due to a bad implementation of some logic. (When either number is 0). I just had to rearrange some lines of code and it ran like a charm.

quiz6

Source Code below: [GitHub Link:  https://github.com/diegodamy/Quiz6]

#include <iostream>
using namespace std;

int GetGDC(int numbers[], int size){
int quotient;
int modulus;

do {

if (numbers[0] == 0){
return 1; //cout << “GDC is ” << numbers [1] << endl;
} else if (numbers[1] == 0){
return 0; //cout << “GDC is ” << numbers [0] << endl;
}

quotient = numbers[0]/numbers[1];
modulus = numbers[0]%numbers[1];

numbers [0] = (numbers [1]*quotient)+modulus;

numbers [0] = numbers [1];
numbers [1] = modulus;

} while ((numbers[0] == 0)||(numbers[1] == 0));

}

int main(){
int array [2];

cout << “In order to find the GCD of two numbers, please input two positive integers:” << endl;
for (int i=0; i<2; i++) {
cin >> array[i];
}

if (GetGDC(array,2)==1) {
cout << “The GDC of given numbers is ” << array[1] << endl;
} else if (GetGDC(array,2)==0) {
cout << “The GDC of given numbers is ” << array[0] << endl;
} else {
cout << “The GDC of the given numbers is ” << GetGDC(array,2) << endl;
}
}

———————————————-

Palindroming all day long

#TC1017 #QUIZ5

So this was hard. This, I must say, is one of my least effective codes written so far, it´s too long, buuuuuuut it works, so I´ll take it. Basically the user inputs the word, letter by letter (ineffective!) and then my code displays it the normal way and then reversed, just so the user sees it. Then it compares index by index to check if its the same.

The function .pushback was helpful for creating vectors of unknown size.

The real hard part was making it case unsensitive, oh man that took a while. Ken helped me out with a magical function and everything ran just fine. Also, mek sure to #include all necessary modules.

QUIZ5.PNG

Source Code below:[GitHub Link: https://github.com/diegodamy/Quiz5%5D

#include <iostream>
#include <vector>
#include <stdio.h>
#include <ctype.h>
using namespace std;

char DisplayWord (vector<char> word){ //Outputs word in order
int size = word.size();
vector<char>word1;

cout << “Your word: “;
for (int i = 0; i<word.size(); i++){
cout << word[i];
}
}

vector<char> DisplayWordReversed (vector<char> word){ //Outputs word in inverse order
int size = word.size();
vector<char>word2;
cout << endl << “Reversed: “;

for (int i = word.size()-1; i >=0; i–){
cout << word[i];
word2.push_back(word[i]);
}
return word2; //Returns value od reversed word (vector)
}

char FindPalindrome (vector<char> word, vector<char> word2){ //Compares indices of tow vectors: normal word and inversed word
int size = word.size();
cout << endl;
for (int i = 0; i<word.size(); i++){
if (word[i] == word2[i]){
return 1; //If elements of indices match, return true for palindrome
} else{
return 0;
}
}
}

int ConvertLowerUpper (){
int i=0;
char str[]=”Test String.n”;
char c;
while (str[i])
{
c=str[i];
putchar (tolower(c));
i++;
}
return 0;
}
int main(){
char letter;

cout << “Please enter letter by

Continue reading “Palindroming all day long”

Euler Aprox.

#TC1017 #QUIZ4

So this quiz was actually kinda challenging, it took me quite some time and a lot of questions to Ken to finally work it out. All noteworthy notes can be found on the picture below.

The factorial function was taken from one of the WSQs we did before, the tricky part was that C++ wasn´t giving me all decimals beacause of a disparity with the function’s types. Ken sorted that out😀

quiz4.PNG

Source Code below [GitHub Link: https://github.com/diegodamy/Quiz4 ]

#include <iostream>
using namespace std;

int factorial (int n){
if (n == 0){
return 1;
} else {
return n*factorial(n-1); //This is an example of recursion, and must be implemented since the formula for the n factorial of any number
} // requires the n-1 factorial of given number
}

long double euler_calc (long double prec)
{
long double euler_sum = 0.0; //It’s better to initialize this variable since we shouldn’t asssume it will start at 0 by default
long double prev_sum; // Sometimes that might work but other times it could return junk memory
int n = 0;

do {

prev_sum = euler_sum;
euler_sum = euler_sum + 1.0/ (factorial(n)); // The “1.0” here is VERY important, otherwise it won´t compile correctly.
n = n+1; // A simple “1” won´t work because C++ will stupidly convert our long double variable into an int one, which we don´t want

} while (euler_sum – prev_sum > prec);

return euler_sum;
}

int main(){

long double precision;

cout << “Please input the value of precision:” << endl;
cin >> precision;

cout << “Result is: ” << euler_calc(precision);
}

—————————————————

Photo Credit: <a href=”https://www.flickr.com/photos/57329804@N00/9984539064/”>spanaut</a&gt; via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by-nc-sa/2.0/”>cc</a&gt;

Arrays, simple yet useful

#TC1017 #WSQ10

This programm was very easy, since the formulas are well known by now, we have used them a lot in other classes. And arrays are my favorite part of C++ so far. I think they are very intuitive to use, and make soving problems like these, with a big set of numbers, quite easy to solve.

wsq10.PNG

 

Source Code below: [GitHub Link: https://github.com/diegodamy/WSQ10 ]

#include <iostream>
#include <math.h>
using namespace std;

float STDV (int size2, float st){
double stdv;
return stdv = sqrt(st/(size2-1));

}

float ST(int list2[], int size2, float average){
double st;
for(int i = 0; i <size2; i++){
st += pow((list2[i] – average),2);
}
return st;
}

float GetAverage (int list[], int size){
float sum = 0.0;
float result = 0.0;

for ( int n = 0; n <size; n++){
sum += list[n];
}
return result = sum / size;
}

int main(){
int array [10];
double average;
double st;

cout << “Please enter ten numbers:” << endl;

for (int i = 0; i<10; i++){
cin >> array [i];
}

average = GetAverage (array, 10);
cout << “Average is ” << average;

st = ST(array,10,average);

cout << endl << “Standard deviation is: ” << STDV (10,st);
}

——————————–

Photo Credit: <a href=”https://www.flickr.com/photos/50318388@N00/23640476465/”>mag3737</a&gt; via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by-nc-sa/2.0/”>cc</a&gt;