Skip to content
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
5 changes: 5 additions & 0 deletions assignment6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1) It won't compile, as it throws an error if you assign a value to a field in a struct. Also, it calls printf without importing stdio, and clrscr and getch without importing conio.h, which is exclusive to windows.

2) One would use a structure when they need to store data that requires lots of variables, whereas one would use enums to make a list of things of a fixed size, where the important value isn't the value of the variable, but its name.

3) Arrays are usually passed by reference to functions, because they are really just pointers in disguise. Structs are passed by value, so wrapping an array in a struct would allow it to be passed by value rather than reference.
48 changes: 48 additions & 0 deletions banking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Harry Brickner
Does basic banking operations */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct Account{
int id;
char name[10];
int balance;
}Account;

/* Withdraws money from an account into the real world. Keep in mind, the units are 1/100 of a dollar. */
void withdraw(Account *from, int amount){
from->balance -= amount;
}
/* Deposits money from the real world into the account. Keep in mind, the units are 1/100 of a dollar. */
void deposit(Account *to, int amount){
to->balance += amount;
}
/* Prints account info */
void printAccount(Account from){
/* Localization? Who needs it? Also, balance is an int to avoid floating point errors.*/
printf("User %d: %s\nBalance: $%01d.%02d\n", from.id, from.name, from.balance / 100, (from.balance * (from.balance < 0? -1 : 1)) % 100);
}

void driver(){
srand(time(0));
Account acc0 = {rand(), "Alice\0", 0};
Account acc1 = {rand(), "Bob\0", 0};
printAccount(acc0);
printAccount(acc1);
printf("Deposit\n");
deposit(&acc0, rand() % 100000);
deposit(&acc1, rand() % 100000);
printAccount(acc0);
printAccount(acc1);
printf("Withdraw\n");
withdraw(&acc0, rand() % 100000);
withdraw(&acc1, rand() % 100000);
printAccount(acc0);
printAccount(acc1);
}

int main(){
driver();
return 0;
}
66 changes: 66 additions & 0 deletions complex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Harry Brickner
* provides functions for basic complex arithmetic. */
#include <stdio.h>

typedef struct cDouble {
double imult;
double add;
}cDouble;

void printCdouble(cDouble num){
if(num.imult == 0)
/* 0i + ? */
printf("%.3lf", num.add);
else if(num.add == 0)
if(num.imult == 1)
/* 1i + 0 */
printf("i");
else
/* ?i + 0 */
printf("%.3lfi", num.imult);
else
if(num.imult == 1)
/* 1i + ? */
printf("i%+.3lf", num.add);
else
/* ?i + ? */
printf("%.3lfi%+.3lf", num.imult, num.add);
}



cDouble add(cDouble a, cDouble b){
cDouble ret = {a.imult + b.imult, a.add + b.add};
return ret;
}
cDouble sub(cDouble a, cDouble b){
cDouble ret = {a.imult - b.imult, a.add - b.add};
return ret;
}
cDouble mult(cDouble a, cDouble b){
cDouble ret = {(a.imult * b.add) + (a.add * b.imult), (a.add * b.add)-(a.imult * b.imult)};
return ret;
}
cDouble div(cDouble a, cDouble b){
cDouble ret;
double div = -(b.imult * b.imult) - (b.add * b.add);
ret.imult = ((a.add * b.imult) - (b.add * a.imult)) / div;
ret.add = -((a.imult * b.imult) + (a.add * b.add)) / div;
return ret;
}

int main(){
printf("Please input a calculation\nPlease note: numbers should be input as %%fi+%%f or %%fi-%%f.\nAlso note: There should be a space between the numbers and the operation, but not between the real and imaginary parts of the numbers.\n");
cDouble a;
cDouble b;
char operation;
scanf("%lfi%lf %c %lfi%lf", &(a.imult), &(a.add), &operation, &(b.imult), &(b.add));
switch(operation){
case '+': printCdouble(add(a, b)); break;
case '-': printCdouble(sub(a, b)); break;
case '*': printCdouble(mult(a, b)); break;
case '/': printCdouble(div(a, b)); break;
}
printf("\n");
return 0;
}
35 changes: 35 additions & 0 deletions studentThing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Harry Brickner
* Makes a bunch of students, then prints out their average score */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

typedef struct Student{
char name[20];
int age;
float scores[5];
}Student;

int main(){
srand(time(0));
struct Student students[5];
char names[5][9] = {"Bob\0","Joe\0","John\0","Alice\0","Reginald\0"};
for(int i = 0; i < 5; i++){
strcpy(students[i].name, names[i]);
students[i].age = (rand() % 18) + 16;
for(int j = 0; j < 5; j++)
students[i].scores[j] = (float)(rand() % 1000) / 10;
}

for(int i = 0; i < 5; i++){
printf("Name: %s\nScores:", students[i].name);
float average = 0;
for(int j = 0; j < 5; j++){
average += students[i].scores[j] / 5;
printf(" %d=%04.1f", j, students[i].scores[j]);
}
printf("\nAverage: %.1f\n\n", average);

}
}