Midweek blogging! Madness.
I took today as annual leave to get my fibre to premises line fitted, I now get a whopping 1Gbps down and up, sadly it doesn’t make me any better at games though. I decided the best use of the time would be to carry on learning, and I had some interesting results.
I started off with a quick dive into blender to practice basic topology and managed to create the below in < 30 minutes which made me over the moon!


After this I set about creating some basic pixel art textures, attempting to create a “Winchester” style pinned cushion sofa, the results of this took longer (an hour) and were unsatisfactory; I learned a lot about UV mapping though!

Having a few hours left of the day, I decided to jump into week 2 task 2 for CS50. I felt pretty confident the task would be much easier and I’d get it done much faster, and realistically it took 3 hours which is faster! A good two hours however were both deducting why my formula didn’t work and tangling with structs to make a scalable program that could grade several different texts.

The final result is below, and I’m pretty happy with it! The issue with my formula was that I’d recorded all variables in integer, however the formula provided utilizes decimal values and so I had to implement a conversion to double. Before being implemented I was getting all kinds of bizarre results like -15 and 300 grades.

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define lowest_grade 1
#define highest_grade 15
/* Pseudocode
typedef struct
{
text -retain user input
letter_count -count letters in string (a-z, A-Z)
sentence count -count full stops in string
grade -retain grade for text
} text;
int input_length = 0;
get_int Input ("How many texts would you like to process?: ");
For (int i = 0; i < how many; i++)
Get User String Input ("Enter text %i: ")
input_length = strlen(Input);
Init struct ("Text_%i", i);
get_letter_count(inputstring);
get_sentence_count(inputstring);
Calculate grade
index = 0.0588 * text_%i.Letters - 0.296 * text_%i.Sentences - 15.8
*/
// Structs
typedef struct
{
string input;
int length;
int letter_count;
int sentence_count;
int word_count;
int grade;
} text;
// Prototypes
text get_input(void);
int get_letter_count(string input, int input_length);
int get_word_count(string input, int input_length);
int get_sentence_count(string input, int input_length);
int get_grade(int letter_count, int sentence_count, int word_count);
int main(void)
{
int input_count = get_int("How many texts would you like to grade today?: ");
text texts[input_count];
for (int i = 0; i < input_count; i++)
{
texts[i] = get_input();
texts[i].length = strlen(texts[i].input);
texts[i].letter_count = get_letter_count(texts[i].input, texts[i].length);
texts[i].sentence_count = get_sentence_count(texts[i].input, texts[i].length);
texts[i].word_count = get_word_count(texts[i].input, texts[i].length);
texts[i].grade =
get_grade(texts[i].letter_count, texts[i].sentence_count, texts[i].word_count);
printf("Text %i\nLetter count: %i\nSentence count: %i\nWord count: %i\n", i + 1,
texts[i].letter_count, texts[i].sentence_count, texts[i].word_count);
if (texts[i].grade < lowest_grade)
{
printf("US Reading Grade 1\n");
}
if (texts[i].grade > highest_grade)
{
printf("US Reading Grade 16+\n");
}
if (texts[i].grade < 15 && texts[i].grade > 1)
{
printf("US Reading Grade %i\n", texts[i].grade);
}
}
}
text get_input(void) // Get user input, return to text struct
{
text t;
t.input = get_string("Enter Text: ");
return t;
}
int get_letter_count(string input, int input_length) // Increment letter var for alpha characters
{
int l = 0;
for (int i = 0; i < input_length; i++)
{
if (isalpha(input[i]))
{
l++;
}
}
return l;
}
int get_word_count(string input, int input_length) // Increment word var for whitespace chars
{
int w = 0;
for (int i = 0; i < input_length; i++)
{
if (input[i] == ' ')
{
w++;
}
}
w = w + 1; // Count the end word.
return w;
}
int get_sentence_count(string input, int input_length) // Increment sentence var for punctuation
{
int s = 0;
for (int i = 0; i < input_length; i++)
{
if (input[i] == '.' || input[i] == '!' || input[i] == '?')
{
s++;
}
}
return s;
}
int get_grade(int letter_count, int sentence_count,
int word_count) // Calculate grades based on the Coleman-Liau index, due to decimals
// all ints used need casting to float.
{
double L = ((double) letter_count / word_count) * 100;
double S = ((double) sentence_count / word_count) * 100;
double grade = 0.0588 * L - 0.296 * S - 15.8;
return (int) (grade + 0.5); // Round up
}
Leave a comment