added ws202425 courses
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
|
||||
Reference in New Issue
Block a user