https://cs50.readthedocs.
io/style/c/#switches
https://cs50.harvard.edu/x/2023/notes/2/
https://www.tutorialspoint.com/c_standard_library/ctype_h.htm
https://manual.cs50.io/#ctype.h
Prompt the user for a string of text using get_string
Count the number of letters, words, and sentences in the text.
Print as output "Grade X" where X is the grade level computed by the Coleman-Liau formula
Variable letters=0 loop and check for letters not counting characters in array,increment variable
Variable words=0 loop and check for spaces and add 1 word for final number
Variable sentences=0 loop and count how many “.” , “!” , “?”
After u calculate nr of letters ,words ,sentences - use formula to calc grade level
index = 0.0588 * L - 0.296 * S - 15.8, L = average number of letters, S = average number of sentence and
round the number(round is declared in math.h)
index = 0.0588 * count_letters - 0.296 * count_sentences - 15.8
L=count letters+count words / 2
S=count letters +count words /2
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
string text = get_string("Text: ");
int count_letters = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) { // Check if the character is a letter
count_letters++;
// Now, 'letters' variable contains the count of letters in the string.
int count_words = 0;
bool in_word = false; // Flag to track if we are inside a word
for (int i = 0; text[i] != '\0'; i++) {
if (isblank(text[i])) {
// Check if the character is a whitespace character
in_word = false;
} else if (!in_word) {
// If we encounter a non-blank character and we were not in a word,
// it means we are starting a new word
in_word = true;
count_words++;
int count_sentences = 0;
for (int i = 0; text[i] != '\0'; i++) {
char current_char = text[i];
if (ispunct(current_char) && current_char != ',' && current_char != '-' && current_char != '\''
&& current_char != ' ' && current_char != ';') {
// Check for punctuation (excluding commas)
count_sentences++;
float x = 0.0588 * (count_letters / count_words * 100) - 0.296 * (count_sentences / count_words *
100) - 15.8;
printf(" Grade %i\n", (int)round(x));
return 0;
Mario
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Positive number: ");
}
while ((n < 1) || (n > 8));
{
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("#");
}
printf("\n");
}
}
oam
#include <stdio.h>
int main() {
int starting_population;
int target_population;
int years = 0;
do {
printf("Enter the starting population (minimum 9): ");
scanf("%d", &starting_population);
} while (starting_population < 9);
do {
printf("Enter the target population (greater than or equal to starting population): ");
scanf("%d", &target_population);
} while (target_population < starting_population);
while (starting_population < target_population) {
starting_population += starting_population / 3;
starting_population -= starting_population / 4;
years++;
printf("Years: %d\n", years);
return 0;
make population/population
./population
V2
#include <stdio.h>
int main(void)
int start_size, end_size;
// Prompt the user for the starting population size and validate it.
do
printf("Enter the starting population size (must be at least 9): ");
scanf("%d", &start_size);
} while (start_size < 9);
// Prompt the user for the ending population size and validate it.
do
printf("Enter the ending population size (must be greater than or equal to the starting size): ");
scanf("%d", &end_size);
} while (end_size < start_size);
int years = 0;
int current_population = start_size;
// Calculate the number of years required for the population to reach the end size.
while (current_population < end_size)
int births = current_population / 3;
int deaths = current_population / 4;
current_population = current_population + births - deaths;
years++;
// Print the result.
printf("Years: %d\n", years);
return 0;
Selction sort O(n2)
Bubble sort worst case O(n2) si best case Omega(n)
time ./sort1 random5000.txt
time ./sort1 random50000.txt
time ./sort1 random5000.txt
Sort 1 random 5000 10000 50000 average 0.909. Selection sort
real 0m0.068s 0m0.188s 0m5.415s
user 0m0.026s 0m0.146s 0m5.057s
sys 0m0.026s 0m0.031s 0m0.172s
time ./sort1 reversed5000.txt
Sort 1 reversed 5000 10000 50000 average 0.832.
real 0m0.061s 0m0.220s 0m4.756s
user 0m0.044s 0m0.176s 0m4.430s
sys 0m0.16s 0m0.032s 0m0.151s
time ./sort2 random5000.txt Merge sort
Sort 1 random 5000 10000 50000 average 0.126.
real 0m0.020s 0m0.43 0m0.383
user 0m0.04s 0m0.14 0m0.032
sys 0m0.15s 0m0.23s 0m0.162
time ./sort2 reversed5000.txt
Sort 2 reversed 5000 10000 50000 average 0.06.
real 0m0.021 0m0.037 0m0.297
user 0m0.004 0m0.004 0m0.024
sys 0m0.15s 0m0.029 0m0.149
time ./sort3 random5000.txt
Sort 3 random 5000 10000 50000 average 0.404.
real 0m0.43s 0m0.131 0m2.190
user 0m0.017s 0m0.096 0m1.812
sys 0m0.020 0m0.27s 0m0.211
time ./sort3 reversed5000.txt
Sort 3 reversed 5000 10000 50000 average 0.412.
real 0m0.050 0m0.136 0m2.395
user 0m0.032 0m0.078 0m2.001
sys 0m0.16s 0m0.037 0m0.162
Plurality
Vote func
Look for a candidate called name
If candidate found ,update their vote total and return true
If no candidate found,don’t update any vote and return false
Iterate in the candidates array to see if u match the candidate name ,if u find -update the votes for that
candidate
bool vote(string name);
bool vote( candidate candidates[], int candidate_count , string name) {
for (int i = 0; i < candidate_count ; i++) {
if (strcmp(candidates[i].name, name) == 0) {
candidates[i].votes++;
return true; // Candidate found and vote updated
return false; // Candidate not found
Runoff
Vote func
-Look for a candidate called NAME
-If candidate found,update VOTER preferences so that they are the voter s RANK pereference and return
true
Pseudo :Loop for name match in candidate array then update the global preferences
array(preferences[I int voter ][J int rank]) to indicate that the voter voter has that candidate
as their rank preference (where 0 is the first preference, 1 is the second preference, etc.
// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
// Update vote totals given a new vote
bool vote(string name)
{
for (int i = 0; i < candidate_count ; i++) {
if (strcmp(name,candidates[i].name) == 0) {
candidates[i].votes++;
return true; // Candidate found and vote updated
}
}
return false; // Candidate not found
// Function to vote for a candidate by name
bool vote(candidate candidates[], int candidate_count, string name) {
for (int i = 0; i < candidate_count; i++) {
if (strcmp(candidates[i].name, name) == 0) {
candidates[i].votes++;
return true; // Candidate found and vote updated
filter
gray Aduni rgbtRed+ rgbtGreen+ rgbtBlue si faci media si le dai le dai media la toate ca noua valoare
Rotunjesti noua valoare ca sa fie int din float
helpers.c:20:23: error: assigning to 'RGBTRIPLE' from incompatible type 'int'
grayscale = (int)average;
int result =round(float average);
./filter -r images/yard.bmp out.bmp
Reflect
Loop For each row in image
Swap pixels on horizontally opposite sides(far left becomes far right and viceversa)
int tmp = *a;
*a = *b;
*b = tmp; By doing this way, you can create a "cache" of elements, and after that you can swap the items
Recover
Pseudocod
Open memory card
Repeat until end of card:
Read 512 bytes into a buffer
If start of new JPEG(u know by looking at the 4 first 4 bytes)
If first JPEG(write 000.jpeg write ur first file then close and open new file for new JPEG)
Else
If already found JPEG
repeat reading 512 bytes to check if it’s the start of a jpeg or not
after u reached the end of memory card
Close any remaining files
Inheritance
The line of code you've written seems to be trying to randomly assign an allele to p-
>alleles[0] based on the current alleles of the person and a random number. However, it's
not clear how this line relates to the alleles of the person's parents, which is what the
TODO comment is asking for.
In the context of this problem, you would typically want to randomly choose one of the
alleles from each parent and assign them to the current person. This might involve
generating a random number (0 or 1), using it to index into the parent's alleles, and then
assigning the chosen allele to the current person.
Remember, the goal is to simulate inheritance. Each parent passes on one of their two
alleles to their child, and which one is passed on is random.
Speller
LOAD
1. bool load(const char *dictionary)
{
// TODO
return false;
}
TODO
1.Open a dictionary file
2.Read strings from file
3.Create a new node for each word
4.Hash word to obtain a hash value
5.Insert word into hash table at the location
SIZE
1.Return number of words that are in your dictionary :
a.Iterating over single one of the linked lists inside your hash table counting the number of nodes inside
of each of the linked lists
b.As your loading the hash table keep track of the number of words added into the dictionary so you can
return that value in your size function
-A good way to go about this is to just create a global variable counting the words and keep track of # of
load iterations, then just return its sum during size func
-Declare an int variable in the global scope (outside of the functions) and initialize it to 0. Inside the load
function each time it iterates and adds a word in your hash table, you can just ++ each time it runs, and
just return it in the size function (this is why you declare the variable in the global scope so you can use it
in other functions) if the load function indeed returns true.
https://stackoverflow.com/questions/26491467/read-words-from-file-into-node-struct-in-c
https://stackoverflow.com/questions/3860488/c-copy-string-into-a-linked-list-variable
hello p
from cs50 import get_string
answer = get_string("Greeting: ")
if answer.startswith("Hello"):
print("$0")
elif answer.startswith(" Hello "):
print("$0")
elif answer.startswith("Hello, Newman"):
print("$0")
elif answer.startswith("h"):
print("$20")
elif answer.startswith( "How you doing?" ):
print("$20")
else:
print("$100")
elif "-f" in argv:
s = get_string("Input: ")
print(figlet.renderText(s))
elif "--font" in argv:
s = get_string("Input: ")
print(figlet.renderText(s))
if len(argv) == 2:
print(f"hello, {argv[1]}")
figlet.setFont(font='ok_beer_') # font name should be a string
print(figlet.renderText(s))
elif “-f” in len (argv)
print(f”figlet.renderText{argv[2]}")
figlet.setFont(font=argv[1])
figlet.setFont(font=argv[1])
elif "-f" in argv:
s = get_string("Input: ")
print(figlet.renderText(s))
figlet.setFont(font=argv[1])
elif "--font" in argv:
s = get_string("Input: ")
print(figlet.renderText(s))
python figlet.py -f slant
if len(argv) == 2 and ("-f" not in argv and "--font" not in argv) and argv[1] not in fonts:
sys.exit("Invalid usage")
# Import the get_string function from the cs50 library to get user input
from cs50 import get_string
# Import the argv variable from the sys module to access command-line arguments
from sys import argv
# Import the random module for choosing a random font
import random
# Import the sys module for exiting the program with a specific message
import sys
# Import the Figlet class from the pyfiglet library for creating ASCII art text
from pyfiglet import Figlet
# Define a tuple of valid fonts for the Figlet class
fonts = ("1943____",
"1row",
"3-d",
"3d-ascii",
"3d_diagonal",
"slant"
"rectangles"
"alphabet" )
# Check for invalid command-line usage
if len(argv) == 2 and ("-f" not in argv and "--font" not in argv) and argv[1]
not in fonts:
sys.exit("Invalid usage")
# If there are no command-line arguments, prompt the user for input
if len(argv) <= 1:
s = get_string("Input: ")
# Create a Figlet object with a randomly chosen font and print the ASCII art
figlet = Figlet(font=random.choice(fonts))
print(figlet.renderText(s))
# If the '-f' or '--font' option is present in the command-line arguments, use
the specified font
elif "-f" in argv or "--font" in argv:
# Create a Figlet object with the specified font and print the ASCII art
figlet = Figlet(font=argv[2])
s = get_string("Input: ")
print(figlet.renderText(s))