Skip to content
This repository was archived by the owner on Nov 10, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Tanmay/pset1/credit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
int main()
{

long long int num;
int oddsum=0,evensum=0,finalsum=0;
printf("Enter your card number: ");
scanf("%lld",&num);
for(long long int a=num;a>0;a=a/100)
{
oddsum=oddsum+(a%10);
}
for(long long int b=num/10;b>0;b/=100)
{
evensum+=((2*(b%10))/10)+((2*(b%10))%10);
}
finalsum=oddsum+evensum;
if(finalsum%10!=0)
printf("Invalid card");
else
{
if(num/1000000000000000==0)
printf("It's an AMEX");
else if(num/10000000000000==0)
printf("It's a VISA");
else if(num/1000000000000000==4)
printf("It's a VISA");
else
printf("It's a MASTERCARD");
}
}



8 changes: 8 additions & 0 deletions Tanmay/pset2/caeser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Hello world!\n");
return 0;
}
70 changes: 70 additions & 0 deletions Tanmay/pset2/vigenere.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>



int main(int argc, string argv[])
{

if(argc != 2)
{
printf("Desired format: ./caesar k");
return 1;
}

string k = argv[1];

//Checks if the key is only alphabetical
for(int i = 0, n = strlen(k); i < n; i++)
{
if(!isalpha(k[i]))
{
printf("Key must contain only alphabets");
return 1;
}
}

string k_upper = k;


//Converts the whole key into uppercase
for(int i = 0, n = strlen(k_upper); i < n; i++)
{
k_upper[i] = toupper(k_upper[i]);
}

int letter = 0;
int key_length = strlen(k_upper);

string input = GetString();

for(int i = 0, n = strlen(input); i < n; i++)
{
int shift = k_upper[letter % key_length] - 65; //Number of shifts needed for te current letter in plaintext

if(isupper(input[i]))
{
if(input[i] + (shift % 26) > 'Z')
{
input[i] -= 26;
}
input[i] += shift % 26;
letter++;
}
else if(islower(input[i]))
{
if(input[i] + (shift % 26) > 'z')
{
input[i] -= 26;
}
input[i] += shift % 26;
letter++;
}

}

printf("%s\n", input);
}
56 changes: 56 additions & 0 deletions Tanmay/pset3/helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "helpers.h"

int duration(string fraction)
{
int num, den;
num = fraction[0] - '0';
den = fraction[2] - '0';
int result = 1;
result *= num;
result *= (8 / den);
return result;
}

//Calculates frequency (in Hz) of a note
int frequency(string notei)
{
double frequency;
int num;
string temp;
char note[3];
for(int i = 0; i < strlen(notei); i++)
{
//printf("%d",i);
if(!((notei[i] >= '0') && (notei[i] <= '9'))) note[i] = notei[i];
else
{
num = notei[i] - '0';
}
}
string values[12][2] = {{"C ", " "}, {"C#", "Db"}, {"D ", " "}, {"D#", "Eb"}, {"E ", " "}, {"F ", " "}, {"F#", "Gb"}, {"G ", " "}, {"G#", "Ab"}, {"A ", " "}, {"A#", "Bb"}, {"B ", " "}};
if(strlen(note) == 1) temp = strcat(note, " ");
else temp = note;
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 2; j++)
{
if(!(strcmp(temp, values[i][j])))
{
frequency = pow(2.0, ((((float) i) - 9) / 12.0));
}
}
}
frequency *= (440.0 * pow(2.0, ((float) num) - 4.0));
return (int) round(frequency);
}

bool is_rest(string s)
{
if(strcmp(s,"")) return false;
return true;
}
56 changes: 56 additions & 0 deletions Tanmay/pset4/recover.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <cs50.h>
#include <stdint.h>

#define BLOCK_SIZE 512

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{

if (argc != 2)
{
fprintf(stderr, "Usage: recover infile\n");
return 1;
}

char *infile = argv[1];
FILE *inptr = fopen(infile, "r");

if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}

BYTE buffer[512];
int imageCount = 0;
char filename[8];
FILE *outptr = NULL;

while (!feof(inptr))
{
int bytesRead = fread(buffer, sizeof(BYTE), BLOCK_SIZE, inptr);

bool containsJpegHeader = buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0;

if (containsJpegHeader)
{
if(outptr!=NULL)
{
fclose(outptr);
imageCount++;
}
sprintf(filename, "%03i.jpg", imageCount);
outptr = fopen(filename, "w");
}

if (outptr != NULL)
{
fwrite(buffer, sizeof(BYTE), bytesRead, outptr);
}
}
fclose(outptr);
fclose(inptr);
}
77 changes: 77 additions & 0 deletions Tanmay/pset4/resize/bmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* BMP-related data types based on Microsoft's own.
*/

#include <stdint.h>

/**
* Common Data Types
*
* The data types in this section are essentially aliases for C/C++
* primitive data types.
*
* Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
*/
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;

/**
* BITMAPFILEHEADER
*
* The BITMAPFILEHEADER structure contains information about the type, size,
* and layout of a file that contains a DIB [device-independent bitmap].
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx.
*/
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((__packed__))
BITMAPFILEHEADER;

/**
* BITMAPINFOHEADER
*
* The BITMAPINFOHEADER structure contains information about the
* dimensions and color format of a DIB [device-independent bitmap].
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=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;

/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
Loading