Initial commit with cleaned history
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// Draws a pyramid using iteration
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void draw(int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get height of pyramid
|
||||
int height = get_int("Height: ");
|
||||
|
||||
// Draw pyramid
|
||||
draw(height);
|
||||
}
|
||||
|
||||
void draw(int n)
|
||||
{
|
||||
// Draw pyramid of height n
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < i + 1; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Implements a phone book without structs
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Arrays of strings
|
||||
string names[] = {"Carter", "David"};
|
||||
string numbers[] = {"+1-617-495-1000", "+1-949-468-2750"};
|
||||
|
||||
// Search for name
|
||||
string name = get_string("Name: ");
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
if (strcmp(names[i], name) == 0)
|
||||
{
|
||||
printf("Found %s\n", numbers[i]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
printf("Not found\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Implements a phone book with structs
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
string name;
|
||||
string number;
|
||||
}
|
||||
person;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
person people[2];
|
||||
|
||||
people[0].name = "Carter";
|
||||
people[0].number = "+1-617-495-1000";
|
||||
|
||||
people[1].name = "David";
|
||||
people[1].number = "+1-949-468-2750";
|
||||
|
||||
// Search for name
|
||||
string name = get_string("Name: ");
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
if (strcmp(people[i].name, name) == 0)
|
||||
{
|
||||
printf("Found %s\n", people[i].number);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
printf("Not found\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Draws a pyramid (incorrectly) using recursion,
|
||||
// must be compiled with -Wno-infinite-recursion
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void draw(int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
draw(1);
|
||||
}
|
||||
|
||||
void draw(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
draw(n + 1);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Draws a pyramid using recursion
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void draw(int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get height of pyramid
|
||||
int height = get_int("Height: ");
|
||||
|
||||
// Draw pyramid
|
||||
draw(height);
|
||||
}
|
||||
|
||||
void draw(int n)
|
||||
{
|
||||
// If nothing to draw
|
||||
if (n <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw pyramid of height n - 1
|
||||
draw(n - 1);
|
||||
|
||||
// Draw one more row of width n
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Implements linear search for integers
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// An array of integers
|
||||
int numbers[] = {20, 500, 10, 5, 100, 1, 50};
|
||||
|
||||
// Search for number
|
||||
int n = get_int("Number: ");
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (numbers[i] == n)
|
||||
{
|
||||
printf("Found\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
printf("Not found\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Implements linear search for strings
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// An array of strings
|
||||
string strings[] = {"battleship", "boot", "cannon", "iron", "thimble", "top hat"};
|
||||
|
||||
// Search for string
|
||||
string s = get_string("String: ");
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (strcmp(strings[i], s) == 0)
|
||||
{
|
||||
printf("Found\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
printf("Not found\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Max number of candidates
|
||||
#define MAX 9
|
||||
|
||||
// Candidates have name and vote count
|
||||
typedef struct
|
||||
{
|
||||
string name;
|
||||
int votes;
|
||||
}
|
||||
candidate;
|
||||
|
||||
// Array of candidates
|
||||
candidate candidates[MAX];
|
||||
|
||||
// Number of candidates
|
||||
int candidate_count;
|
||||
|
||||
// Function prototypes
|
||||
bool vote(string name);
|
||||
void print_winner(void);
|
||||
|
||||
int main(int argc, string argv[])
|
||||
{
|
||||
// Check for invalid usage
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Usage: plurality [candidate ...]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Populate array of candidates
|
||||
candidate_count = argc - 1;
|
||||
if (candidate_count > MAX)
|
||||
{
|
||||
printf("Maximum number of candidates is %i\n", MAX);
|
||||
return 2;
|
||||
}
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
candidates[i].name = argv[i + 1];
|
||||
candidates[i].votes = 0;
|
||||
}
|
||||
|
||||
int voter_count = get_int("Number of voters: ");
|
||||
|
||||
// Loop over all voters
|
||||
for (int i = 0; i < voter_count; i++)
|
||||
{
|
||||
string name = get_string("Vote: ");
|
||||
|
||||
// Check for invalid vote
|
||||
if (!vote(name))
|
||||
{
|
||||
printf("Invalid vote.\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Display winner of election
|
||||
print_winner();
|
||||
}
|
||||
|
||||
// Update vote totals given a new vote
|
||||
bool vote(string name)
|
||||
{
|
||||
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (strcmp(candidates[i].name, name) == 0)
|
||||
{
|
||||
candidates[i].votes += 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Print the winner (or winners) of the election
|
||||
void print_winner(void)
|
||||
{
|
||||
|
||||
int winner = 0;
|
||||
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (candidates[i].votes > winner)
|
||||
{
|
||||
winner = candidates[i].votes;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (candidates[i].votes == winner)
|
||||
{
|
||||
printf("%s\n", candidates[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// Max voters and candidates
|
||||
#define MAX_VOTERS 100
|
||||
#define MAX_CANDIDATES 9
|
||||
|
||||
// preferences[i][j], j is the preference for voter i
|
||||
int preferences[MAX_VOTERS][MAX_CANDIDATES];
|
||||
|
||||
// Candidates have name, vote count, eliminated status
|
||||
typedef struct
|
||||
{
|
||||
string name;
|
||||
int votes;
|
||||
bool eliminated;
|
||||
}
|
||||
candidate;
|
||||
|
||||
// Array of candidates
|
||||
candidate candidates[MAX_CANDIDATES];
|
||||
|
||||
// Numbers of voters and candidates
|
||||
int voter_count;
|
||||
int candidate_count;
|
||||
|
||||
// Function prototypes
|
||||
bool vote(int voter, int rank, string name);
|
||||
void tabulate(void);
|
||||
bool print_winner(void);
|
||||
int find_min(void);
|
||||
bool is_tie(int min);
|
||||
void eliminate(int min);
|
||||
|
||||
int main(int argc, string argv[])
|
||||
{
|
||||
// Check for invalid usage
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Usage: runoff [candidate ...]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Populate array of candidates
|
||||
candidate_count = argc - 1;
|
||||
if (candidate_count > MAX_CANDIDATES)
|
||||
{
|
||||
printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
|
||||
return 2;
|
||||
}
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
candidates[i].name = argv[i + 1];
|
||||
candidates[i].votes = 0;
|
||||
candidates[i].eliminated = false;
|
||||
}
|
||||
|
||||
voter_count = get_int("Number of voters: ");
|
||||
if (voter_count > MAX_VOTERS)
|
||||
{
|
||||
printf("Maximum number of voters is %i\n", MAX_VOTERS);
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Keep querying for votes
|
||||
for (int i = 0; i < voter_count; i++)
|
||||
{
|
||||
|
||||
// Query for each rank
|
||||
for (int j = 0; j < candidate_count; j++)
|
||||
{
|
||||
string name = get_string("Rank %i: ", j + 1);
|
||||
|
||||
// Record vote, unless it's invalid
|
||||
if (!vote(i, j, name))
|
||||
{
|
||||
printf("Invalid vote.\n");
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Keep holding runoffs until winner exists
|
||||
while (true)
|
||||
{
|
||||
// Calculate votes given remaining candidates
|
||||
tabulate();
|
||||
|
||||
// Check if election has been won
|
||||
bool won = print_winner();
|
||||
if (won)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Eliminate last-place candidates
|
||||
int min = find_min();
|
||||
bool tie = is_tie(min);
|
||||
|
||||
// If tie, everyone wins
|
||||
if (tie)
|
||||
{
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (!candidates[i].eliminated)
|
||||
{
|
||||
printf("%s\n", candidates[i].name);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Eliminate anyone with minimum number of votes
|
||||
eliminate(min);
|
||||
|
||||
// Reset vote counts back to zero
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
candidates[i].votes = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Record preference if vote is valid
|
||||
bool vote(int voter, int rank, string name)
|
||||
{
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (strcmp(candidates[i].name, name) == 0)
|
||||
{
|
||||
preferences[voter][rank] = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tabulate votes for non-eliminated candidates
|
||||
void tabulate(void)
|
||||
{
|
||||
for (int i = 0; i < voter_count; i++)
|
||||
{
|
||||
for (int j = 0; j < candidate_count; j++)
|
||||
{
|
||||
if (preferences[i][0] == j && candidates[j].eliminated == false)
|
||||
{
|
||||
candidates[j].votes += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Print the winner of the election, if there is one
|
||||
bool print_winner(void)
|
||||
{
|
||||
int winner = 0;
|
||||
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (candidates[i].votes > winner)
|
||||
{
|
||||
winner = candidates[i].votes;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (candidates[i].votes == winner && candidates[i].votes > (voter_count / 2))
|
||||
{
|
||||
printf("%s\n", candidates[i].name);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return the minimum number of votes any remaining candidate has
|
||||
int find_min(void)
|
||||
{
|
||||
int min = voter_count;
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if ( candidates[i].votes < min && candidates[i].eliminated == false)
|
||||
{
|
||||
min = candidates[i].votes;
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
// Return true if the election is tied between all candidates, false otherwise
|
||||
bool is_tie(int min)
|
||||
{
|
||||
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
|
||||
if (candidates[i].votes > min && candidates[i].eliminated == false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Eliminate the candidate (or candidates) in last place
|
||||
void eliminate(int min)
|
||||
{
|
||||
for (int i = 0; i < candidate_count; i++)
|
||||
{
|
||||
if (candidates[i].votes <= min && candidates[i].eliminated == false)
|
||||
{
|
||||
candidates[i].eliminated = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
|
||||
|
||||
sort1 uses: Bubble sort
|
||||
|
||||
How do you know?: Significant faster than sort3 with a sorted list.
|
||||
|
||||
sort2 uses: Merge sort
|
||||
|
||||
How do you know?: Fastest in all Tests.
|
||||
|
||||
sort3 uses: Selection sort
|
||||
|
||||
How do you know?: Is not Bubble or Merge sort.
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,16 @@
|
||||
n = 50000
|
||||
|
||||
random:
|
||||
sort1: 8,250s
|
||||
*sort2: 0,989s
|
||||
sort3: 4,138s
|
||||
|
||||
reversed:
|
||||
sort1: 6,467s
|
||||
*sort2: 0,902s
|
||||
sort3: 4,331s
|
||||
|
||||
sorted:
|
||||
sort1: 0,566s
|
||||
*sort2: 0,466s
|
||||
sort3: 3,641s
|
||||
@@ -0,0 +1,15 @@
|
||||
// Explicitly casts chars to ints
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("String: ");
|
||||
for (int i = 0; i < strlen(s); i++)
|
||||
{
|
||||
int c = (int) s[i];
|
||||
printf("%c %i\n", s[i], c);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Implicitly casts chars to ints
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("String: ");
|
||||
for (int i = 0; i < strlen(s); i++)
|
||||
{
|
||||
printf("%c %i\n", s[i], s[i]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Buggy example for printf
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i <= 3; i++)
|
||||
{
|
||||
printf("#\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Buggy example for debug50
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int get_negative_int(void);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = get_negative_int();
|
||||
printf("%i\n", i);
|
||||
}
|
||||
|
||||
// Prompt user for positive integer
|
||||
int get_negative_int(void)
|
||||
{
|
||||
int n;
|
||||
do
|
||||
{
|
||||
n = get_int("Negative Integer: ");
|
||||
}
|
||||
while (n < 0);
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Uses get_string
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string answer = get_string("What's your name? ");
|
||||
printf("hello, %s\n", answer);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Uses get_string with better-named variable
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string name = get_string("What's your name? ");
|
||||
printf("hello, %s\n", name);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Prints a command-line argument
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, string argv[])
|
||||
{
|
||||
if (argc == 2)
|
||||
{
|
||||
printf("hello, %s\n", argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Prints command-line arguments
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, string argv[])
|
||||
{
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
printf("%s\n", argv[i]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string name = get_string("What's your name? ");
|
||||
printf("hello, %s\n", name);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Prints chars
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char c1 = 'H';
|
||||
char c2 = 'I';
|
||||
char c3 = '!';
|
||||
|
||||
printf("%c%c%c\n", c1, c2, c3);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Prints chars' ASCII codes
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char c1 = 'H';
|
||||
char c2 = 'I';
|
||||
char c3 = '!';
|
||||
|
||||
printf("%i %i %i\n", c1, c2, c3);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Prints string
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = "HI!";
|
||||
printf("%s\n", s);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Prints string's ASCII codes
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = "HI!";
|
||||
printf("%i %i %i\n", s[0], s[1], s[2]);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Prints string's ASCII codes, including NUL
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = "HI!";
|
||||
printf("%i %i %i %i\n", s[0], s[1], s[2], s[3]);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Multiple strings
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = "HI!";
|
||||
string t = "BYE!";
|
||||
|
||||
printf("%s\n", s);
|
||||
printf("%s\n", t);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Array of strings
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string words[2];
|
||||
|
||||
words[0] = "HI!";
|
||||
words[1] = "BYE!";
|
||||
|
||||
printf("%s\n", words[0]);
|
||||
printf("%s\n", words[1]);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string words[2];
|
||||
|
||||
words[0] = "HI!";
|
||||
words[1] = "BYE!";
|
||||
|
||||
printf("%c%c%c\n", words[0][0], words[0][1], words[0][2]);
|
||||
printf("%c%c%c%c\n", words[1][0], words[1][1], words[1][2], words[1][3]);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Determines the length of a string
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt for user's name
|
||||
string name = get_string("Name: ");
|
||||
|
||||
// Count number of characters up until '\0' (aka NUL)
|
||||
int n = 0;
|
||||
while (name[n] != '\0')
|
||||
{
|
||||
n++;
|
||||
}
|
||||
printf("%i\n", n);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Determines the length of a string using a function
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int string_length(string s);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt for user's name
|
||||
string name = get_string("Name: ");
|
||||
int length = string_length(name);
|
||||
printf("%i\n", length);
|
||||
}
|
||||
|
||||
int string_length(string s)
|
||||
{
|
||||
// Count number of characters up until '\0' (aka NUL)
|
||||
int n = 0;
|
||||
while (s[n] != '\0')
|
||||
{
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Determines the length of a string using a function
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt for user's name
|
||||
string name = get_string("Name: ");
|
||||
int length = strlen(name);
|
||||
printf("%i\n", length);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Averages three (hardcoded) numbers
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Scores
|
||||
int score1 = 72;
|
||||
int score2 = 73;
|
||||
int score3 = 33;
|
||||
|
||||
// Print average
|
||||
printf("Average: %f\n", (score1 + score2 + score3) / 3.0);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Averages three (hardcoded) numbers using an array
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Scores
|
||||
int scores[3];
|
||||
scores[0] = 72;
|
||||
scores[1] = 73;
|
||||
scores[2] = 33;
|
||||
|
||||
// Print average
|
||||
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Averages three numbers using an array
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get scores
|
||||
int scores[3];
|
||||
scores[0] = get_int("Score: ");
|
||||
scores[1] = get_int("Score: ");
|
||||
scores[2] = get_int("Score: ");
|
||||
|
||||
// Print average
|
||||
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Averages three numbers using an array and a loop
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get scores
|
||||
int scores[3];
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
scores[i] = get_int("Score: ");
|
||||
}
|
||||
|
||||
// Print average
|
||||
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Averages three numbers using an array, a constant, and a helper function
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Constant
|
||||
const int N = 3;
|
||||
|
||||
// Prototype
|
||||
float average(int length, int array[]);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get scores
|
||||
int scores[N];
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
scores[i] = get_int("Score: ");
|
||||
}
|
||||
|
||||
// Print average
|
||||
printf("Average: %f\n", average(N, scores));
|
||||
}
|
||||
|
||||
float average(int length, int array[])
|
||||
{
|
||||
// Calculate average
|
||||
int sum = 0;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
sum += array[i];
|
||||
}
|
||||
return sum / (float) length;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Returns explicit value from main
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, string argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("Missing command-line argument\n");
|
||||
return 1;
|
||||
}
|
||||
printf("hello, %s\n", argv[1]);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Prints string char by char, using strlen
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("Input: ");
|
||||
printf("Output: ");
|
||||
for (int i = 0; i < strlen(s); i++)
|
||||
{
|
||||
printf("%c", s[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Prints string char by char, using strlen, remembering string's length
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("Input: ");
|
||||
printf("Output: ");
|
||||
for (int i = 0, n = strlen(s); i < n; i++)
|
||||
{
|
||||
printf("%c", s[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Uppercases a string
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("Before: ");
|
||||
printf("After: ");
|
||||
for (int i = 0, n = strlen(s); i < n; i++)
|
||||
{
|
||||
if (s[i] >= 'a' && s[i] <= 'z')
|
||||
{
|
||||
printf("%c", s[i] - 32);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%c", s[i]);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Uppercases string using ctype library (and an unnecessary condition)
|
||||
|
||||
#include <cs50.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("Before: ");
|
||||
printf("After: ");
|
||||
for (int i = 0, n = strlen(s); i < n; i++)
|
||||
{
|
||||
if (islower(s[i]))
|
||||
{
|
||||
printf("%c", toupper(s[i]));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%c", s[i]);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Uppercases string using ctype library
|
||||
|
||||
#include <cs50.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string s = get_string("Before: ");
|
||||
printf("After: ");
|
||||
for (int i = 0, n = strlen(s); i < n; i++)
|
||||
{
|
||||
printf("%c", toupper(s[i]));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
const int BITS_IN_BYTE = 8;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string text = get_string("Text: ");
|
||||
|
||||
// Loop over every character
|
||||
|
||||
int i = 0;
|
||||
int l = 0;
|
||||
double rest = 0;
|
||||
double zahl;
|
||||
int len = 0;
|
||||
int bi;
|
||||
int n = 0;
|
||||
int m = 0;
|
||||
|
||||
for (i = 0, len = strlen(text); i < len; i++)
|
||||
{
|
||||
zahl = text[i];
|
||||
bi = 0;
|
||||
for (l = 0; l < BITS_IN_BYTE; l++)
|
||||
{
|
||||
zahl = (zahl - (rest * pow(10, -1))) / 2.0 ;
|
||||
rest = (zahl - floor(zahl)) * 10;
|
||||
|
||||
if (rest > 0)
|
||||
{
|
||||
bi = bi + pow(10, l);
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (bi >= 1000000 && bi <= 1111111)
|
||||
{
|
||||
char test[bi];
|
||||
sprintf(test, "0%d", bi);
|
||||
//printf("%s",test);
|
||||
for (n = 0, m = strlen(test); n < m; n++)
|
||||
{
|
||||
if (test[n] == 48)
|
||||
{
|
||||
// Dark emoji
|
||||
printf("\U000026AB");
|
||||
}
|
||||
else if (test[n] == 49)
|
||||
{
|
||||
// Light emoji
|
||||
printf("\U0001F7E1");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (bi >= 100000 && bi <= 111111)
|
||||
{
|
||||
char test[bi];
|
||||
sprintf(test, "00%d", bi);
|
||||
//printf("%s",test);
|
||||
for (n = 0, m = strlen(test); n < m; n++)
|
||||
{
|
||||
if (test[n] == 48)
|
||||
{
|
||||
// Dark emoji
|
||||
printf("\U000026AB");
|
||||
}
|
||||
else if (test[n] == 49)
|
||||
{
|
||||
// Light emoji
|
||||
printf("\U0001F7E1");
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get Text
|
||||
string text = get_string("Text: ");
|
||||
|
||||
// Letter, word, sentence count
|
||||
double lettercount = 0;
|
||||
double wordcount = 1;
|
||||
double sentencecount = 0;
|
||||
|
||||
for (int i = 0, len = strlen(text); i < len; i++)
|
||||
{
|
||||
if ((text[i] >= 65 && text[i] <= 90) || (text[i] >= 97 && text[i] <= 122))
|
||||
{
|
||||
lettercount += 1;
|
||||
}
|
||||
else if (text[i] == 32)
|
||||
{
|
||||
wordcount += 1;
|
||||
}
|
||||
else if (text[i] == 33 || text[i] == 63 || text[i] == 46)
|
||||
{
|
||||
sentencecount += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate Index
|
||||
double L = lettercount / wordcount * 100.0;
|
||||
double S = sentencecount / wordcount * 100.0;
|
||||
double index = 0.0588 * L - 0.296 * S - 15.8;
|
||||
|
||||
// Print conditions
|
||||
if (index <= 1)
|
||||
{
|
||||
printf("Before Grade 1\n");
|
||||
}
|
||||
else if (index >= 16)
|
||||
{
|
||||
printf("Grade 16+\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Grade %i\n", (int) round(index));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <ctype.h>
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Points assigned to each letter of the alphabet
|
||||
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
|
||||
|
||||
int compute_score(string word);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get input words from both players
|
||||
string word1 = get_string("Player 1: ");
|
||||
string word2 = get_string("Player 2: ");
|
||||
|
||||
// Score both words
|
||||
int score1 = compute_score(word1);
|
||||
int score2 = compute_score(word2);
|
||||
|
||||
// TODO: Print the winner
|
||||
if (score1 > score2)
|
||||
{
|
||||
printf("Player 1 wins!");
|
||||
}
|
||||
else if (score1 < score2)
|
||||
{
|
||||
printf("Player 2 wins!");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Tie!");
|
||||
}
|
||||
}
|
||||
|
||||
int compute_score(string word)
|
||||
{
|
||||
// TODO: Compute and return score for string
|
||||
int score = 0;
|
||||
|
||||
for (int i = 0, len = strlen(word); i < len; i++)
|
||||
{
|
||||
if (isupper(word[i]))
|
||||
{
|
||||
score += POINTS[word[i] - 'A'];
|
||||
}
|
||||
else if (islower(word[i]))
|
||||
{
|
||||
score += POINTS[word[i] - 'a'];
|
||||
}
|
||||
}
|
||||
return score;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Logical operators
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user to agree
|
||||
char c = get_char("Do you agree? ");
|
||||
|
||||
// Check whether agreed
|
||||
if (c == 'Y' || c == 'y')
|
||||
{
|
||||
printf("Agreed.\n");
|
||||
}
|
||||
else if (c == 'N' || c == 'n')
|
||||
{
|
||||
printf("Not agreed.\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Addition with int, allows for overflow
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
int x = get_int("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
int y = get_int("y: ");
|
||||
|
||||
// Perform addition
|
||||
printf("%i\n", x + y);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Addition with long
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
long x = get_long("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
long y = get_long("y: ");
|
||||
|
||||
// Perform addition
|
||||
printf("%li\n", x + y);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Division with longs, demonstrating truncation
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
long x = get_long("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
long y = get_long("y: ");
|
||||
|
||||
// Divide x by y
|
||||
printf("%li\n", x / y);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Division with longs, demonstrating truncation
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
long x = get_long("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
long y = get_long("y: ");
|
||||
|
||||
// Divide x by y
|
||||
float z = x / y;
|
||||
printf("%f\n", z);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Division with longs, demonstrating type casting
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
long x = get_long("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
long y = get_long("y: ");
|
||||
|
||||
// Divide x by y
|
||||
float z = (float) x / (float) y;
|
||||
printf("%f\n", z);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Division with longs, demonstrating floating-point imprecision
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
long x = get_long("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
long y = get_long("y: ");
|
||||
|
||||
// Divide x by y
|
||||
float z = (float) x / (float) y;
|
||||
printf("%.20f\n", z);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Division with longs, demonstrating double
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for x
|
||||
long x = get_long("x: ");
|
||||
|
||||
// Prompt user for y
|
||||
long y = get_long("y: ");
|
||||
|
||||
// Divide x by y
|
||||
double z = (double) x / (double) y;
|
||||
printf("%.20f\n", z);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Conditionals, Boolean expressions, relational operators
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for integers
|
||||
int x = get_int("What's x? ");
|
||||
int y = get_int("What's y? ");
|
||||
|
||||
// Compare integers
|
||||
if (x < y)
|
||||
{
|
||||
printf("x is less than y\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Conditionals, Boolean expressions, relational operators
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for integers
|
||||
int x = get_int("What's x? ");
|
||||
int y = get_int("What's y? ");
|
||||
|
||||
// Compare integers
|
||||
if (x < y)
|
||||
{
|
||||
printf("x is less than y\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("x is not less than y\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Conditionals, Boolean expressions, relational operators
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for integers
|
||||
int x = get_int("What's x? ");
|
||||
int y = get_int("What's y? ");
|
||||
|
||||
// Compare integers
|
||||
if (x < y)
|
||||
{
|
||||
printf("x is less than y\n");
|
||||
}
|
||||
else if (x > y)
|
||||
{
|
||||
printf("x is greater than y\n");
|
||||
}
|
||||
else if (x == y)
|
||||
{
|
||||
printf("x is equal to y\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Conditionals, Boolean expressions, relational operators
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for integers
|
||||
int x = get_int("What's x? ");
|
||||
int y = get_int("What's y? ");
|
||||
|
||||
// Compare integers
|
||||
if (x < y)
|
||||
{
|
||||
printf("x is less than y\n");
|
||||
}
|
||||
else if (x > y)
|
||||
{
|
||||
printf("x is greater than y\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("x is equal to y\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Counting
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
printf("\r%i", i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Counting
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
printf("\r%i", i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// A program that says hello to the world
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// get_string and printf with incorrect placeholder
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string answer = get_string("What's your name? ");
|
||||
printf("hello, answer\n");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// get_string and printf with %s
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string answer = get_string("What's your name? ");
|
||||
printf("hello, %s\n", answer);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Prints a row of 4 question marks
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("????\n");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Prints a row of 4 question marks with a loop
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
printf("?");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Prints a column of 3 bricks with a loop
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
printf("#\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Prints a 3-by-3 grid of bricks with nested loops
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Prints a 3-by-3 grid of bricks with nested loops using a constant
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const int n = 3;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Prints an n-by-n grid of bricks with nested loops
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int n = get_int("Size: ");
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Prints an n-by-n grid of bricks, re-prompting user for positive integer
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int n = get_int("Size: ");
|
||||
while (n < 1)
|
||||
{
|
||||
n = get_int("Size: ");
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Prints an n-by-n grid of bricks, re-prompting user for positive integer
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Get size of grid
|
||||
int n;
|
||||
do
|
||||
{
|
||||
n = get_int("Size: ");
|
||||
}
|
||||
while (n < 1);
|
||||
|
||||
// Print grid of bricks
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Prints an n-by-n grid of bricks, re-prompting user for positive integer
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int get_size(void);
|
||||
void print_grid(int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int n = get_size();
|
||||
print_grid(n);
|
||||
}
|
||||
|
||||
int get_size(void)
|
||||
{
|
||||
int n;
|
||||
do
|
||||
{
|
||||
n = get_int("Size: ");
|
||||
}
|
||||
while (n < 1);
|
||||
return n;
|
||||
}
|
||||
|
||||
void print_grid(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Opportunity for better design
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("meow\n");
|
||||
printf("meow\n");
|
||||
printf("meow\n");
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Better design
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < 3)
|
||||
{
|
||||
printf("meow\n");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Better design
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
printf("meow\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Abstraction
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void meow(void);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
meow();
|
||||
}
|
||||
}
|
||||
|
||||
// Meow once
|
||||
void meow(void)
|
||||
{
|
||||
printf("meow\n");
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Abstraction with parameterization
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void meow(int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
meow(3);
|
||||
}
|
||||
|
||||
// Meow some number of times
|
||||
void meow(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("meow\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Conditionals, Boolean expressions, relational operators
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for points
|
||||
int points = get_int("How many points did you lose? ");
|
||||
|
||||
// Compare points against mine
|
||||
if (points < 2)
|
||||
{
|
||||
printf("You lost fewer points than me.\n");
|
||||
}
|
||||
else if (points > 2)
|
||||
{
|
||||
printf("You lost more points than me.\n");
|
||||
}
|
||||
else if (points == 2)
|
||||
{
|
||||
printf("You lost the same number of points as me.\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Design
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Prompt user for points
|
||||
int points = get_int("How many points did you lose? ");
|
||||
|
||||
// Compare points against mine
|
||||
if (points < 2)
|
||||
{
|
||||
printf("You lost fewer points than me.\n");
|
||||
}
|
||||
else if (points > 2)
|
||||
{
|
||||
printf("You lost more points than me.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("You lost the same number of points as me.\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Constants
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Number of points that I lost
|
||||
const int MINE = 2;
|
||||
|
||||
// Prompt user for points
|
||||
int points = get_int("How many points did you lose? ");
|
||||
|
||||
// Compare points against mine
|
||||
if (points < MINE)
|
||||
{
|
||||
printf("You lost fewer points than me.\n");
|
||||
}
|
||||
else if (points > MINE)
|
||||
{
|
||||
printf("You lost more points than me.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("You lost the same number of points as me.\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int get_cents(void);
|
||||
int calculate_quarters(int cents);
|
||||
int calculate_dimes(int cents);
|
||||
int calculate_nickels(int cents);
|
||||
int calculate_pennies(int cents);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Ask how many cents the customer is owed
|
||||
int cents = get_cents();
|
||||
|
||||
// Calculate the number of quarters to give the customer
|
||||
int quarters = calculate_quarters(cents);
|
||||
cents = cents - quarters * 25;
|
||||
|
||||
// Calculate the number of dimes to give the customer
|
||||
int dimes = calculate_dimes(cents);
|
||||
cents = cents - dimes * 10;
|
||||
|
||||
// Calculate the number of nickels to give the customer
|
||||
int nickels = calculate_nickels(cents);
|
||||
cents = cents - nickels * 5;
|
||||
|
||||
// Calculate the number of pennies to give the customer
|
||||
int pennies = calculate_pennies(cents);
|
||||
cents = cents - pennies * 1;
|
||||
|
||||
// Sum coins
|
||||
int coins = quarters + dimes + nickels + pennies;
|
||||
|
||||
// Print total number of coins to give the customer
|
||||
printf("%i\n", coins);
|
||||
}
|
||||
|
||||
int get_cents(void)
|
||||
{
|
||||
int cents;
|
||||
do
|
||||
{
|
||||
cents = get_int("Number of cents: \n");
|
||||
}
|
||||
while (cents < 0);
|
||||
return cents;
|
||||
|
||||
}
|
||||
|
||||
int calculate_quarters(int cents)
|
||||
{
|
||||
int quarters;
|
||||
quarters = cents / 25;
|
||||
return quarters;
|
||||
}
|
||||
|
||||
int calculate_dimes(int cents)
|
||||
{
|
||||
int dimes;
|
||||
dimes = cents / 10;
|
||||
return dimes;
|
||||
}
|
||||
|
||||
int calculate_nickels(int cents)
|
||||
{
|
||||
int nickels;
|
||||
nickels = cents / 5;
|
||||
return nickels;
|
||||
}
|
||||
|
||||
int calculate_pennies(int cents)
|
||||
{
|
||||
int pennies;
|
||||
pennies = cents / 1;
|
||||
return pennies;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//// TODO ////
|
||||
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int modulo = 0;
|
||||
int start = 0;
|
||||
long number = 0;
|
||||
|
||||
// Promt for input
|
||||
number = get_long("Number: ");
|
||||
|
||||
printf("%li\n", number);
|
||||
|
||||
// Calculate checksum
|
||||
//for (int i = length; i < length; i-=2)
|
||||
//{
|
||||
// printf("number[i]");
|
||||
//}
|
||||
|
||||
|
||||
// Check for card length and starting digits
|
||||
|
||||
|
||||
// If invalid Print INVALID\n
|
||||
|
||||
if (modulo != 0)
|
||||
{
|
||||
printf("INVALID\n");
|
||||
}
|
||||
|
||||
// Else
|
||||
else
|
||||
{
|
||||
// Print AMEX\n
|
||||
|
||||
if (start == 34 || start == 37)
|
||||
{
|
||||
printf("AMEX\n");
|
||||
}
|
||||
|
||||
// Print MASTERCARD\n
|
||||
else if (start == 51 || start == 52 || start == 53 || start == 54 || start == 55)
|
||||
{
|
||||
printf("MASTERCARD\n");
|
||||
}
|
||||
|
||||
// Print VISA\n
|
||||
else if (start == 4)
|
||||
{
|
||||
printf("VISA\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <cs50.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
string name = get_string("What is your Name? ");
|
||||
printf("hello, %s\n ", name);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int size;
|
||||
int spaces;
|
||||
int hashes;
|
||||
|
||||
// Asking for size
|
||||
do
|
||||
{
|
||||
size = get_int("Size: ");
|
||||
}
|
||||
while (size < 1 || size > 8);
|
||||
|
||||
// Building
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
|
||||
// left spaces
|
||||
for (spaces = (size - i); spaces >= 2; spaces--)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
// hashes
|
||||
for (hashes = 0; hashes <= i; hashes++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
|
||||
// middle spaces
|
||||
printf(" ");
|
||||
|
||||
// hashes
|
||||
for (hashes = 0; hashes <= i; hashes++)
|
||||
{
|
||||
printf("#");
|
||||
}
|
||||
|
||||
// next line
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <cs50.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// TODO: Prompt for start size
|
||||
int start;
|
||||
do
|
||||
{
|
||||
start = get_int("Start size over 9: ");
|
||||
}
|
||||
while (start < 9);
|
||||
|
||||
|
||||
|
||||
// TODO: Prompt for end size
|
||||
int end;
|
||||
do
|
||||
{
|
||||
end = get_int("End size: ");
|
||||
}
|
||||
while (end < start);
|
||||
|
||||
// TODO: Calculate number of years until we reach threshold
|
||||
int years = 0;
|
||||
|
||||
while (start < end)
|
||||
{
|
||||
start = start + (start / 3) - (start / 4);
|
||||
years++;
|
||||
}
|
||||
|
||||
// TODO: Print number of years
|
||||
printf("Years: %i\n", years);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
filter:
|
||||
clang -ggdb3 -gdwarf-4 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-gnu-folding-constant -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow -lm -o filter filter.c helpers.c
|
||||
@@ -0,0 +1,57 @@
|
||||
// BMP-related data types based on Microsoft's own
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// These data types are essentially aliases for C/C++ primitive data types.
|
||||
// Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
|
||||
// See https://en.wikipedia.org/wiki/C_data_types#stdint.h for more on stdint.h.
|
||||
|
||||
typedef uint8_t BYTE;
|
||||
typedef uint32_t DWORD;
|
||||
typedef int32_t LONG;
|
||||
typedef uint16_t WORD;
|
||||
|
||||
// The BITMAPFILEHEADER structure contains information about the type, size,
|
||||
// and layout of a file that contains a DIB [device-independent bitmap].
|
||||
// Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
|
||||
|
||||
typedef struct
|
||||
{
|
||||
WORD bfType;
|
||||
DWORD bfSize;
|
||||
WORD bfReserved1;
|
||||
WORD bfReserved2;
|
||||
DWORD bfOffBits;
|
||||
} __attribute__((__packed__))
|
||||
BITMAPFILEHEADER;
|
||||
|
||||
// The BITMAPINFOHEADER structure contains information about the
|
||||
// dimensions and color format of a DIB [device-independent bitmap].
|
||||
// Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
|
||||
|
||||
typedef struct
|
||||
{
|
||||
DWORD biSize;
|
||||
LONG biWidth;
|
||||
LONG biHeight;
|
||||
WORD biPlanes;
|
||||
WORD biBitCount;
|
||||
DWORD biCompression;
|
||||
DWORD biSizeImage;
|
||||
LONG biXPelsPerMeter;
|
||||
LONG biYPelsPerMeter;
|
||||
DWORD biClrUsed;
|
||||
DWORD biClrImportant;
|
||||
} __attribute__((__packed__))
|
||||
BITMAPINFOHEADER;
|
||||
|
||||
// The RGBTRIPLE structure describes a color consisting of relative intensities of
|
||||
// red, green, and blue. Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
|
||||
|
||||
typedef struct
|
||||
{
|
||||
BYTE rgbtBlue;
|
||||
BYTE rgbtGreen;
|
||||
BYTE rgbtRed;
|
||||
} __attribute__((__packed__))
|
||||
RGBTRIPLE;
|
||||
@@ -0,0 +1,150 @@
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "helpers.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// Define allowable filters
|
||||
char *filters = "bgrs";
|
||||
|
||||
// Get filter flag and check validity
|
||||
char filter = getopt(argc, argv, filters);
|
||||
if (filter == '?')
|
||||
{
|
||||
printf("Invalid filter.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Ensure only one filter
|
||||
if (getopt(argc, argv, filters) != -1)
|
||||
{
|
||||
printf("Only one filter allowed.\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Ensure proper usage
|
||||
if (argc != optind + 2)
|
||||
{
|
||||
printf("Usage: ./filter [flag] infile outfile\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Remember filenames
|
||||
char *infile = argv[optind];
|
||||
char *outfile = argv[optind + 1];
|
||||
|
||||
// Open input file
|
||||
FILE *inptr = fopen(infile, "r");
|
||||
if (inptr == NULL)
|
||||
{
|
||||
printf("Could not open %s.\n", infile);
|
||||
return 4;
|
||||
}
|
||||
|
||||
// Open output file
|
||||
FILE *outptr = fopen(outfile, "w");
|
||||
if (outptr == NULL)
|
||||
{
|
||||
fclose(inptr);
|
||||
printf("Could not create %s.\n", outfile);
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Read infile's BITMAPFILEHEADER
|
||||
BITMAPFILEHEADER bf;
|
||||
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
|
||||
|
||||
// Read infile's BITMAPINFOHEADER
|
||||
BITMAPINFOHEADER bi;
|
||||
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
|
||||
|
||||
// Ensure infile is (likely) a 24-bit uncompressed BMP 4.0
|
||||
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
|
||||
bi.biBitCount != 24 || bi.biCompression != 0)
|
||||
{
|
||||
fclose(outptr);
|
||||
fclose(inptr);
|
||||
printf("Unsupported file format.\n");
|
||||
return 6;
|
||||
}
|
||||
|
||||
// Get image's dimensions
|
||||
int height = abs(bi.biHeight);
|
||||
int width = bi.biWidth;
|
||||
|
||||
// Allocate memory for image
|
||||
RGBTRIPLE(*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));
|
||||
if (image == NULL)
|
||||
{
|
||||
printf("Not enough memory to store image.\n");
|
||||
fclose(outptr);
|
||||
fclose(inptr);
|
||||
return 7;
|
||||
}
|
||||
|
||||
// Determine padding for scanlines
|
||||
int padding = (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;
|
||||
|
||||
// Iterate over infile's scanlines
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
// Read row into pixel array
|
||||
fread(image[i], sizeof(RGBTRIPLE), width, inptr);
|
||||
|
||||
// Skip over padding
|
||||
fseek(inptr, padding, SEEK_CUR);
|
||||
}
|
||||
|
||||
// Filter image
|
||||
switch (filter)
|
||||
{
|
||||
// Blur
|
||||
case 'b':
|
||||
blur(height, width, image);
|
||||
break;
|
||||
|
||||
// Grayscale
|
||||
case 'g':
|
||||
grayscale(height, width, image);
|
||||
break;
|
||||
|
||||
// Reflection
|
||||
case 'r':
|
||||
reflect(height, width, image);
|
||||
break;
|
||||
|
||||
// Sepia
|
||||
case 's':
|
||||
sepia(height, width, image);
|
||||
break;
|
||||
}
|
||||
|
||||
// Write outfile's BITMAPFILEHEADER
|
||||
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
|
||||
|
||||
// Write outfile's BITMAPINFOHEADER
|
||||
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
|
||||
|
||||
// Write new pixels to outfile
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
// Write row to outfile
|
||||
fwrite(image[i], sizeof(RGBTRIPLE), width, outptr);
|
||||
|
||||
// Write padding at end of row
|
||||
for (int k = 0; k < padding; k++)
|
||||
{
|
||||
fputc(0x00, outptr);
|
||||
}
|
||||
}
|
||||
|
||||
// Free memory for image
|
||||
free(image);
|
||||
|
||||
// Close files
|
||||
fclose(inptr);
|
||||
fclose(outptr);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
#include "helpers.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Convert image to grayscale
|
||||
// The function grayscale takes three arguments: height and width, which are integers representing the dimensions of an image, and image, which is a two-dimensional array of type RGBTRIPLE representing the pixels of the image. The function converts the image to grayscale by setting the red, green, and blue components of each pixel to the mean of the original values. The result is stored back in the image array. The function uses nested loops to iterate over all pixels in the image. For each pixel, it calculates the mean of its red, green, and blue components using the formula (red + green + blue) / 3, and rounds the result to the nearest integer using the round function. Then it sets the red, green, and blue components of the pixel to the calculated mean. Finally, the function returns void, indicating that it does not return any value.
|
||||
void grayscale(int height, int width, RGBTRIPLE image[height][width])
|
||||
{
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
int mean = (int) round((image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3.0);
|
||||
image[i][j].rgbtRed = mean;
|
||||
image[i][j].rgbtGreen = mean;
|
||||
image[i][j].rgbtBlue = mean;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert image to sepia
|
||||
void sepia(int height, int width, RGBTRIPLE image[height][width])
|
||||
{
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
int sepiaRed = (int) round(.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue) ;
|
||||
int sepiaGreen = (int) round(.349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue);
|
||||
int sepiaBlue = (int) round(.272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue);
|
||||
|
||||
if (sepiaRed > 255)
|
||||
{
|
||||
sepiaRed = 255;
|
||||
}
|
||||
|
||||
if (sepiaGreen > 255)
|
||||
{
|
||||
sepiaGreen = 255;
|
||||
}
|
||||
|
||||
if (sepiaBlue > 255)
|
||||
{
|
||||
sepiaBlue = 255;
|
||||
}
|
||||
|
||||
image[i][j].rgbtRed = sepiaRed;
|
||||
image[i][j].rgbtGreen = sepiaGreen;
|
||||
image[i][j].rgbtBlue = sepiaBlue;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Reflect image horizontally
|
||||
void reflect(int height, int width, RGBTRIPLE image[height][width])
|
||||
{
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width / 2; j++)
|
||||
{
|
||||
RGBTRIPLE og = image[i][j];
|
||||
image[i][j] = image[i][width - 1 - j];
|
||||
image[i][width - 1 - j] = og;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Blur image
|
||||
void blur(int height, int width, RGBTRIPLE image[height][width])
|
||||
{
|
||||
RGBTRIPLE og[height][width];
|
||||
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
og[i][j] = image[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
// inbetween
|
||||
if (i >= 1 && i < height - 1 && j >= 1 && j < width - 1)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i - 1][j - 1].rgbtRed +
|
||||
og[i - 1][j].rgbtRed +
|
||||
og[i - 1][j + 1].rgbtRed +
|
||||
og[i][j - 1].rgbtRed +
|
||||
og[i][j + 1].rgbtRed +
|
||||
og[i + 1][j - 1].rgbtRed +
|
||||
og[i + 1][j].rgbtRed +
|
||||
og[i + 1][j + 1].rgbtRed) / 9.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i - 1][j - 1].rgbtGreen +
|
||||
og[i - 1][j].rgbtGreen +
|
||||
og[i - 1][j + 1].rgbtGreen +
|
||||
og[i][j - 1].rgbtGreen +
|
||||
og[i][j + 1].rgbtGreen +
|
||||
og[i + 1][j - 1].rgbtGreen +
|
||||
og[i + 1][j].rgbtGreen +
|
||||
og[i + 1][j + 1].rgbtGreen) / 9.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i - 1][j - 1].rgbtBlue +
|
||||
og[i - 1][j].rgbtBlue +
|
||||
og[i - 1][j + 1].rgbtBlue +
|
||||
og[i][j - 1].rgbtBlue +
|
||||
og[i][j + 1].rgbtBlue +
|
||||
og[i + 1][j - 1].rgbtBlue +
|
||||
og[i + 1][j].rgbtBlue +
|
||||
og[i + 1][j + 1].rgbtBlue) / 9.0);
|
||||
}
|
||||
|
||||
// Corners
|
||||
// upper left corner
|
||||
if (i == 0 && j == 0)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i][j + 1].rgbtRed +
|
||||
og[i + 1][j].rgbtRed +
|
||||
og[i + 1][j + 1].rgbtRed) / 4.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i][j + 1].rgbtGreen +
|
||||
og[i + 1][j].rgbtGreen +
|
||||
og[i + 1][j + 1].rgbtGreen) / 4.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i][j + 1].rgbtBlue +
|
||||
og[i + 1][j].rgbtBlue +
|
||||
og[i + 1][j + 1].rgbtBlue) / 4.0);
|
||||
}
|
||||
// upper right corner
|
||||
if (i == 0 && j == width - 1)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i][j - 1].rgbtRed +
|
||||
og[i + 1][j - 1].rgbtRed +
|
||||
og[i + 1][j].rgbtRed) / 4.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i][j - 1].rgbtGreen +
|
||||
og[i + 1][j - 1].rgbtGreen +
|
||||
og[i + 1][j].rgbtGreen) / 4.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i][j - 1].rgbtBlue +
|
||||
og[i + 1][j - 1].rgbtBlue +
|
||||
og[i + 1][j].rgbtBlue) / 4.0);
|
||||
}
|
||||
|
||||
// lower left corner
|
||||
if (i == height - 1 && j == 0)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i - 1][j].rgbtRed +
|
||||
og[i - 1][j + 1].rgbtRed +
|
||||
og[i][j + 1].rgbtRed) / 4.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i - 1][j].rgbtGreen +
|
||||
og[i - 1][j + 1].rgbtGreen +
|
||||
og[i][j + 1].rgbtGreen) / 4.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i - 1][j].rgbtBlue +
|
||||
og[i - 1][j + 1].rgbtBlue +
|
||||
og[i][j + 1].rgbtBlue) / 4.0);
|
||||
}
|
||||
|
||||
// lower right corner
|
||||
if (i == height - 1 && j == width - 1)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i - 1][j - 1].rgbtRed +
|
||||
og[i - 1][j].rgbtRed +
|
||||
og[i][j - 1].rgbtRed) / 4.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i - 1][j - 1].rgbtGreen +
|
||||
og[i - 1][j].rgbtGreen +
|
||||
og[i][j - 1].rgbtGreen) / 4.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i - 1][j - 1].rgbtBlue +
|
||||
og[i - 1][j].rgbtBlue +
|
||||
og[i][j - 1].rgbtBlue) / 4.0);
|
||||
}
|
||||
|
||||
// Sides
|
||||
// Upper side
|
||||
if (i == 0 && j > 0 && j < width - 1)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i][j - 1].rgbtRed +
|
||||
og[i][j + 1].rgbtRed +
|
||||
og[i + 1][j - 1].rgbtRed +
|
||||
og[i + 1][j].rgbtRed +
|
||||
og[i + 1][j + 1].rgbtRed) / 6.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i][j - 1].rgbtGreen +
|
||||
og[i][j + 1].rgbtGreen +
|
||||
og[i + 1][j - 1].rgbtGreen +
|
||||
og[i + 1][j].rgbtGreen +
|
||||
og[i + 1][j + 1].rgbtGreen) / 6.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i][j - 1].rgbtBlue +
|
||||
og[i][j + 1].rgbtBlue +
|
||||
og[i + 1][j - 1].rgbtBlue +
|
||||
og[i + 1][j].rgbtBlue +
|
||||
og[i + 1][j + 1].rgbtBlue) / 6.0);
|
||||
}
|
||||
|
||||
// lower side
|
||||
if (i == height - 1 && j > 0 && j < width - 1)
|
||||
{
|
||||
image[i][j].rgbtRed = (int)round((og[i][j].rgbtRed +
|
||||
og[i - 1][j - 1].rgbtRed +
|
||||
og[i - 1][j].rgbtRed +
|
||||
og[i - 1][j + 1].rgbtRed +
|
||||
og[i][j - 1].rgbtRed +
|
||||
og[i][j + 1].rgbtRed) / 6.0);
|
||||
image[i][j].rgbtGreen = (int)round((og[i][j].rgbtGreen +
|
||||
og[i - 1][j - 1].rgbtGreen +
|
||||
og[i - 1][j].rgbtGreen +
|
||||
og[i - 1][j + 1].rgbtGreen +
|
||||
og[i][j - 1].rgbtGreen +
|
||||
og[i][j + 1].rgbtGreen) / 6.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i - 1][j - 1].rgbtBlue +
|
||||
og[i - 1][j].rgbtBlue +
|
||||
og[i - 1][j + 1].rgbtBlue +
|
||||
og[i][j - 1].rgbtBlue +
|
||||
og[i][j + 1].rgbtBlue) / 6.0);
|
||||
}
|
||||
|
||||
// left side
|
||||
if (i > 0 && i < height - 1 && j == 0)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i - 1][j].rgbtRed +
|
||||
og[i - 1][j + 1].rgbtRed +
|
||||
og[i][j + 1].rgbtRed +
|
||||
og[i + 1][j].rgbtRed +
|
||||
og[i + 1][j + 1].rgbtRed) / 6.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i - 1][j].rgbtGreen +
|
||||
og[i - 1][j + 1].rgbtGreen +
|
||||
og[i][j + 1].rgbtGreen +
|
||||
og[i + 1][j].rgbtGreen +
|
||||
og[i + 1][j + 1].rgbtGreen) / 6.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i - 1][j].rgbtBlue +
|
||||
og[i - 1][j + 1].rgbtBlue +
|
||||
og[i][j + 1].rgbtBlue +
|
||||
og[i + 1][j].rgbtBlue +
|
||||
og[i + 1][j + 1].rgbtBlue) / 6.0);
|
||||
}
|
||||
|
||||
// right side
|
||||
if (i > 0 && i < height - 1 && j == width - 1)
|
||||
{
|
||||
image[i][j].rgbtRed = (int) round((og[i][j].rgbtRed +
|
||||
og[i - 1][j - 1].rgbtRed +
|
||||
og[i - 1][j].rgbtRed +
|
||||
og[i][j - 1].rgbtRed +
|
||||
og[i + 1][j - 1].rgbtRed +
|
||||
og[i + 1][j].rgbtRed) / 6.0);
|
||||
image[i][j].rgbtGreen = (int) round((og[i][j].rgbtGreen +
|
||||
og[i - 1][j - 1].rgbtGreen +
|
||||
og[i - 1][j].rgbtGreen + og[i][j - 1].rgbtGreen +
|
||||
og[i + 1][j - 1].rgbtGreen + og[i + 1][j].rgbtGreen) / 6.0);
|
||||
image[i][j].rgbtBlue = (int) round((og[i][j].rgbtBlue +
|
||||
og[i - 1][j - 1].rgbtBlue +
|
||||
og[i - 1][j].rgbtBlue +
|
||||
og[i][j - 1].rgbtBlue +
|
||||
og[i + 1][j - 1].rgbtBlue +
|
||||
og[i + 1][j].rgbtBlue) / 6.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//00 = [i - 1][j - 1]
|
||||
//01 = [i - 1][j]
|
||||
//02 = [i - 1][j + 1]
|
||||
//10 = [i][j - 1]
|
||||
//12 = [i][j + 1]
|
||||
//20 = [i + 1][j - 1]
|
||||
//21 = [i + 1][j]
|
||||
//22 = [i + 1][j + 1]
|
||||
|
||||
// Dazwischen
|
||||
//00 = [i - 1][j - 1]
|
||||
//01 = [i - 1][j]
|
||||
//02 = [i - 1][j + 1]
|
||||
//10 = [i][j - 1]
|
||||
//12 = [i][j + 1]
|
||||
//20 = [i + 1][j - 1]
|
||||
//21 = [i + 1][j]
|
||||
//22 = [i + 1][j + 1]
|
||||
|
||||
//Ecke Links oben
|
||||
//12 [i][j + 1]
|
||||
//21 [i + 1][j]
|
||||
//22 [i + 1][j + 1]
|
||||
|
||||
//Ecke Rechts oben
|
||||
//10 [i][j - 1]
|
||||
//20 [i + 1][j - 1]
|
||||
//21 [i + 1][j]
|
||||
|
||||
//Ecke Links unten
|
||||
//01 [i - 1][j]
|
||||
//02 [i - 1][j + 1]
|
||||
//12 [i][j + 1]
|
||||
|
||||
//Ecke Rechts unten
|
||||
//00 [i - 1][j - 1]
|
||||
//01 [i - 1][j]
|
||||
//10 [i][j - 1]
|
||||
|
||||
// Seite oben
|
||||
//10 [i][j - 1]
|
||||
//12 [i][j + 1]
|
||||
//20 [i + 1][j - 1]
|
||||
//21 [i + 1][j]
|
||||
//22 [i + 1][j + 1]
|
||||
|
||||
// Seite unten
|
||||
//00 [i - 1][j - 1]
|
||||
//01 [i - 1][j]
|
||||
//02 [i - 1][j + 1]
|
||||
//10 [i][j - 1]
|
||||
//12 [i][j + 1]
|
||||
|
||||
// Seite links
|
||||
//01 [i - 1][j]
|
||||
//02 [i - 1][j + 1]
|
||||
//12 [i][j + 1]
|
||||
//21 [i + 1][j]
|
||||
//22 [i + 1][j + 1]
|
||||
|
||||
// Seite rechts
|
||||
//00 [i - 1][j - 1]
|
||||
//01 [i - 1][j]
|
||||
//10 [i][j - 1]
|
||||
//20 [i + 1][j - 1]
|
||||
//21 [i + 1][j]
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "bmp.h"
|
||||
|
||||
// Convert image to grayscale
|
||||
void grayscale(int height, int width, RGBTRIPLE image[height][width]);
|
||||
|
||||
// Convert image to sepia
|
||||
void sepia(int height, int width, RGBTRIPLE image[height][width]);
|
||||
|
||||
// Reflect image horizontally
|
||||
void reflect(int height, int width, RGBTRIPLE image[height][width]);
|
||||
|
||||
// Blur image
|
||||
void blur(int height, int width, RGBTRIPLE image[height][width]);
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
typedef uint8_t BYTE;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("Usage: ./recover IMAGE\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *file = fopen(argv[1], "r");
|
||||
FILE *img = NULL;
|
||||
BYTE buffer[512];
|
||||
int img_number = 0;
|
||||
char img_name[8];
|
||||
|
||||
while (fread(&buffer, 512, 1, file) == 1)
|
||||
{
|
||||
if (buffer[0] == 0xff &&
|
||||
buffer[1] == 0xd8 &&
|
||||
buffer[2] == 0xff &&
|
||||
(buffer[3] & 0xf0) == 0xe0)
|
||||
{
|
||||
|
||||
if (!(img_number == 0))
|
||||
{
|
||||
fclose(img);
|
||||
}
|
||||
|
||||
sprintf(img_name, "%03i.jpg", img_number);
|
||||
img = fopen(img_name, "w");
|
||||
img_number ++;
|
||||
|
||||
}
|
||||
|
||||
if (!(img_number == 0))
|
||||
{
|
||||
fwrite(&buffer, 512, 1, img);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
fclose(img);
|
||||
return 0;
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user